gpt4 book ai didi

html - 有没有办法指定第一个元素*类型*?

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:18 24 4
gpt4 key购买 nike

我想知道有什么方法可以让下面的 CSS 示例按照我的预期运行吗?

<!DOCTYPE html> 
<html>
<head>
<style type="text/css">
.col p:first-child {
background:yellow;
}
</style>
</head>

<body>
<div class="col">
<h1>Woot</h1>
<p>I am a super nun. I am a super nun.</p>
<p>I am a super nun. I am a super nun.</p>
</div>
</body>
</html>

基本上我需要第一个<p>以某种方式设置样式的元素...但是first-child如果它是第二个 child (duh),则不起作用,即使我试图说:“父元素中的第一个 <p> 元素(即 .col p:first-child )”。

是否可以在 CSS 中选择特定类型的第一个 child 而不是第一个 child ?

最佳答案

幸运的是,对于元素 types,CSS3 提供了 :first-of-type 伪类(没有 first of class,first of 伪类,first具有特定属性等)。

.col > p:first-of-type

不幸的是,由于它是一个 CSS3 选择器,浏览器对它的支持很差。所有IE版本中,只有IE9支持。

如果 .col 的结构是可预测的(足够),例如,它总是包含一个元素后跟 p,您可以这样做相反,仅使用 CSS2 选择器:

.col > :first-child + p

关于html - 有没有办法指定第一个元素*类型*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6142613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com