html - 如何为第三个 div 类 span8 的最后一个 <p> 元素应用 CSS 更改

标签 html css

我需要为最后一个 <p> 应用 CSS 更改第三元素<div class="span8">不使用 jQuery。

HTML

<div class="span8">
    <p>Some text.</p>
    <p>This collection includes video, audio and essays.</p>
    <p class="last">Visit now</p>
    <div>Something else</div>
</div><br/>

<div class="span8">
    <p>Some text.</p>
    <p>This collection includes video, audio and essays.</p>
    <p class="last">Visit now</p>
    <div>Something else</div>
</div><br/>

<div class="span8">
    <p>Some text.</p>
    <p>This collection includes video, audio and essays.</p>
    <p class="last">Visit now</p>
    <div>Something else</div>
</div><br/>

CSS

div.span8>p:nth-of-type(2n+1) {
    font-style: bold;  
}

输出

一些文本。 该合集包括视频、音频和论文。 立即访问 别的东西

一些文本。 该合集包括视频、音频和论文。 立即访问 别的东西

一些文本。 This collection includes video, audio and essays. 立即访问(这一项需要加粗。) 别的东西

最佳答案

您可以通过向第 3 个 <div class="span8"> 添加一个类来完成此操作像这样:

<div class="span8">
    ...
</div>

<div class="span8">
    ...
</div>

<div class="span8 boldClass">
    ...
</div>

你的 CSS 是这样的:

.boldClass > p:nth-child(3) {
    border:2px solid #ff0000;
    padding:10px;
    font-weight: 600;
}

Jsfiddle Example here

==============更新================

或者,在执行此操作的第二种方法中,您可以像这样创建同样的东西:

.span8:nth-child(3) > p:nth-child(3) {
    border:2px solid #ff0000;
    padding:10px;
    font-weight: 600;
}

但还需要删除所有的<br />从你的代码。我不知道为什么,但是这些中断在使用 .span8:nth-child(3) > p:nth-child(3) 时破坏了代码(明白了吗?!:P)

虽然没有使用 <br />你可以在 .span8 中添加一个 margin-bottom然后在最后一个 div 上删除它

.span8{
    margin-bottom:15px;
}
.span8:last-child {
    margin-bottom:0;
}

这是一个 example

关于html - 如何为第三个 div 类 span8 的最后一个 <p> 元素应用 CSS 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29333946/

相关文章:

html - 如果用户点击关闭按钮,总是隐藏 div

javascript - 使用图像创建精美的 HTML 按钮

javascript - 将 HTML 模板打印为 PDF - Angular 2

html - 如何使用有效的 XHTML/CSS 制作 2 列列表?

javascript - 按下回车键时,返回 false 不会阻止页面重新加载

Javascript replace() 每次只发生一次

javascript - 在另一个文件中添加JS src

html - 即使在浏览器调整大小时也制作粘性页脚

css - 我的网页中没有底部填充

html - 居中 UL 但保持文本左对齐