css第一个单词加粗
如何使用 CSS 选择器将句子的第一个单词加粗?就浏览器可比性而言,这是一个好还是坏的方法?
代码:
<h2>this is a sentence</h2>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
CSS 中没有
::first-word
伪元素;您必须将第一个单词包装在额外的元素中,然后选择它。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.当前的 CSS 选择器不允许这样做。
所以我改变了方法,将第一行加粗。
HTML
CSS
The current CSS selectors do NOT allow this.
So I change my approach to bold the first line.
HTML
CSS
使用 Span 类
因此,创建一个名为“bold”的类,并将该类中的第一个单词换行。
然后
Use Span class
So creat a class called bold and wrap first word in the class.
Then
我已将单词放在
First
之间I have put the word between
<b>First</b>
只需将第一个单词放在强标记中即可。
这是 JS Fiddle
just put the first word in a strong tag.
Here is the JS Fiddle
如果可以选择 PHP:
这将为您提供第一个单词以及字符串的其余部分。然后您可以单独设计每个样式。毫无疑问,$all_but 有一个更平滑的选项。
If PHP is an option:
This will give you both the first word plus the balance of the string. Then you can style each separately. Undoubtedly there is a smoother option for the $all_but.