如何正确地将 :before
和 :after
伪类应用于样式化组件?
我知道你可以使用
&:hover {}
来将 :hover
伪类应用于样式化组件。
对于所有伪元素(例如 before 和 after),这种方法是否都适用?
我尝试过在一些比较复杂的示例中使用 &:before
和 &:after
策略,但我不确定我的尝试是否失败是因为我在示例中搞错了什么,还是它就不是这样工作的。
是否有人对此有所了解?谢谢。
如何正确地将 :before
和 :after
伪类应用于样式化组件?
我知道你可以使用
&:hover {}
来将 :hover
伪类应用于样式化组件。
对于所有伪元素(例如 before 和 after),这种方法是否都适用?
我尝试过在一些比较复杂的示例中使用 &:before
和 &:after
策略,但我不确定我的尝试是否失败是因为我在示例中搞错了什么,还是它就不是这样工作的。
是否有人对此有所了解?谢谢。
styled-components
中,伪选择器的工作方式与CSS中的工作方式相同(或者更准确地说是Sass)。如果某些东西不起作用,可能是您特定代码中的问题,但是如果没有看到实际代码,很难进行调试!< /p >
< p >下面是使用简单的:after
的示例: < /p >const UnicornAfter = styled.div`
&:after {
content: " ";
}
`;
<UnicornAfter>I am a</UnicornAfter> // renders: "I am a "
如果您发布您的代码,我很可能能够帮助调试您特定的问题!&:before
和&:after
是否有效。我在谷歌上找不到明确的答案...所以...谢谢! - rimraf这将在div的中间打印三角形。
const LoginBackground = styled.div`
& {
width: 30%;
height: 75%;
padding: 0.5em;
background-color: #f8d05d;
margin: 0 auto;
position: relative;
}
&:after {
border-right: solid 20px transparent;
border-left: solid 20px transparent;
border-top: solid 20px #f8d05d;
transform: translateX(-50%);
position: absolute;
z-index: -1;
content: "";
top: 100%;
left: 50%;
height: 0;
width: 0;
}
`;
继承 @mxstbr 的答案:
请注意,如果您想根据 props 渲染内容,请不要忘记用双引号(或单引号)将其包装起来,例如:
const Button = styled.button`
&:before {
content:"${(props)=>props.theme==='dark'?'dark theme':'white theme'}";
}
content:${(props)=>props.theme==='dark'?'dark theme':'white theme'};
无法正常工作
https://dev59.com/XVcO5IYBdhLWcg3wZQzX#45871869,作者为mxstbr。
但是对于需要更复杂逻辑的元素,我更喜欢以下方法:const Figure = styled.div`
${Square}:before,
${Square}:after,
${Square} div:before,
${Square} div:after {
background-color: white;
position: absolute;
content: "";
display: block;
-webkit-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}
`;
作为一个对象(注意双引号):
const Div = styled.div({
'&:before': {
content: '"a string"',
},
})
Can try like this.
It works perfectly fine
var setValue="abc";
var elementstyle = '<style>YourClass:before { right:' + abc + 'px;}</style>'
$(".YourClass").append(setValue);
var rightMarginForNotificationPanelConnectorStyle = '<style>.authenticated-page.dx-authenticated .dx-notification .dx-notification-dialog.dx-content-frame:before { right:' + rightMarginForNotificationPanelConnectorWithBadge + 'px;}</style>'
$(".authenticated-page.dx-authenticated .dx-notification .dx-notification-dialog.dx-content-frame").append(rightMarginForNotificationPanelConnectorStyle);