CSS derivation Selector

Source: Internet
Author: User
Tags define

Core tip: by defining a style based on the context in which the element is positioned, you can make the tag more concise.

Derivation Selector


By defining a style based on the context in which the element is positioned, you can make the tag more concise.

In CSS1, selectors that apply rules in this way are called contextual selectors (contextual selectors) because they rely on context to apply or avoid a rule. In CSS2, they are called derivation selectors, but they work the same regardless of what you call them.

The derivation selector allows you to determine the style of a label based on the contextual relationship of the document. By using the derivation selector rationally, we can make the HTML code cleaner.

For example, if you want the strong element in the list to become italic rather than the usual bold word, you can define a derivation selector:

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


Note the context of the blue code labeled <strong>:

<p><strong> I'm bold, not italic, because I'm not in the list, so this rule doesn't work for me </strong></p>

<ol>
<li><strong> I am a italic character. This is because the strong element is inside the LI element. </strong></li>
<li> I am the normal font. </li>
</ol>


In the example above, only the strong element in the LI element is styled in italics, and the code is more concise without having to define a particular class or ID for the strong element.

Then look at the following CSS rules:

Strong {
color:red;
}

H2 {
color:red;
}

H2 Strong {
Color:blue;
}

The following is the HTML that it exerts influence on:

<p>the strongly emphasized word in this paragraph is<strong>red</strong>.</p>



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.