CSS derived Selector

Source: Internet
Author: User

Derived Selector


You can define a style based on the context of the element in its position to make the tag more concise.

In CSS1, the selectors that apply rules are called contextual selectors because they depend on context relationships to apply or avoid a rule. In CSS2, they are called derivative selectors, but whatever you call them, they all play the same role.

The derived selector allows you to determine the style of a tag Based on the context of the document. By properly using the derived selector, we can make HTML code more clean.

For example, if you want the strong element in the list to become italic, rather than the regular italic, you can define a derived selector as follows:

Li strong {
Font-style: italic;
Font-weight: normal;
}


Note the context of the Blue Code marked as <strong>:

<P> <strong> I am in bold or not, because I am not in the list, this rule does not work for me </strong> </p>

<Ol>
<Li> <strong> I am a italic. This is because the strong element is located in the li element. </Strong> </li>
<Li> I am a normal font. </Li>
</Ol>


In the above example, only the style of the strong element in the li element is italic, and no special class or id needs to be defined for the strong element. The code is more concise.

Let's look at the following CSS rules:

Strong {
Color: red;
}

H2 {
Color: red;
}

H2 strong {
Color: blue;
}

The following is the HTML that it influences:

<P> The stronugly emphasized word in this paragraph is <strong> red </strong>. </p>
<H2> This subhead is also red. <H2> The stronugly emphasized word in this subhead is <strong> blue </strong>.


Related Article

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.