覆盖像 MuiTab 这样使用媒体查询的组件

Ben*_*nit 10 css material-ui

我正在尝试为 MuiTab 提供 CSS 覆盖以增加字体大小。

使用关于 material-ui 上 CSS 覆盖的文档我设法增加了大多数元素的字体大小,但是我被困在使用媒体查询的元素上,因为它们产生的 CSS 规则比我提供的覆盖规则更具体。

主题.ts:

import { createMuiTheme } from '@material-ui/core';

const fontSizeStyle = {
  fontSize: '1rem',
};

const fontFamilyStyle = {
  fontFamily: '"Ubuntu", sans-serif'
};

const theme = createMuiTheme({
  overrides: {
    MuiTab: {
      root: {
        ...fontFamilyStyle,
        ...fontSizeStyle,
      },
      label: fontSizeStyle,
    },
  }
});

export default theme;
Run Code Online (Sandbox Code Playgroud)

这会生成以下应用于 MuiTab 的 css 规则:

muitab 覆盖示例

该规则由以下文件生成:

https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Tab/Tab.js

[theme.breakpoints.up('md')]: {
  fontSize: theme.typography.pxToRem(13),
},
Run Code Online (Sandbox Code Playgroud)

有没有人有如何使用 createMuiTheme 函数覆盖此媒体查询的示例?我没有断点,所以也许我还需要指定断点才能在我的覆盖中使用它们

亲切的问候

小智 11

我通过以下方式指定它解决了它:

    MuiTab: {
      root: {
        minWidth: 0,
        '@media (min-width: 0px)': {
          minWidth: 0
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)


Mus*_*ris 7

指定如下

let theme = createMuiTheme({});

theme = {
  ...theme,
  overrides: {
    MuiTab: {
      root: {
        [theme.breakpoints.up("xs")]: {
          minHeight: 10
        }
      }
    }
  }
}

export default theme;
Run Code Online (Sandbox Code Playgroud)

theme.breakpoints 公开了四种帮助方法来创建 CSS 媒体查询:

  • theme.breakpoints.up(key)
  • theme.breakpoints.down(key)
  • theme.breakpoints.only(key)
  • theme.breakpoints.between(start, end)

其中每个key都是一个断点并与固定的屏幕宽度匹配。允许的键值为xs|sm|md|lg|xl

有关更多信息,请参阅material-ui 文档


ano*_*iva 1

我也面临同样的问题。我阅读了有关断点的文档并找到了解决这种情况的方法,但我发现它有点丑陋,因为我必须在每个Tab使用中应用重写的样式classes

createMuiTheme注意:我不知道使用函数解决这个问题

将样式应用于样式breakpoints。在这种情况下,

const styles = theme => ({
  mediaFont:{
    [theme.breakpoints.up('md')]: {
     fontSize:fontSizeStyle.fontSize,
    },
  },
  });
Run Code Online (Sandbox Code Playgroud)

将以上样式应用到TabLabel

<Tab label="Item One" classes={{label:classes.mediaFont}} />
Run Code Online (Sandbox Code Playgroud)