CSS tutorial 17. Specificity of CSS priority

Source: Internet
Author: User

If the same element has two or more conflicting CSS rules, the browser has some basic rules to decide which one is special and wins.
It may not be as important as others. In most cases, you do not need to worry about conflicts. However, large and complex CSS files or CSS files may contain conflicts.
If the selector is the same, the subsequent attributes will overwrite the previous attributes. For example:
P {color: red ;}
P {color: blue ;}
The element of the p element will be blue because it follows the following rules.
However, you cannot often use the same selector conflict to achieve the goal. When you use a nested selector, a reasonable conflict arises. For example:
Div p {color: red ;}
P {color: blue ;}
Maybe the color of the p element in the div element is blue, just like the rules of the p element, but the feature of the first selector is red. Basically, the more features a selector has, the style will be displayed when there is a style conflict.
The actual characteristics of a set of nested selectors can be calculated. Basically, the value of the ID selector is 100, the value of the class selector is 10, and the value of each html selector is 1. They can be used together to calculate the value of the feature.
P features 1 (an html selector)
Div p features 2 (two html selectors)
Tree features 10 (one class selector)
Div p. tree features 1 + 1 + 10 = 12 (two html selectors, one class selector)
# Baobab features 100 (1 ID selector)
Body # content. alternative p features 112 (two html selectors, one ID selector and one class selector)
According to the above rules, div p. tree features higher than div p, and body # content. alternative p is higher than both of them.

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.