(CSS): Last-child and: Last-of-type difference

Source: Internet
Author: User

Compare the description of the pseudo-class selector in W3cschool: Last-child and: Last-of-type, as follows:

: Last-child      p:last-child      selects each <p> element that belongs to the last child element of its parent element. : Last-of-type    p:last-of-type    selects each <p> element that belongs to the last <p> element of its parent element.

To tell the truth, in the two relations do not understand the time, the more you look at the two descriptions, the more awkward, around the mouth, difficult to understand, to read the information on the Internet to understand after, I think that said is quite reasonable.

Take a piece of code for example:

<! DOCTYPE html>{            color:  Red;        }         p:last-of-type{            color: blue;        }     </style>

The result of the run shows that no row is shown in red and the third line is blue. Perhaps you might think that the two selector is the same row, but when you comment out the blue selector you will find that the red is still not selected.

In fact, the reason why red is not selected is very simple, the previous article has been said, here is a simple description

: Last-child selects the last child node of the parent node and matches the selector, the last node of the parent Div is span, and the matching selector is P, which does not correspond so it does not match.

and a different selector.

: Last-of-type is looking for the last child node in the parent node that is the same as the selector, that is, this is not the last node, but the last P-element node, so only the third row can be found.

(CSS): Last-child and: Last-of-type difference

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.