你能帮助解决关于第一个孩子和后选择器的问题吗?



我需要在每个部分的第一个子项之后显示单词first

section:first-child::after {
content: "first";
}

因为您没有设置元素的父级。CSS 中的所有元素都继承自body。 在您的示例中,section处理方式为:

body section: first-child

如果section元素之前没有任何内容,并且它是body的第一个子元素,那么它将具有指定的属性。 否则属性将被忽略。

您应该将section包装到容器中。例如:

<div id="parent">
<section>...</section>
<section>...</section>
</div>

和 CSS:

.parent section:first-child::after {
content: "first";
}

最新更新