nth-child选择所有元素。

huangapple go评论68阅读模式
英文:

nth-child selects everything

问题

我试图理解:nth-child是如何工作的,但不理解为什么它选择了下面的所有内容:

:nth-child(2) {
    color: blue;
}

同样的情况也发生在我使用nth-child(1)时,但当我使用nth-child(3)时,它只选择了"two"和"hello"。

:nth-child(3) {
    color: blue;
}

可以有人为我详细解释一下它是如何工作的,以便我能理解这个概念吗?

英文:

I'm trying to understand how the :nth-child works but don't understand why it is selecting everything below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

    :nth-child(2) {
        color: blue;
    }

<!-- language: lang-html -->

&lt;div&gt;
    hello
    &lt;h1&gt;test&lt;/h1&gt;
    &lt;p&gt;one&lt;/p&gt;
    &lt;p&gt;two&lt;/p&gt;
    &lt;p&gt;three&lt;/p&gt;
    &lt;p&gt;four&lt;/p&gt;
&lt;/div&gt;

&lt;div&gt;
    &lt;span&gt;hey&lt;/span&gt;
&lt;/div&gt;

&lt;p&gt;hello&lt;/p&gt;

test

<!-- end snippet -->

Same happens when I do nth-child(1) but then nth-child(3) selects only "two" and "hello"

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

    :nth-child(3) {
        color: blue;
    }

<!-- language: lang-html -->

&lt;div&gt;
    hello
    &lt;h1&gt;test&lt;/h1&gt;
    &lt;p&gt;one&lt;/p&gt;
    &lt;p&gt;two&lt;/p&gt;
    &lt;p&gt;three&lt;/p&gt;
    &lt;p&gt;four&lt;/p&gt;
&lt;/div&gt;

&lt;div&gt;
    &lt;span&gt;hey&lt;/span&gt;
&lt;/div&gt;

&lt;p&gt;hello&lt;/p&gt;

test

<!-- end snippet -->

Can somebody give me a walkthrough on how it's working so I can understand the concept?

答案1

得分: 2

你忘了一些东西。nth-child 是一个选择器,用于选择你同一列表中的特定项。看看HTML,h1元素是其父元素div元素的第二个子元素,因此被选中,并且其颜色设置为蓝色。其他p元素也是其父元素div元素的第二个子元素,因此它们也被选中,其颜色设置为蓝色。

所以,如果你想要给**"hey"**上色,你需要升级代码为,

div:nth-child(2) {color: blue;}

如果你想要给**"two"**上色,你需要升级代码为,

p:nth-child(2) {color: blue;}

你没有做错任何事,只是忘了在HTML中选择元素。

希望这对你有所帮助。

英文:

you forgot something. nth-child is a selector is selecting one specific item in your same list.look HTML, the h1 element is the second child of its parent div element, so it is selected and its color is set to blue. The other p elements are also the second child of their parent div element, so they are also selected and their color is set to blue.

so if you need color the "hey" , you need to upgrade the code to ,

div:nth-child(2) {color :blue;}

you need color the "two", you need to upgrade the code to,

p:nth-child(2) {color :blue;}

you not done anything wrong. only you forgot select element in html.

I hope this help you.

答案2

得分: 1

你可以检查一个元素以查找它从哪里获取其样式。在Firefox中,右键单击一个元素,然后单击Inspect以打开开发者工具(Chrome有一个非常类似的过程):

nth-child选择所有元素。

我们在这里看到&lt;h1&gt;&lt;body&gt;继承了颜色属性,这是因为它是&lt;html&gt;的第二个子元素。同样,使用:nth-child(1),将匹配&lt;html&gt;,并且所有元素都将继承自那里的颜色。最好只在具体的父元素/祖先上使用:nth-child(),这样您就不会出现意外的继承关系。

英文:

You can inspect an element to find out where it's getting its styles from. In Firefox, right-click an element and click Inspect to bring up the Developer Tools (there is a very similar procedure for Chrome):

nth-child选择所有元素。

We see here that &lt;h1&gt; is inheriting the color property from the &lt;body&gt;, which is selected because it's the 2nd child of &lt;html&gt;. Likewise, with :nth-child(1), &lt;html&gt; is matched and everything will inherit color from that. It's smart to use :nth-child() only with a combinator targeting a specific parent/ancestor so you don't have unexpected inheritances like that.

huangapple
  • 本文由 发表于 2023年1月9日 09:33:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定