4

How can an HTML tag be inserted by content: property of the ::before pseudo-element, I am trying to an <hr> before each <article> tag, so far I have:

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

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

Thanks,

NickC
  • 1,253
  • 5
  • 16
  • 23
  • Are you specifically looking to introduce a hr tag or just a line before every article? – Harry Feb 13 '15 at 13:23
  • You can't use CSS for adding pseudo HTML tags. See [this](http://stackoverflow.com/questions/5865937/using-before-and-after-css-selector-to-insert-html). – Nikhil Girraj Feb 13 '15 at 13:27

2 Answers2

4

You can't inject HTML elements using the content property of CSS pseudo-elements; you can, however, set them to display: block and (as seems to be required in your question, though not your demonstrated/attempted code) use height and background-color to emulate an <hr />:

article::before {
    content: '';
    display: block;
    margin: 1em 0;
    height: 5px;
    background-color: #f00;
}

article::before {
  content: '';
  display: block;
  margin: 1em 0;
  height: 5px;
  background-color: #f00;
}
<article>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</article>
<article>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</article>

You could, of course, supply a width, and use auto for margin-left and margin-right to centre a 'shorter' pseudo-<hr />:

article::before {
    content: '';
    display: block;
    margin: 1em auto;
    height: 5px;
    width: 80%;
    background-color: #f00;
}

article::before {
  content: '';
  display: block;
  margin: 1em auto;
  height: 5px;
  width: 80%;
  background-color: #f00;
}
<article>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum.</article>
<article>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
  one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
  et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
  1.10.32.</article>

If you don't want this pseudo-<hr /> before the first <article> you could change your selector to:

article:not(:first-child)::before { /* ...CSS... */}

Or, assuming the <article> elements are siblings:

article + article::before { /* ...CSS... */ }
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • Thanks David that works well. The actual CSS I found to replicate the
    tag closest is: article::before { content: ''; display: block; margin: 1em 0; height: 1px; background-color: dimgray ; border-bottom: 1px lightgray solid ;
    – NickC Feb 13 '15 at 15:01
1

There isn't any way to insert HTML tags with CSS content. This answer suggests using jQuery, if that's an option.

Community
  • 1
  • 1