css bold first word

2019-01-15 13:01发布

问题:

How can I bold the first word of sentence using CSS selectors and is this a good/bad way to do this with respects to browser comparability?

code:

  <h2>this is a sentence</h2>

thx

回答1:

There is no ::first-word pseudo-element in CSS; you'll have to wrap the first word in an extra element and then select that.



回答2:

Use Span class

So creat a class called bold and wrap first word in the class.

.bold {
font-weight:bold;
}

Then

<h2><span class="bold">the</span> brown fox</h2>


回答3:

I have put the word between <b>First</b>



回答4:

just put the first word in a strong tag.

<h1><strong>This</strong> is how you do it simply.</h1>

Here is the JS Fiddle



回答5:

The current CSS selectors do now allow this.

So I change my approach to bold the first line.

HTML

<h2>this<br> is a sentence</h2>

CSS

h2::first-line{
   font-weight: bold;
}