我无法将句子作为css中的段落对齐

时间:2016-04-19 00:58:33

标签: html css

这是我的代码:



p {
  background-color: yellow;
}
p.ex {}

<p class="ex">Security vulnerabilities that put customers at</p>
<p class="ex">risk have affected Asda's website for a couple</p>
<p class="ex">of years,a security expert has revealed</p>
&#13;
&#13;
&#13;

我想把所有句子放在一起形成一个段落。我做错了什么?

2 个答案:

答案 0 :(得分:2)

只需将所有文字放在一个<p>标记中即可。文本将根据需要自动换行,但您可以使用<br>标签手动插入换行符。

如果要禁用/修改自动文本换行,请参阅white-space属性。

&#13;
&#13;
p { background-color: yellow; }
&#13;
<p class="ex">
    Security vulnerabilities that put customers at <br>
    risk have affected Asda's website for a couple <br>
    of years, a security expert has revealed.
</p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果由于某种原因你无法将所有段落合并为一个,如另一个答案所示,你可以使用:last-of-type:not伪类来删除除最后一个之外的所有段落像这样的段落:

p:not(:last-of-type) {
  background-color: yellow;
  margin:0;
}
p:last-of-type {
  margin-top:0;
  background-color: yellow;
}
p.ex {}
<p class="ex">Security vulnerabilities that put customers at</p>
<p class="ex">risk have affected Asda's website for a couple</p>
<p class="ex">of years,a security expert has revealed</p>
<div>foo</div>