css - 将 css 样式转换为材质 ui makeStyles
问题描述
我最初有这个 CSS 样式
const Root = styled.div`
display: flex;
flex-direction: row;
margin: 0 auto;
width: 100%;
position: relative;
@media (min-width: ${(p) => p.theme.screen.md}) {
width: ${(p) => p.theme.screen.md};
// p.theme.screen.md is 1007px
}
@media (min-width: ${(p) => parseInt(p.theme.screen.lg, 10) + 20 + 'px'}) {
width: ${(p) => p.theme.screen.lg};
// p.theme.screen.lg is 1100px
}
`;
我想将其转换为材质 ui makeStyle css 我能够做到这一点
const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
flexDirection: 'row',
margin: '0 auto',
padding: '20px',
width: '100%',
position: 'relative',
},
}));
我不明白如何改变那些@media 的东西(mixins),请有人帮忙。(并且不要给我文档链接,我读了它,但无法理解)
解决方案
您可以使用主题对象中的断点来添加基于宽度的样式
const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
flexDirection: 'row',
margin: '0 auto',
padding: '20px',
width: '100%',
position: 'relative',
[theme.breakpoints.up('md')]: {
// if you want to set the md size value
width:theme.breakpoints.width('md')
},
[theme.breakpoints.up('lg')]: {
// if you want to set the lg size value
width:theme.breakpoints.width('lg')
},
}}));
向上 -> 最小宽度,向下 -> 最大宽度
这些是默认断点
xs 超小:0 像素,sm 小:600 像素,md 中:960 像素,lg 大:1280 像素,xl 超大:1920 像素,
编辑:用于更新默认断点主题值
您需要在传递给 ThemeProvider 的主题对象中传递断点对象及其值,如下所示
const theme = {
...
breakpoints: {values: {xs: 0, sm: 600, md: 1007, lg: 1100, xl: 1920}}
}
推荐阅读
- java - Spring ExceptionHandler for Request method 'POST' not supported
- bash - 删除常规文本文件中的字母间距
- mysql - MYSQL; 根据表 A 和表 B 的连接更新表 A
- node.js - Node.js 中间件阻塞了程序,为什么程序无法运行?
- python - Python hash with if case
- angular - Angular 6递归组件创建多级问题答案层次结构
- c# - 如果不和谐机器人中的字符串为空
- regex - “使用未定义的常量”如何修复所有文件中的替换
- javascript - 如何获取 FullCalendar 中所有事件的 id
- asp.net - ASP.NET MVC Enitity 框架计算商