如何在样式组件中设置 ::selection 伪元素的样式?
我想用一个样式组件来做到这一点:
.jTHnFZ ::selection {
background-color: pink;
}
我编写了这个样式组件:
const MyDiv = styled.div`
::selection {
background-color: pink;
}
`
但结果是:
.jTHnFZ::selection {
background-color: pink;
}
结果与这个样式组件相同:
const MyDiv = styled.div`
& ::selection {
background-color: pink;
}
`
我发现将颜色赋予 ::selection 伪元素就是这样做的:
const MyDiv = styled.div`
*::selection {
background-color: pink;
}
`
结果是:
.jTHnFZ *::selection {
background-color: pink;
}
它可以工作,但是有更好的方法来做到这一点,避免 *
吗?
I want to do that with a styled component:
.jTHnFZ ::selection {
background-color: pink;
}
I write this styled component:
const MyDiv = styled.div`
::selection {
background-color: pink;
}
`
But the result is:
.jTHnFZ::selection {
background-color: pink;
}
The result is the same with this styled component:
const MyDiv = styled.div`
& ::selection {
background-color: pink;
}
`
The only way I found to attibute a color to the ::selection
pseudo element is to do this:
const MyDiv = styled.div`
*::selection {
background-color: pink;
}
`
And the result is then:
.jTHnFZ *::selection {
background-color: pink;
}
It works but is there a better way to do it, avoiding the *
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
请删除
&
之后的空格:注意: https://www.reddit.com/r/reactjs/comments/nz8gt9/using_the_selection_css_attribute_using_styled/
Try this:
Please remove space after
&
:Note to: https://www.reddit.com/r/reactjs/comments/nz8gt9/using_the_selection_css_attribute_using_styled/