Keenan Bailles
“Writing CSS is easy. Maintaining CSS is hard.”
You can either make things
or
Example using Atomic CSS:
@std-margin: 16px;
.mt-1 { margin-top: @std-margin; }
.mr-1 { margin-right: @std-margin; }
.mt-2 { margin-top: 2*@std-margin; }
.ov-h { overflow: hidden; }
.d-b { display: block; }
.va-m { vertical-align: middle; }
.fl-st { float: @float-start; }
.fl-nd { float: @float-end; }
.fw-b { font-weight: bold; }
.fs-i { font-style: italic; }
.headshot {
width: @headshot-dimension;
}
BEM is a naming convention to do away with nesting selectors and increase awareness of the elements that are receiving styles. There are three categories in BEM:
Organize files according to the UI design structure
Browsers read CSS right to left. Because of this, the rightmost selector is often called the "key selector"
There are four kinds of "key selector's": Id -> class -> tag -> universal. The order here is also the order of efficiency. Id's are much faster to process than a universal selector.
If two selectors target the same element the one with the higher specificity will win. If they have the same specificity then cascade.
Greatest specificity to lowest is: Inline -> Id -> Class/Psuedo-Class/Attribute -> Element/Psuedo-Elm. !important trumps everything. !important is only overridden with !important. That is why you should not use it.
Selector | Value |
---|---|
Inline | 1000 |
Id | 100 |
Class/Psuedo-Class/Attribute | 10 |
Element/Psuedo-Elm | 1 |
Example 1
ul#Nav li.active a {}
This selector contains 1 Id, 1 class/psuedo-class/attribute, and targets 3 elements. It's specificity would be: 0, 100, 10, 3.
Example 2
#footer *:not(nav) li {}
This selector contains 1 Id, 0 class/psuedo-class/attribute (:not is a unique psuedo class in that it adds no specificity. What's inside the parens is what adds to the specificity), and targets 2 elements (Universal selector has a no specificity value). It's specificity would be: 0, 100, 0, 2.