html - 如何为某种元素的第一个 child 设计样式?

标签 html css css-selectors

例如,我有这样的 HTML:

<div class='page'>
    <span>2</span>
    <span>2</span>
    <a>1</a>
    <a>6</a>
</div>

我如何为第一个 child 设计样式

我是这样使用的:.page a:first-child {color:red}

但它没有运行。

最佳答案

使用 first-of-type 而不是 first-child

.page a:first-of-type{
    color:red;
}

The :first-of-type CSS pseudo-class represents the first sibling of its type in the list of children of its parent element.

取自MDN Documentation .您可以找到更多详细信息和示例 here.

解释::first-child not working as expected

关于html - 如何为某种元素的第一个 child 设计样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20802061/

相关文章:

java - 使用 Jsoup 替换标签内的文本

javascript - 如何验证 Grafana 图表并将其嵌入 iframe?

javascript - 附加元素的 JQuery 悬停状态

html - float 在影响兄弟元素高度的元素上,Wy?

css - 是否可以通过 css 选择器通过多个伪类来定位元素?

javascript - jstree检查父节点

HTML 书签 + CSS 溢出 :Hidden - disappearing content?

css - 如何避免在 CSS 中重新定义父元素类

html - 使用css选择器还是更直接的id?

css - 通用选择器,如何排除某些元素