Relative weight of elements, classes and ids
From the Frontend Masters course CSS3 In-Depth by Estelle Weyl
When you are styling using css, the "cascade" is:
- 2) Closer to the element overrides farther away from the element (inline style overrides <style> in the <head>, which overrides styles.css style sheet)
- 1) Lower on the page overrides higher on the page
- 3) Stronger specificity overrides weaker specificity (a higher total score overrides a weaker score)
in this chart, the farther down the chart you go, the more powerful the selector.
You can select an element using a long string of element selectors, but at some point it's simpler to create an id or class.
(Unless you are styling a WordPress theme and can't.)
In Dreamweaver, you can select a piece of type, go to the CSS Designer, add a selector, and the selector created will be the most exact selector possible (It usually includes a very long string of elements.) You can use the up and down arrows on your keyboard to control how many of those element designations are; it's generally better to have fewer so they apply to more elements.
Least Powerful Selectors
*Universal selector: 0-0-0 |
|||
Elements |
|||
divOne element: 0-0-1 |
li > ul2 elements: 0-0-2 |
body div ul li p a6 elements: 0-0-6 |
|
Classes |
|||
.myclass1 class: 0-1-0 |
*.myclass1 class |
*[type=checkbox]1 attribute selector |
a:hover1 pseudo-class |
li.myclass1 class |
li[attr]One attribute |
li:nth-of-type(3n) > li1 pseudo-class |
form input[type=email]1 attribute |
li.myclass:nth-of-type(3n)
1 class / 1 pseudo-class |
input[type]:not(.myclass)1 attribute / 1 pseudo-class |
.myclass:nth-child(odd) .chk[type] …10 classes/attributes/ | |
IDs |
|||
#myid1 id selector 1-0-0 |
#myid li.myclass a[href]1 id selector |
#mypage #myid a2 id selectors |
|
Inline Styles |
|||
<div style="color:red">Inline style attached directly to an html element: 1-0-0-0 |
|||
!important |
p {font-size:16px !important}Can be used in style sheets or inline. Overrides everything, can only be overridden by another !important, only use in dire emergencies: 1-0-0-0-0 |