css - 如何仅使用 CSS 将 content:"NEW" 添加到第 2 和第 3 段?
问题描述
- 帮助我将 content="NEW" 与 css 一起使用,但仅限于第 2 和第 3 段。
- 我不能更改 html 代码。
- 如果可能,请使用伪类。
<div id="first-div">
<h2>Subheading</h2>
<p>Paragraph 2</p> <!-- i need to add content here-->
<p>Paragraph 3</p> <!--and here -->
<div id="second-div">
<h3>Sub subheading</h3>
<p>Paragraph 4</p>
<p>Paragraph 5</p>
<p>Paragraph 6</p>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
</div>
</div>
<p>Paragraph 9</p>
<p>Paragraph 10</p>
解决方案
您要做的是首先#first-div
使用直接后代运算符 ( ) 查看并深入了解其子项>
。
如果您需要 2 个特定元素,则可以将直接子运算符与nth-child
函数一起使用。
#first-div > p:nth-child(2):after,
#first-div > p:nth-child(3):after {
color: red;
content: "NEW";
}
如果您需要p
下的所有标签#first-div
,只需选择所有标签
#first-div > p:after {
color: red;
content: "NEW";
}
推荐阅读
- java - java.lang.NoSuchMethodError:org.springframework.util.ReflectionUtils.accessibleConstructor
- docker - HaProxy 转发代理在 HTTP 上工作,但在 HTTPS 上给出 503
- android-studio - 如何在 Flutter 中仅为 OutlineButton 制作带下划线的边框
- laravel - 我如何修复 Illuminate\Database\Eloquent\Collection 类的错误对象无法转换为 int
- python - 带有 Flask 的 livereload.Server 在 Windows 中导致 NotImplementedError
- c# - WPF - MVVM - 如何更新propertychange的通知组合框
- python - 预计数据框列表只有一个数据框
- node.js - 如何管理私人频道中的邀请链接?
- android - 在动画仍在淡入/淡出时单击底部导航后崩溃
- java - IntelliJ IDEA 虚拟机选项