Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 878
Alec Jacobson Avatar asked Jul 24 '15 22:07

Alec Jacobson


People also ask

How to use font-family in CSS?

In CSS, we use the font-family property to specify the font of a text. The font-family property should hold several font names as a "fallback" system, to ensure maximum compatibility between browsers/operating systems. Start with the font you want, and end with a generic family (to let the browser pick a similar font in the generic family, ...

What is the importance of font in CSS?

The font adds value to your text. It is also important to choose the correct color and text size for the font. In CSS there are five generic font families: Serif fonts have a small stroke at the edges of each letter. They create a sense of formality and elegance. Sans-serif fonts have clean lines (no small strokes attached).

What are the different types of fonts in CSS?

Some Font Examples 1 Times New Roman Georgia Garamond 2 Arial Verdana Helvetica 3 Courier New Lucida Console Monaco 4 Brush Script MT Lucida Handwriting 5 Copperplate Papyrus. In CSS, we use the font-family property to specify the font of a text. ...

Can I use two fonts in the same stylesheet with @font-face?

Can I use both in the same stylesheet? Just list them together as I've shown in my example. There is no reason you can't declare both. All that @font-face does is instruct the browser to download and make a font-family available.


2 Answers

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.

like image 79
Pedro Ferreira Avatar answered Oct 15 '22 11:10

Pedro Ferreira


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;
}
like image 26
achoukah Avatar answered Oct 15 '22 12:10

achoukah