html - 为最后一个孩子的最后一个孩子添加样式
问题描述
这是我的代码的图像。我有一个footer-container
里面有几个footer-content
类,每个类都包含一个<p>
标签,然后是一个<span>
标签。我想对span
最后一个样式应用footer-content
我试过这个
.footer-container:last-child :last-child {
display: none;
}
但这隐藏了所有跨度标签
解决方案
编辑:要在您的条目之间创建这些破折号,而不是完全创建这些破折号span.footer-dash
,您可以仅使用 CSS 来做到这一点:
.footer-content:not(:last-child) .footer-item::after {
content: "-";
color: #666;
padding: 0 20px;
}
根据需要应用样式。选择器确保没有在最后一个元素之后添加破折号,因此如果一开始不存在任何内容,则无需隐藏任何内容。
:last-child
问我是我父母的最后一个孩子吗?,不是我的最后一个子元素是谁?(您的选择器建议您认为)。
所以要么使用后代选择器(空格):
.footer-container :last-child :last-child {
display: none;
}
或在正确的子元素上使用它:
.footer-content:last-child :last-child {
display: none;
}
请注意,:last-child
应该谨慎使用 of,因为它将您的内容与 DOM 结构紧密联系在一起(您可能希望稍后更改)。
我建议你像这样改变它:
.footer-content:last-child .footer-dash {
display: none;
}
:last-child
CSS 伪类表示一组兄弟元素中的最后一个元素。
/* Selects any <p> that is the last element
among its siblings */
p:last-child {
color: lime;
}
https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child
推荐阅读
- java - 意外覆盖:以下声明在特定类层次结构上具有相同的 JVM 签名错误
- javascript - 是否有可能,遍历数组并分配`` 到数组中的每个元素,并将其分配给一个变量?
- kubernetes - Kubernetes:验证数据时出错:[ValidationError(Deployment.spec):io.k8s.api.apps.v1.DeploymentSpec 中的未知字段“容器”
- csv - Ansible:将csv数据转换为目录
- c# - 如何在接口中使用枚举?
- sql - 由于 SSIS 中的多个 JOIN 导致超时
- r - 使用 felm 进行固定效应回归
- python - 如何在 Euros + pandas Python 中过滤样式格式的数据
- javascript - 输入数字后如何自动将焦点移动到下一个字段?
- mysql - 如何使用 C# 和任务计划程序修复无法连接到任何指定的 MySQL 主机