:before 在 DOM 中如何渲染?

huangapple go评论109阅读模式
英文:

How do I make :before render in the DOM?

问题

I can't figure out how to make :before render in the DOM.

我不知道如何在 DOM 中呈现 :before。

I tried this:

我尝试了以下代码:

  1. const inline = {
  2. backgroundColor: "yellow",
  3. width: "100px",
  4. height: "30px",
  5. display: "inline-block",
  6. position: "relative",
  7. "&::before": {
  8. content: '"Good Day"',
  9. position: "absolute",
  10. left: "-50px",
  11. top: "50px",
  12. },
  13. }
  14. return (
  15. <p style={style}>
  16. Lorem ipsum dolor sit amet, <i style={inline}></i>consectetur
  17. adipiscing elit, sed do<i style={inline}></i> eiusmod tempor
  18. incididunt ut labore et dolore magna aliqua.
  19. <i style={inline}></i>
  20. </p>
  21. )

the styling of the i elements works as expected, except for the :before that is not rendered.

i 元素的样式按预期工作,除了 :before 不会被呈现。

Any tip on how t accomplish this would be much appreciated 🙌

对于如何实现这一点的任何提示将不胜感激 🙌

英文:

I can't figure out how to make :before render in the DOM.

I tried this:

  1. const inline = {
  2. backgroundColor: &quot;yellow&quot;,
  3. width: &quot;100px&quot;,
  4. height: &quot;30px&quot;,
  5. display: &quot;inline-block&quot;,
  6. position: &quot;relative&quot;,
  7. &quot;&amp;::before&quot;: {
  8. content: &#39;&quot;Good Day&quot;&#39;,
  9. position: &quot;absolute&quot;,
  10. left: &quot;-50px&quot;,
  11. top: &quot;50px&quot;,
  12. },
  13. }
  14. return (
  15. &lt;p style={style}&gt;
  16. Lorem ipsum dolor sit amet, &lt;i style={inline}&gt;&lt;/i&gt;consectetur
  17. adipiscing elit, sed do&lt;i style={inline}&gt;&lt;/i&gt; eiusmod tempor
  18. incididunt ut labore et dolore magna aliqua.
  19. &lt;i style={inline}&gt;&lt;/i&gt;
  20. &lt;/p&gt;
  21. )

the styling of the i elements works as expected, except for the :before that is not rendered.

Any tip on how t accomplish this would be much appreciated 🙌

答案1

得分: 1

  1. const inline = { /* ... */
  2. "&::before": { /* ... */ },
  3. }

The ampersand ("parent selector") is a part of SCSS/SASS/LESS, not the raw CSS properties that React can understand.

There is no syntax that allows ":before" applied as an inline style, as in this SO answer, nor through JavaScript manipulation as in this SO answer.

英文:
  1. const inline = { /* ... */
  2. &quot;&amp;::before&quot;: { /* ... */ },
  3. }

The ampersand ("parent selector") is a part of SCSS/SASS/LESS, not the raw CSS properties that React can understand.

There is no syntax that allows ":before" applied as an inline style, as in this SO answer, nor through JavaScript manipulation as in this SO answer.

答案2

得分: 1

内联CSS样式不支持伪元素(它们不是元素本身的一部分)。唯一将样式应用于伪元素的方法是使用CSS声明(例如:设置一个id并设置带有#id::before{ }的CSS声明)。

对于ReactJS组件,一种选项是使用类似于:before(或:after)伪元素的显式标签,将其添加到元素内容的开头或末尾。例如:

  1. const inline = {
  2. backgroundColor: "yellow",
  3. width: "100px",
  4. height: "30px",
  5. display: "inline-block",
  6. position: "relative",
  7. }
  8. const before = {
  9. position: "absolute",
  10. left: "-50px",
  11. top: "50px",
  12. }
  13. return (
  14. <p style={style}>
  15. Lorem ipsum dolor sit amet, <i style={inline}></i>consectetur
  16. adipiscing elit, sed do<i style={inline}></i> eiusmod tempor
  17. incididunt ut labore et dolore magna aliqua.
  18. <i style={inline}><div style={before}>"Good Day"</div>Content</i>
  19. </p>
  20. )
英文:

Inline css styles do not support pseudo elements (they are not part of the element itself). The only way to apply style to pseudo elements is using css declarations (eg: setting an id and setup a css declaration with #id::before{ }).

One option for reactjs components is to use an explicit tag that works like :before (or :after) pseudo element, adding it to the start or end of the element content. Eg:

  1. const inline = {
  2. backgroundColor: &quot;yellow&quot;,
  3. width: &quot;100px&quot;,
  4. height: &quot;30px&quot;,
  5. display: &quot;inline-block&quot;,
  6. position: &quot;relative&quot;,
  7. }
  8. const before = {
  9. position: &quot;absolute&quot;,
  10. left: &quot;-50px&quot;,
  11. top: &quot;50px&quot;,
  12. }
  13. return (
  14. &lt;p style={style}&gt;
  15. Lorem ipsum dolor sit amet, &lt;i style={inline}&gt;&lt;/i&gt;consectetur
  16. adipiscing elit, sed do&lt;i style={inline}&gt;&lt;/i&gt; eiusmod tempor
  17. incididunt ut labore et dolore magna aliqua.
  18. &lt;i style={inline}&gt;&lt;div style={before}&gt;&quot;Good Day&quot;&lt;/div&gt;Content&lt;/i&gt;
  19. &lt;/p&gt;
  20. )

huangapple
  • 本文由 发表于 2023年3月31日 02:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定