The difference between first-child and first-of-type in the css Selector,

Source: Internet
Author: User

The difference between first-child and first-of-type in the css Selector,

: The first-child selector is the selector defined in css2. It is also the first sub-element to be understood literally. For example, there is a code segment:

P: first-child matches the p element, because the p element is the first child element of the div;

H1: first-child does not match any element, because h1 is the second child element of div, not the first one;

Span: first-child does not match any element, because both span elements are not the first child element of div;

 

Then, the selector first-of-type is defined in css3. What is the difference between this selector and first-child? Let's look at the Code:

P: first-of-type matches the p element, because p is the first of all the child elements of the div whose type is p;

H1: first-of-type matches the h1 element, because h1 is the first of all the child elements of div whose type is h1;

Span: first-of-type matches the third child element span. Here, div has two child elements of span, matching the first of them.

 

Therefore, the above two examples can draw a conclusion:

: First-childMatching is the first child element of a parent element. It can be said that it is the first child element in the structure.

: First-of-typeMatch the first child element of the same type under a parent element. For example, p: first-of-type indicates the first child element of all types p. It is no longer the first child element, as long as it is the first element of this type.

The same type of selector: last-child and: last-of-type,: nth-child (n) and: nth-of-type (n) can also be understood in this way.

Related Article

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.