CSS descendant selectors, sub-selectors and neighboring sibling selectors

Source: Internet
Author: User

Usually in the code practice, often use the descendant selector, sub-selectors will also be used, here to do a summary:

1, descendants selector and child selector differ:

① does not have the same wording:
Descendant selectors are identified as: spaces
such as: UL li{width:150px;} "between Ul and Li is separated by a space"
The child selector is identified as:>
such as: ul>li{width:150px;} "> separated between UL and Li"
The identity of the adjacent sibling selector is: +
such as: H1 + P {margin-top:50px;} [H1 and P separated by + ]

The ② function is not the same:
Descendant selectors (descendant selector), also known as containing selectors, can select elements of an element's descendants (descendant elements), as in the example above, the descendant selector is all the LI elements in all the elements surrounded by the selection of UL, including the son element, the grandson element, Great-grandchild elements and so on.
In contrast to descendant selectors, child selectors can only select elements that are elements of the son element of an element, and the sub-selector selects only the LI element in the son element that surrounds the UL, not including the grandchild element, the great-grandchild element, and so on.
The adjacent sibling selector is well understood, which is to select the element immediately after the other element, and both have the same parent element.

③ compatibility is not the same:
The descendant selector is compatible with mainstream browsers.
The child selector and the adjacent sibling selector in IE6 are unsupported selectors and will be out bug!
2, descendant selector, child selector and adjacent sibling selector combined with examples:

Take a look at the selector below:

HTML > Body table + ul {margin-top:20px;}

This selector is interpreted as: Select the first adjacent sibling UL element that appears immediately after the table element, which is contained within a BODY element, and the body element itself is a child of the HTML element.

CSS descendant selectors, sub-selectors and neighboring sibling selectors

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.