css - 如何设置嵌套元素的样式以与样式组件做出反应?
问题描述
我正在使用样式化组件,并希望通过嵌套 css 为 div 内的子项设置样式。在这里查看我的演示
const styles = theme => ({
root: {
...theme.typography.button,
backgroundColor: theme.palette.common.white,
padding: theme.spacing.unit,
">span": {
background: "red"
}
}
});
function TypographyTheme(props) {
return (
<div className={props.classes.root}>
<h1>
<span>{"This div's text looks like that of a button."}</span>
</h1>
</div>
);
}
解决方案
它看起来像 JSS 语法,而不是样式化组件。
改变这个:
const styles = theme => ({
root: {
...theme.typography.button,
backgroundColor: theme.palette.common.white,
padding: theme.spacing.unit,
">span": {
background: "red"
}
}
});
有了这个:
const styles = theme => ({
root: {
...theme.typography.button,
backgroundColor: theme.palette.common.white,
padding: theme.spacing.unit,
"& span": {
background: "red"
}
}
});
或者如果您不更改所有嵌套跨度
const styles = theme => ({
root: {
...theme.typography.button,
backgroundColor: theme.palette.common.white,
padding: theme.spacing.unit,
"& > h1 > span": {
background: "red"
}
}
});
推荐阅读
- reactjs - React 找不到导出的 renderRoutes 函数
- python - 通过sqlalchemy重复插入sqlite数据库导致内存泄漏?
- python-3.x - 根据另一列的进度值创建数据框列?
- c - 何时执行功能结语?
- docker - 通过 Docker 中的 nginx 代理将烧瓶套接字事件传递到 uWSGI 服务器的位置
- javascript - $(变量).find(); 返回未定义
- matlab - 如何从 Julia 中的字典创建结构或类型?
- r - 什么时候可以使用方括号,什么时候可以使用 $ 符号?
- mysql - SQL 列出所有重复项
- sql - 如何从表中检索所有列,但一列应在另一表中查找值(SQL-server)