Alec Jacobson
Alec Jacobson

Reputation: 6264

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

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.

Upvotes: 10

Views: 8907

Answers (2)

Pedro Ferreira
Pedro Ferreira

Reputation: 493

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.

Upvotes: 2

achoukah
achoukah

Reputation: 1445

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;
}

Upvotes: 2

Related Questions