Is it possible to use (multiple) font styles within CSS `content`?
Asked Answered
J

2

10

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.

Jenijenica answered 24/7, 2015 at 22:5 Comment(3)
I don't think so (hack?), but I'll be interested to see if someone can work that out.Cherlycherlyn
Seems to be impossible, according to the answers 1 and 2. It's probably will be marked as a duplicate.Aminopyrine
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?Nahuatlan
O
2

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;
}
Orcein answered 25/7, 2015 at 13:5 Comment(0)
R
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.

Randy answered 7/11, 2016 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.