CSS selector: The difference between First-child and: First-of-type

Source: Internet
Author: User

: The First-child selector is a selector defined in Css2, and is understood literally as a first child element. For example, there is a segment code:

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

H1:first-child matches no element, because here H1 is the second child element of the Div, not the first one;

Span:first-child does not match any elements, as there are two span elements that are not the first child elements of a div;

Then, in the CSS3 also defined: First-of-type this selector, this with: First-child What is the difference? Or look at that piece of code:

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

H1:first-of-type matches to the H1 element, because H1 is the first of all the subtypes of the div that are H1 child elements;

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

Therefore, through the above two examples can draw a conclusion:

: First-child matches the first child element of a parent element, which can be said to be the first child of a structure.

: First-of-type matches the first of the child elements of the same type under a parent element, such as P:first-of-type, which refers to the first of all child elements of type P. There is no longer a limit to the first child element, as long as it is the first of the elements of that type.

The same types of selectors: Last-child and: Last-of-type,: Nth-child (N), and: Nth-of-type (n) can also be understood.

Description: This article turns from http://www.cnblogs.com/2050/p/3569509.html, thanks to Yumbo Master's experience!

CSS selector: The difference between First-child and: First-of-type

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.