html - 块内类的第一个实例的 CSS 选择器
问题描述
我正在尝试定位块中类的第一个实例。
.ContactPage-details div > .ContactPage-title ~ .ContactPage-title:not(:first-child) {
background: none;
}
.ContactPage-details div > .ContactPage-title {
background: red;
color: white;
}
<div class="ContactPage-details">
<div class="ContactPage-telephone">
<h3 class="ContactPage-title">The First paragraph.</h3>
</div>
<div class="ContactPage-emailAddress">
<h3 class="ContactPage-title">The Second paragraph.</h3>
</div>
<div class="ContactPage-address">
<h3 class="ContactPage-title">The Third paragraph.</h3>
</div>
</div>
我正在尝试瞄准<h3 class="ContactPage-title">The First paragraph.</h3>
第一个 h3 父级可以更改,它可能不一定总是.ContactPage-telephone
如此,所以我不能直接定位它,因为它会有所不同,具体取决于它们是否在数据库中列出了电话号码。
这是我当前尝试针对的第一个实例的 cssContactPage-title
此刻,所有h3
的都有一个红色的背景和白色的颜色。
ContactPage-title
有没有一种方法可以在不使用 javascript的情况下定位第一个实例?
解决方案
要回答您对问题的描述:
不,CSS 有:first-child
,:first-of-type
但没有:first-of-class
。
但是,您的示例代码看起来像是您实际上想要定位.ContactPage-title
块中第一个孩子的孩子。
那将是:
.ContactPage-details > :first-child > .ContactPage-title {}
推荐阅读
- html - Is it possible for front-end to know the local supported font-family?
- python - URL, GET, POST parameters in Django
- android - 片段在 tabLayout 中出现多次
- radare2 - How to go to a highlighted function in radare2?
- java - 如何克隆 TestNG 的 TestResult
- spring-boot - delete have body in Angular 8 and Spring Boot
- python - 相交两个列表并计算每个元素重叠的次数
- ios - AppIcon 未显示在设置屏幕中(在 Apple ID 登录页面中)
- android - 如果我重新创建通知,我应该调用 startforeground(id, notification) 吗?安卓以上奥利奥,api28
- java - 钢琴应用程序中如何使用多线程?