css第一个单词加粗

发布于 2024-12-04 15:04:27 字数 168 浏览 4 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

魔法唧唧 2024-12-11 15:04:27

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.

夜清冷一曲。 2024-12-11 15:04:27

当前的 CSS 选择器不允许这样做。

所以我改变了方法,将第一行加粗。

HTML

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

CSS

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

The current CSS selectors do NOT 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;
}
寻找一个思念的角度 2024-12-11 15:04:27

使用 Span 类

因此,创建一个名为“bold”的类,并将该类中的第一个单词换行。

.bold {
font-weight:bold;
}

然后

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

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>
我只土不豪 2024-12-11 15:04:27

我已将单词放在 First 之间

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

回忆凄美了谁 2024-12-11 15:04:27

只需将第一个单词放在强标记中即可。

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

这是 JS Fiddle

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

怪我太投入 2024-12-11 15:04:27

如果可以选择 PHP:

$string = "I've got a wonderful feeling";
$arr = explode(' ',trim($string));
$first = $arr[0];
$all_but = str_replace ("$first ","",$string);

这将为您提供第一个单词以及字符串的其余部分。然后您可以单独设计每个样式。毫无疑问,$all_but 有一个更平滑的选项。

If PHP is an option:

$string = "I've got a wonderful feeling";
$arr = explode(' ',trim($string));
$first = $arr[0];
$all_but = str_replace ("$first ","",$string);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文