gpt4 book ai didi

reactjs - 符号 &tagname : for css styling in material UI. 的含义是什么 Ex: &div: { display: 'flex' '}

转载 作者:行者123 更新时间:2023-12-01 23:34:09 25 4
gpt4 key购买 nike

我正在使用 React Material UI。符号 &div: 在组件中的此 css 样式中意味着什么。 css 样式如下。

  contactWrapper: {
marginTop: theme.spacing(1),
'& div': {
display: 'flex',
},
},

下面是使用该类的代码片段。

 <div className={classes.contactWrapper}>
<span className={classes.contentLabel}> Contact:</span>
<div><Person className={classes.contactIcon} fontSize="small" /> {primaryContact.name}</div>
<div><Phone className={classes.contactIcon} fontSize="small" /> {primaryContact.phone}</div>
</div>
</div>

最佳答案

它是 Material-ui 的一部分 Styled-Components API .它是元素父级类名的引用。

The ampersand (&) can be used to refer back to the main component.

Ampersands (&) get replaced by our generated, unique classname forthat styled component, making it easy to have complex logic.

参见 Material-UI 的 Nesting Selectors演示。

const useStyles = makeStyles({
root: {
color: 'red',
'& p': { // p that is child of root classname
margin: 0,
color: 'green',
'& span': { // span that is child of parent p
color: 'blue',
},
},
},
});

export default function NestedStylesHook() {
const classes = useStyles();

return (
<div className={classes.root}>
This is red since it is inside the root.
<p>
This is green since it is inside the paragraph{' '}
<span>and this is blue since it is inside the span</span>
</p>
</div>
);
}

关于reactjs - 符号 &tagname : for css styling in material UI. 的含义是什么 Ex: &div: { display: 'flex' '},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65840468/

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