: First-child and: last-child learning Notes

Source: Internet
Author: User

I was curious about a small problem I found when I was tossing the topic.

First, we will introduce the first-child pseudo class, that is, selecting the first object.

For example:

<Div class = "loop"> Test </div>
<Div class = "loop"> Test </div>
<Div class = "loop"> Test </div>
In css, you can use. loop: first-child to define a style for the first layer. Similarly, you can use. loop: last-child to define the last layer separately.
Problems
If you add a line below:

<Div class = "loop"> Test </div>
<Div class = "loop"> Test </div>
<Div class = "loop"> Test </div>
<Div> I don't have any styles to confuse </div>
The. loop: last-child will expire and will not act on any div. Similarly, if a div is added to the front,. loop: first-child will also become invalid.

Solution
Use a div to wrap all. loop, namely:

<Div>
<Div class = "loop"> Test </div>
<Div class = "loop"> Test </div>
<Div class = "loop"> Test </div>
</Div>
<Div> I don't have any styles to confuse </div>
So. loop: last-child is effective, and. loop: first-child.

Supplement
Depending on the structure, in some cases, the last-child failure may occur only. In short, as long as there is a problem between the two, it is very likely that the layer using this style is not fully wrapped.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.