html - 如何在css中选择tspecific类的最后一个孩子
问题描述
需要将 CSS 应用于具有类名的 div ,test
但最后一个 div 具有 class test
。
在以下情况下 - 前 2 个 div.test 应该有边距,但第 3 个 div.test 不应该。
<div class="parent">
<div class="test">
<div>The first paragraph.</div>
</div>
<div class="test">The second paragraph.</div>
<div class="test">The third paragraph.</div>
<div class="test">The fourth paragraph.</div>
<div class="test-1">The firth paragraph</div>
</div>
div.test-1
不应该应用任何东西
解决方案
用于.test:nth-last-child(-n+2)
选择的最后一个孩子.test
.test:nth-last-child(-n+2){
color:red;
}
<div class="parent">
<div class="test">
<div>The first paragraph.</div>
</div>
<div class="test">The second paragraph.</div>
<div class="test">The third paragraph.</div>
<div class="test">The fourth paragraph.</div>
<div class="test-1">The fourth paragraph.</div>
</div>
推荐阅读
- php - 为什么我的一些 shell_exec 有时会因管道损坏而失败,但仍会产生必要的输出?
- php - 当我单击引导按钮时,我想调用 php 插入查询
- .net - 添加项目引用错误地添加了 dll 引用
- html - 使用 VBA 单击带有数据切换和数据目标的按钮
- sql - 如何将此python SQL语句重写为SQL语句
- php - 订单页面未显示数据 - Magento
- javascript - 有没有办法将画布图像用作类型等于数据而不是文件的输入的数据?
- python - 如何可视化单词模式?
- machine-learning - 如果有多个模型具有相同或接近的验证错误,如何选择模型
- javascript - 单击时如何在 Vue.js 中重定向到不同的 url?