4

I am trying to use the pseudo-element ::before to insert an <hr> before each <article> tag, so far I have:

  article::before {
    content: "\003C br \\ \003E";
  }

but this doesn't work. Is there some other way of inserting HTML tags into pseudo-element CSS?

NickC
  • 1,253
  • 5
  • 16
  • 23

2 Answers2

5

As said by @rounin you can't insert html in the generated content.

But hr is just an element with its custom CSS styling.

You can recreate the styling for your generated content

article::before {
  content: "";
  border: 1px inset;
  box-sizing: border-box;
  color: gray;
  display: block;
  height: 2px;
  margin: 0.5em auto;
}
<article>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque nihil odit impedit ipsam et ducimus.</article>
<article>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim deleniti!
</article>
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
4

You can't insert HTML markup into the content: attribute of the ::before pseudo-element - the CSS spec won't allow it.

However, you can simulate an <hr> before the contents of your article (if that's what you need) simply by declaring margin-top: followed by border-top: followed by padding-top:

article {
  margin-top: 12px;
  border-top: 1px solid rgba(0, 0, 0, 0.4);
  padding-top: 12px;
}
<article>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque nihil odit impedit ipsam et ducimus.</article>
<article>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim deleniti!
</article>
OXiGEN
  • 2,041
  • 25
  • 19
Rounin
  • 27,134
  • 9
  • 83
  • 108