html - 对第一个孩子和后代选择器感到困惑
问题描述
不想问,但在这个问题上对第一个孩子感到非常困惑。
<div class="container">
<div class="row">
<div class="col-4">
<a href="#" class="btn">Primary Button |<span> red text here</span></a>
</div>
<div class="col-4">
<a href="#" class="btn">Secondary button |<span> yellow text here</span></a>
</div>
<div class="col-4">
<a href="#" class="btn">Danger Button |<span> green text here</span></a>
</div>
</div>
</div>
我拥有的 CSS 是...
.row span:first-child {
color: red;
}
.row span {
color: yellow;
}
.row span:last-child {
color: green;
}
<div class="container">
<div class="row">
<div class="col-4">
<a href="#" class="btn">Primary Button |<span> red text here</span></a>
</div>
<div class="col-4">
<a href="#" class="btn">Secondary button |<span> yellow text here</span></a>
</div>
<div class="col-4">
<a href="#" class="btn">Danger Button |<span> green text here</span></a>
</div>
</div>
</div>
某些原因所有文本颜色都变成“绿色”,因为它应用了所有颜色变化,甚至是红色和黄色,因为它认为它们是跨度的第一个和最后一个孩子。我将如何做到这一点,以便跨度为每个跨度设置正确的文本?
解决方案
因为它认为他们是跨度的第一个也是最后一个孩子。
不正确。
这是因为span
是其父元素(即)的:first-child
and 。:last-child
a
如果你想定位span
它的后代,div
它是:first-child
它的父级(即 的后代.row
)......那么你必须编写一个匹配的选择器div
。
.row div:first-child span {
推荐阅读
- node.js - NodeJs:Sequelize(版本 - 5+)N:M 关联不起作用
- php - 如何为 arangodb-php 中的流式事务增加“maxTransactionSize”
- python - Python:在对数刻度的两条曲线之间拟合圆,以连接圆的原点
- vue.js - VueJS PWA's - 通过 GenerateSW 启用 Workbox “调试”
- angular - 更新时共享变量值未“共享”
- azure-devops - 如何从动态发布中排除部署组目标
- c# - 在 MVC 数据列表中,在 SQL 查询中切换条件的最佳方法是什么
- java - 将 Iterable 与 List 进行比较会引发 NullPointerException
- blazor - Blazor,没有 ID 为“x”的跟踪对象。也许 DotNetObjectReference 实例已经被释放
- python - 在 python 环境中运行 Tesseract 或替代 OCR 模块,无需下载 .exe 文件