CSS Design Guide Context Selector

Source: Internet
Author: User

Suppose you have the following scenario: you want to separate the paragraph text in article and aside by setting a different font size. Problems like this "location-based" transform label style can be resolved with a contextual selector.

1. Normal context Selectors

Format: Label 1 label 2 {declaration}

Label 2 is the target we want to select, and is selected only if label 1 is its ancestor (not necessarily the parent element).

Article p{color:red;} The context selector In this example indicates that only the P element of the article descendant will apply the following style

2. Sub-selectors >

Format: Label 1 > Label 2 {declaration}

Label 2 is the target we want to select, and is selected only if the label 1 is its parent element.

3. Close to sibling selector +

Format: Label 1 + label 2 {declaration}

Label 2 is the target we want to choose, and label 2 must be immediately behind its fellow label 1.

4. General Sibling Selector ~

Format: Label 1 + label 2 {declaration}

Label 2 is the target we want to choose, and the label 2 must (not necessarily immediately followed) at the back of its fellow label 1

5. Universal Selector *

* is a wildcard character that matches any element.

*{color:red;} will cause all elements to turn green

P *{color:red will turn the text of all elements contained in P into a knockout, but the text of P itself will not change color.

This selector has a very interesting use, that is, to make a non-sub-selector, such as section * a{Declaration}, any part of the grandson element, not the child element of a tag will be selected.

The above is the knowledge of context selectors I know, but in the learning of sub-selectors encountered a problem, trouble to understand the children's shoes to help answer,

Supposedly, the H2 element in the P element should not turn red, but how does it change color?

Context selectors for the CSS design guide

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.