使用 css 设置最后一段中第一行的样式
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
:last-child
是 CSS3 伪元素-班级。它不是伪元素,因此您始终可以使用它将:first-line
附加到选择器。如果您不担心浏览器兼容性,则可以轻松使用:
如果您需要兼容性,请坚持使用现有的 CSS 类解决方案。
至于
br
产生过多的空白,看看是否可以减小p
的line-height
。: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:
If you need compatibility, stick with your existing CSS class solution.
As for the
br
producing too much whitespace, see if you can decrease theline-height
of yourp
.@BoltClock 有 CSS 答案。
如果您正在寻求跨浏览器兼容性并且愿意使用js,您可以执行以下操作:
CSS
EDIT
Per mr.nicksta's观察:
这是修改后的代码,应该处理任何句子。
修改后的示例: 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
EDIT
Per mr.nicksta's observation:
Here is the revised code, that should handle any # of sentences.
Revised example: http://jsfiddle.net/jasongennaro/3ZKP9/5/
第一行伪元素表示文本的第一个格式化行。它应该位于块级元素、内联块、表格标题或表格单元格中。
它不处理您的符号(点回车...),仅基于格式和块大小。
请注意浏览器的兼容性。您会在那里找到一张好图片: 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