html - CSS :first-child and :first-of-type (example)

标签 html css css-selectors pseudo-class

<分区>

我仍在使用实例学习 :first-child 和 :first-of-type 伪类之间的区别。

我从另一个网站复制了一些 HTML/CSS 代码并将其添加到:http://codepen.io/muygalan/pen/RroQNp .

问题: 当我从 CSS 文件中删除以下代码时:

#blog article:first-of-type {
background: green;
}

为什么文本不嵌套在 <article> 中标签变红?

不是...

#blog article:first-child {
color: red;
}

...如果先前的 :first-of-type 已从代码中删除,应该将文本颜色变为红色?

最佳答案

Here's a great explanation on CSS Tricks .基本上,#blog div 的第一个子元素不是 article 元素;它是一个 header 元素。 :first-child 将匹配父容器中的第一个元素。 :first-of-type 将匹配父容器中该类型的第一个元素,无论它前面是否有其他元素。

关于html - CSS :first-child and :first-of-type (example),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522436/

上一篇:jquery - 了解缩略图轮播中的显示元素

下一篇:javascript - Jquery animate div 在点击时增加高度并在第二次点击时折叠

相关文章:

html - 如何在不使用任何样式和类的情况下为 html 中的第二个 p 标记着色?

javascript - 将一个 div 标签对齐到另一个标签下方

html - 分机号 : How to write custom styles CSS/SCSS for Grid column header?

vertical-alignment - 在具有自动高度的div中垂直居中div

css - 为什么 Chrome 突然在超链接标签上出现背景图像问题?

html - 垂直和水平居中图标和文本

html - 想要在 Rails 中的每个页面上使用相同的横幅/导航菜单

jquery - 刷新时获取图片高度

html - CSS [attribute ="value"] 选择器是不必要的吗?

CSS 选择器在 2 个元素之后跳 1 个元素