CSS
As a CSS rule
- !important this has the highest priority, when a rule comes with this, it’s the boss.
- style on element here style is the HTML element attribute, like <p style=“margin:2px”/>
- ID, again ID is the id of a HTML element. like <div id=“css”/>, then you can use #css to define CSS rules for this div.
- CSS class, like .cssClass{...}, also including the pseudo, like .cssClass:after{...}, they have the same priority
- HTML element like p, div, br .etc
- * this is the universal selector, you can write CSS like this *{padding:1px}, then it applies to every HTML element
Sometimes, you may use a combination of several selectors, for example, #css .cssClass .button{...}, or #css2 .cssClass p {...}. at this time, the one has higher priority when it has more selectors of a higher priority. so in the above example, the first one win, as both of them have ID selector, but the first one has two class selectors, and the second has only one class selector.
Location Priority
What if both selectors have the same number and level of selectors? they’er further prioritized by location. here are the rules, from high priority to low:
- <style> in HTML <header>
- @import within <style>
- <link>
- @import within a CSS file attached by <link>
- CSS file attached by an end user? how? don’t understand :(
- default CSS supplied by a browser
No comments:
Post a Comment