CSS Derivation selectors

Source: Internet
Author: User
Derived selectors you can make the markup more concise by defining the style based on the context in which the element is located. In CSS1, selectors that apply rules in this way are called context selectors (contextual selectors), because they rely on context relationships to

Derived selectors


You can make the markup more concise by defining the style based on the context of the element in its place.

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

A derived selector allows you to determine the style of a label based on the context of the document. By using derived selectors reasonably, we can make the HTML code neater.

For example, if you want the strong element in the list to become italic instead of the usual bold character, you can define a derived selector like this:

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


Note the contextual relationship of the blue code marked <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 italic. 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, without defining a particular class or ID for the strong element, and the code is more concise.

Then look at the following CSS rules:

Strong {
color:red;
}

H2 {
color:red;
}

H2 Strong {
Color:blue;
}

Here 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.