React Emotion 是否支持 ::before/ ::after 选择器?

mik*_*ssy 5 emotion reactjs

我正在尝试使用React Emotion设置一条水平线,中间有文本。它看起来像这样:

----------------------SOME TEXT----------------------------------

我在 StackOverflow 上找到了有关如何使用 CSS 执行此操作的示例,但它对我不起作用。

这是我正在使用的 DIV,但它不起作用。

这可以用反应情感来设置吗?我缺少什么?

<div
  css={{
    display: 'flex',
    lineHeight:'1em',
    color:'gray',
    '&:before, &:after': {
        content:'',
        display:'inline-block',
        flexGrow:'1px',
        marginTop:'0.5em',
        backgroundColor:'gray',
        height:'1px',
        marginRight:'10px',
        marginLeft:'10px'
    }
   
  }}
>
  SOME TEXT
</div>
Run Code Online (Sandbox Code Playgroud)

ale*_*ejo 2

您的代码关于::beforeand::after语法是正确的,但是,在尝试复制时我发现了两个错误:

  1. flexGrow 不应该有 px 单位,只有1flexGrow:'1
  2. 内容应该有双引号content:'""'
<div
    css={{
      display: 'flex',
      lineHeight:'1em',
      color:'gray',
      '&:before, &:after': {
          content:'""',
          display:'inline-block',
          flexGrow:'1',
          marginTop:'0.5em',
          backgroundColor:'gray',
          height:'1px',
          marginRight:'10px',
          marginLeft:'10px'
      }
    }}
  >
Run Code Online (Sandbox Code Playgroud)