CSS Neighbor Sibling Selector

Source: Internet
Author: User

The adjacent sibling selector (adjacent sibling selector) selects the element immediately following the other element, and both have the same parent element.

Select adjacent Brothers

If you need to select an element immediately after another element, and both have the same parent element, you can use the adjacent sibling selector (adjacent sibling selector).

For example, if you want to increase the top margin of a paragraph that appears immediately after the H1 element, you can write:

H1 + P {margin-top:50px;}

This selector reads: "Select the paragraph that appears immediately after the H1 element, and the H1 and P elements have a common parent element."

Try it yourself.

Grammatical explanations

The adjacent sibling selector uses the plus sign (+), which is the adjacent sibling (adjacent sibling combinator).

Note: As with a sub-binding, there can be white space next to the adjacent sibling binding character.

Take a look at the following document tree fragment:

<div>  <ul>    <li>list Item 1</li>    <li>list item 2</li>    <li >list Item 3</li>  </ul>  <ol>    <li>list item 1</li>    <li>list Item 2</li>    <li>list item 3</li>  </ol></div>

In the above fragment, the DIV element contains two lists: An unordered list, an ordered list, each containing three list items. These two lists are adjacent brothers, and the list item itself is also an adjacent sibling. However, the list items in the first list and the list items in the second list are not adjacent siblings because the two sets of list items do not belong to the same parent element (up to the cousin).

Keep in mind that you can only select the second element in two adjacent siblings with a single binding character. Please see the selector below:

Li + li {font-weight:bold;}

The above selector only changes the second and third list items in the list to bold. The first list item is not affected.

Try it yourself.

Combined with other selectors

Adjacent sibling combinations can also be combined with other binding characters:

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

This selector is interpreted as: Select all sibling ul elements that appear 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 Neighbor Sibling Selector

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.