7

I've got a CSS element set up to insert some content, via:

.foo:after {
  content: "Bold, italics";
}

I'd like the word "Bold" to be rendered in a bold font-weight and the word "italics" to be rendered in an italics font-style. I know it's possible to add lines:

font-weight: bold;
font-style: italics;

But this will make both words bold and italics. If I could use html elements inside the content field I would put something like:

content: "<strong>Bold</strong>, <em>italics</em>"

But alas that's not possible.

Is there another way to achieve this effect? Ideally without invoking javascript and purely using html/css.

Community
  • 1
  • 1
Alec Jacobson
  • 5,326
  • 4
  • 41
  • 75
  • 1
    I don't think so (hack?), but I'll be interested to see if someone can work that out. – Jared Farrish Jul 24 '15 at 22:11
  • Seems to be impossible, according to the answers [1](http://stackoverflow.com/questions/4505093/css-content-property-is-it-possible-to-insert-html-instead-of-text) and [2](http://stackoverflow.com/questions/22511499/add-html-tag-inside-css-content-property). It's probably will be marked as a duplicate. – Stickers Jul 24 '15 at 22:20
  • 3
    other than using `.foo:before { font-weight: bold; }` and then `.foo:after { font-style: italics; }` I would also love to know this one too. Seems like doing only one psuedo w/o changing the markup isn't possible? – s0rfi949 Jul 24 '15 at 22:22

2 Answers2

2

You do have other pseudo elements than 'after'/'before', like first-line or first-letter, which, with some imagination, maybe you could use on your particularly case:
w3schools Pseudo-elements
But 'inside' those first 2 I think you can not do nothing more, like @s0rfi949 pointed out.

Pedro Ferreira
  • 449
  • 6
  • 12
1

It's mentioned above, but unless you add a :before and :after not too sure how it can be accomplished without JS..

.foo {
  float: left;
}
.foo:after {
  content: "Bold, ";
  font-weight: bold;
  float: right;
  margin-left: .5em;
  display: block;
}
.foo:before {
  content: 'Italic';
  font-style: italic;
  float: right;
  margin-left: .5em;
  display: block;
}

It also contains floats everywhere, but, hey! it works:)

Check it here: http://codepen.io/achoukah/pen/gpBopd

EDIT:

Heres the same, but with flex-box:

.foo {
  width: 100%;
  clear: both;
  display: flex;
}
.foo:before {
  content: "Bold, ";
  font-weight: bold;
  margin-left: .5em;
  order: 1;

}
.foo:after {
  content: 'Italic';
  font-style: italic;
  margin-left: .5em;
  order: 2;
}
achoukah
  • 865
  • 1
  • 7
  • 10