gpt4 book ai didi

javascript - Material -ui makeStyles : how to style by tag name?

转载 作者:行者123 更新时间:2023-12-01 15:55:35 24 4
gpt4 key购买 nike

我想为所有 <p> 添加一条规则在当前组件中。我在任何地方都找不到有关如何按标签名称添加 CSS 规则的信息(material-ui 文档、Stack Overflow 等)。

不支持吗?

我正在尝试做这样的事情:

const useStyles = makeStyles((theme: Theme) =>
createStyles({
'p': {
margin: 0,
},
someClass: {
fontSize: 14,
},
})
);

编辑:

使用 Ryan 的解决方案有效,但会产生一个新问题:
import React from 'react';
import { makeStyles, Theme, createStyles } from '@material-ui/core';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
'& p': {
margin: 0,
},
},
// I want this rule to override the rule above. It's not possible in the current configuration, because `.root p` is more specific than `.title`
// This problem originates from the fact that I had to nest the rule for `<p>` inside a CSS class
title: {
margin: '0 0 16px',
},
})
);

export default () => {
const classes = useStyles({});

return (
<div className={classes.root}>
<p className={classes.title}>My title</p>
<p>A paragraph</p>
<p>Another paragraph</p>
</div>
);
};

最佳答案

由于您希望将其作用于您的组件,因此您需要一个类来应用到您的组件(例如,下面示例中的 classes.root)。然后你可以定位所有p使用 & p 的元素.如果您需要覆盖 p组件中另一个 CSS 类的标签样式,您可以使用另一个嵌套规则来定位 p也具有该类的标签(例如示例中的 classes.title)。

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

const useStyles = makeStyles(theme => ({
root: {
"& p": {
margin: 0,
"&$title": {
margin: "0 0 16px"
}
}
},
title: {}
}));
export default function App() {
const classes = useStyles();
return (
<div className="App">
<p>This paragraph isn't affected.</p>
<p>This paragraph isn't affected.</p>
<div className={classes.root}>
<p className={classes.title}>My title</p>
<p>A paragraph</p>
<p>Another paragraph</p>
</div>
</div>
);
}

Edit Target nested tags in JSS

相关文档: https://cssinjs.org/jss-plugin-nested?v=v10.1.1#use--to-reference-selector-of-the-parent-rule

关于javascript - Material -ui makeStyles : how to style by tag name?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61345362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com