使用 css 设置最后一段中第一行的样式

发布于 2024-12-08 00:08:52 字数 843 浏览 0 评论 0原文

如何使用 css 将最后一段中的第一句话加粗?我还想在最后一段的顶部添加额外的填充,以便它环绕图像。如果我增加浮动图像底部的填充,该段落不会换行到图像的底部齐平左边缘,它只是沿着图像的右侧流动 - 这不是我想要的。我将一个类绑定到“p”元素,因为我只想影响某个段落(并且我没有看到“:last-child”伪元素,也不知道是否可以在一个元素或类)。

#wf_mainContent_left p.last_wfp:first-line {
padding-top:20px;
font-weight:bold;
color:red;
}

HTML

<p class="last_wfp">This is the first sentence that I want bold and red. This is the second
 sentence that I don't want that treatment.</p>

当我只想要第一个句子时,这会将两个句子加粗,即使它们位于同一行。这应该可以根据本教程完成。并且对填充没有影响。我尝试添加
标签,但它增加了很多空间。我尝试设计
标签的样式,但根据 我刚刚读过的这篇文章。无论如何,我只想第一行加粗,而不是两个句子。

How do i make the first sentence in the last paragraph bold using css? I also want to add extra padding to the top of this last paragraph so that it will wrap around an image. If I increase the padding on the bottom of the floated image, the paragraph does not wrap to the bottom flush left margin of the image, it just flows along the right of it -- not what I want. I tied a class to the "p" element since I only want to effect the a certain paragraph (and I don't see a ":last-child" pseudo-element nor do I know if you can use two pseudo-elements on one element or class).

#wf_mainContent_left p.last_wfp:first-line {
padding-top:20px;
font-weight:bold;
color:red;
}

HTML

<p class="last_wfp">This is the first sentence that I want bold and red. This is the second
 sentence that I don't want that treatment.</p>

This bolds both sentences when I only want the first sentence, even though they are on the same line. This should be able to be done according to this tutorial. And there is no effect on the padding. I've tried adding a <br> tag but it adds to much space. And I tried styling the <br> tag but that's not possible according to this post that I just read. At any rate, I just want the first line to be bold, not both sentences.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

月隐月明月朦胧 2024-12-15 00:08:52

(我没有看到“:last-child”伪元素,也不知道是否可以在一个元素或类上使用两个伪元素)

:last-child 是 CSS3 伪元素-班级。它不是伪元素,因此您始终可以使用它将 :first-line 附加到选择器。

如果您不担心浏览器兼容性,则可以轻松使用:

#wf_mainContent_left p:last-child:first-line {
    padding-top:20px;
    font-weight:bold;
    color:red;
}

如果您需要兼容性,请坚持使用现有的 CSS 类解决方案。

至于 br 产生过多的空白,看看是否可以减小 pline-height

(and I don't see a ":last-child" pseudo-element nor do I know if you can use two pseudo-elements on one element or class)

:last-child is a CSS3 pseudo-class. It is not a pseudo-element, so you can always attach :first-line to a selector with it.

If you're not worried about browser compatibility, you can easily use this:

#wf_mainContent_left p:last-child:first-line {
    padding-top:20px;
    font-weight:bold;
    color:red;
}

If you need compatibility, stick with your existing CSS class solution.

As for the br producing too much whitespace, see if you can decrease the line-height of your p.

半步萧音过轻尘 2024-12-15 00:08:52

@BoltClock 有 CSS 答案。

如果您正在寻求跨浏览器兼容性并且愿意使用js,您可以执行以下操作:

CSS

.last_bold{
    font-weight:bold;
    color:red;
    padding-top:2em;
    display:inline-block;
}

var a = $('.last_wfp').text();
var b = a.split('. ');

$('.last_wfp').html('<span class="last_bold">' + b[0] + '</span>' + '. ' + b[1]);

EDIT

Per mr.nicksta's观察:

寻找一个周期的第一个索引可能更有效
然后从开始到该位置创建一个子字符串?还有这个
方法假设每个段落元素只有两个句子,任何进一步的
句子丢失。

这是修改后的代码,应该处理任何句子。

var a = $('.last_wfp').text();
var b = a.slice(0, a.indexOf('. '));
var c = a.slice(a.indexOf('. '), a.length);

$('.last_wfp').html('<span class="last_bold">' + b + '</span>' + c);

修改后的示例: http://jsfiddle.net/jasongennaro/3ZKP9/5/

@BoltClock has the CSS answers.

If you are looking for cross-browser compatibility and you are willing to use js, you can do the following:

CSS

.last_bold{
    font-weight:bold;
    color:red;
    padding-top:2em;
    display:inline-block;
}

var a = $('.last_wfp').text();
var b = a.split('. ');

$('.last_wfp').html('<span class="last_bold">' + b[0] + '</span>' + '. ' + b[1]);

EDIT

Per mr.nicksta's observation:

potentially more efficient to look for first index of a period and
then create a substring from the start to that position? and this
approach assumes only two sentences per paragraph element, any further
sentences are lost.

Here is the revised code, that should handle any # of sentences.

var a = $('.last_wfp').text();
var b = a.slice(0, a.indexOf('. '));
var c = a.slice(a.indexOf('. '), a.length);

$('.last_wfp').html('<span class="last_bold">' + b + '</span>' + c);

Revised example: http://jsfiddle.net/jasongennaro/3ZKP9/5/

陈甜 2024-12-15 00:08:52

第一行伪元素表示文本的第一个格式化行。它应该位于块级元素、内联块、表格标题或表格单元格中。

它不处理您的符号(点回车...),仅基于格式和块大小。

请注意浏览器的兼容性。您会在那里找到一张好图片: http://reference.sitepoint.com/css/pseudoelement-firstline

请参阅问题其他部分的其他答案我不会复制粘贴

The first-line pseudo element represents the first formatted line of your text. It should be in a block-level element, an inline block, a table caption, or a table cell.

It doesn t take care of your sign (dot carriage return...), only based on formatting and block size.

Be careful at browser's compatibility. You ll find a good picture there : http://reference.sitepoint.com/css/pseudoelement-firstline

See other answer for the other part of your question I ll not do a copy paste

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