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.

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

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.