javascript - 如何在 Reactjs Material UI 上使用 CSS @media 响应 makeStyles?

标签 javascript html css reactjs material-ui

const useStyles = makeStyles(theme => ({
  wrapper: {
    width: "300px"
  },
  text: {
    width: "100%"
  },
  button: {
    width: "100%",
    marginTop: theme.spacing(1)
  },
  select: {
    width: "100%",
    marginTop: theme.spacing(1)
  }
}));

有没有办法在上述变量中​​使用 CSS @media?

如果不可能,我怎样才能使我的自定义 css 响应?

最佳答案

下面的示例显示了在 makeStyles 中指定媒体查询的两种方法。 (下面是使用 styled 的 v5 示例)。您可以使用 up , down , only , 和 between theme.breakpoints 中的函数(根据主题中指定的断点为您生成媒体查询字符串),或者您可以直接使用媒体查询。

import React from "react";
import Button from "@material-ui/core/Button";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  button: {
    color: "white",
    [theme.breakpoints.down("xs")]: {
      marginTop: theme.spacing(1),
      backgroundColor: "purple"
    },
    [theme.breakpoints.between("sm", "md")]: {
      marginTop: theme.spacing(3),
      backgroundColor: "blue"
    },
    "@media (min-width: 1280px)": {
      marginTop: theme.spacing(5),
      backgroundColor: "red"
    }
  }
}));
export default function App() {
  const classes = useStyles();
  return (
    <Button className={classes.button} variant="contained">
      Hello World!
    </Button>
  );
}
Edit media queries
相关文档:
  • https://material-ui.com/customization/breakpoints/
  • https://cssinjs.org/jss-plugin-nested/?v=v10.1.1#nest-at-rules

  • 下面是一个使用 Material-UI v5 的类似示例。这已被调整为使用 styled而不是 makeStyles以及 theme.breakpoints.down 的用法和 theme.breakpoints.between已根据这些函数的行为变化进行了调整(down 现在不包含指定的断点而不是包含的断点,并且 between 的结束断点现在也是不包含的,因此对于这两个断点都需要指定比 v4 中使用的内容更上一层楼)。此外,min-width直接指定的媒体查询的数量已从 1280px 调整至1200px匹配 lg 的新值v5 中的断点。
    import React from "react";
    import Button from "@material-ui/core/Button";
    import { styled } from "@material-ui/core/styles";
    
    const StyledButton = styled(Button)(({ theme }) => ({
      color: "white",
      [theme.breakpoints.down("sm")]: {
        marginTop: theme.spacing(1),
        backgroundColor: "purple"
      },
      [theme.breakpoints.between("sm", "lg")]: {
        marginTop: theme.spacing(3),
        backgroundColor: "blue"
      },
      "@media (min-width: 1200px)": {
        marginTop: theme.spacing(5),
        backgroundColor: "red"
      }
    }));
    export default function App() {
      return <StyledButton variant="contained">Hello World!</StyledButton>;
    }
    
    Edit media queries
    从 v4 到 v5 的断点更改文档:https://next.material-ui.com/guides/migration-v4/#theme

    关于javascript - 如何在 Reactjs Material UI 上使用 CSS @media 响应 makeStyles?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61010951/

    相关文章:

    javascript - 是否有 "function(){return false;}"的 JavaScript(或 jQuery)快捷方式?

    javascript - 加载 HTML 格式的大型视频文件

    javascript - 如何获取用户的输入并将其存储以用于 Javascript 函数?

    html - 如何只允许在主要部分滚动? (HTML 和 CSS)

    html - 如何确定父div的高度并为子div设置?

    javascript - Highcharts.js : Strange rendering of stacked graph

    javascript - 如何用一个数组填充另一个数组?

    javascript - "window.location.hash = location.hash"在 Webkit(Safari 和 Chrome)中不起作用

    css - 蓝图CSS不内联两个div

    html - 如何将边界边缘拉近以消除边界内的空间