Can Reactjs programmatically handle :before?

后端 未结 3 1167
执念已碎
执念已碎 2021-01-19 06:46

I somehow have to programmatically set the width of the :before for a div.

&l
相关标签:
3条回答
  • 2021-01-19 07:13

    @diehell, a working example of using the <style> component for pseudo elements in ReactJS:

    <style>
      {`.element::before {
        content: '';
        ...other CSS you want to use
      }`}
    </style>
    
    0 讨论(0)
  • 2021-01-19 07:24

    Pseudo elements cannot be styled with inline styles as explained in https://stackoverflow.com/a/14141821/368697. You will have to style the something class name in a stylesheet with the .something:before selector. This is not a limitation of React but rather a design choice for HTML + CSS.

    If you need to programmatically change the width of the pseudo :before element, it is probably more appropriate as a regular DOM element rendered by React.

    0 讨论(0)
  • 2021-01-19 07:32

    Yes, you can programmatically change the value of pseudo-elements like ::before, ::after in react.

    Here is a trick.

    app.js

    const widthVar = 34;
    const someStyle = {
         "--width": widthVar
    }
    <div className="something" style={someStyle}> </div>
    

    style.css

    .something:before{
          width: var(--width),
          // remaining code
    }
    
    0 讨论(0)
提交回复
热议问题