Incomplete CSS Cheatsheet
Introduction
Some running notes about CSS.
I first used CSS at least ten years ago. Since then, I have acquired knowledge and skills piecemeal. I recently decided to sit down and learn the best practices of the specification in 2023 from this site. Here are my summarized notes.
Table of Contents
The Box Model
content box
: artworkpadding box
: mounting boardborder box
: framemargin box
: space between each frame
__________ ________border box________ _______________________
| | padding box | | |
_______ | m | ___________________ | m | _________________ |
| | a | | | | a | | | |
| | r | | | | r | | | |
| | g | | |\---/| | | g | | | |
| | i | | | o_o | | | i | | | |
| | n | | \_^_/ | | n | | | |
| | | | | | | | | |
| | b | | content box | | b | | | |
| | o | | | | o | | | |
| | x | | | | x | | | |
_______| | | |__________________| | | |_________________| |
| | | | |
__________| |________________________| |______________________|
Selectors
-
selector
- what element is being styled -
declaration
- a property-value combination -
property
- what is being configured -
value
- the configuration
.my-css-rule {
__________________
Declaration --> | background: red; |
------------------
________ ________
Property --> | color: | | beige; | <-- Value
-------- --------
font-size: 1.2rem;
}
Types of Selectors
Universal selector
- matches all HTML elements and applies the styles
* {
color: green;
}
Type selector
- matches an HTML element directly and applies the styles
<section>...</section>
section {
padding: 2em;
}
Class selector
- matches any element containing
my-class
and applies the styles
<div class = "my-class">...</div>
.my-class {
color: red;
}
Id selector
- matches any element with
id = my-id
and applies the styles
<div id = "my-id">...</div>
#my-id {
border: 1px solid blue;
}
Attribute selector
- matches any element with
{attr} = {value}
and applies the styles
<div data-type = "primary">...</div>
[data-type = 'primary'] {
color: red;
}
/* matches any element with the data-type attribute, regardless of value */
[data-type] {
background-color: yellow;
}
group selector
- matches multiple elements at once
<div class = "first">This is important</div>
<div class = "second">So is this</div>
<div class = "third">And this</div>
.first,
.second,
.third {
font-size: 2em
}
Complex Selectors
- composed of multiple selectors and a combinator, which describes the relationship between selectors
{a} {combinator} {b}
Descendant selector
- selects all descendants of
a
that match selectorb
and applies styles tob
- combinator is a
- may be applied recursively
<div>
<p>This is styled by a descendant selector
<p>So is this</p>
</p>
</div>
Descendant combinator
|
|
v
div p {
color: blue;
}
Child selector
- selects direct children of
a
that match selectorb
and applies styles tob
- combinator is
>
<div>
<span>This span is styled</span>
<section>
<span>This one is not, because it's not a direct child of a div</span>
</section>
</div>
Child combinator
|
|
v
div > span {
font-size: 2em;
}
Next-sibling selector
- selects the next element immediately following
a
that matches selectorb
and applies styles tob
- combinator is
+
<p>This is a paragraph</p>
<span>This span is styled by a next-sibling selector</span>
<span>This one isn't</span>
Next-sibling combinator
|
|
v
p + span {
color: red;
font-weight: bold;
}
Subsequent-sibling selector
- selects any siblings following
a
and matches selectorb
and applies styles tob
- combinator is
~
<div>
<p>This paragraph is styled by a subsequent-sibling selector</p>
<span>I'm not styled</span>
<p>But I am</p>
</div>
Subsequent-sibling combinator
|
|
v
div ~ p {
color: green;
}
Compound selectors
- selectors can be combined
<a class = "my-class">...</a>
a.my-class {
color: red;
}
The Cascade
The Cascade is an algorithm for solving conflicts where multiple CSS rules apply to an HTML element. It decides which single style value applies for any particular element. To resolve conflicts, the algorithm processes CSS rules through the following stages:
Position and order of appearance
- Sequential order in which CSS rules appear
Specificity
- Numerical algorithm that awards points for how specific a selector is
- More details in specificity section
Origin
- The original source of the CSS content
- Hierarchy (top-to-bottom)
- User Agent styles with
!important
- Local user styles with
!important
- Authored CSS with
!important
- Authored CSS
- Local user styles
- User Agent styles
Importance
- Specific elements have elevated importance
- Hierarchy (top-to-bottom)
transition
rule-type!important
rule-typeanimation
rule-type- all other rules, such as
font-size
,background
, orcolor
Specificity
Specificity is awarded on a points system. CSS rules with more points are used over rules with fewer points.
Scoring
Rule | Points | Example |
---|---|---|
Universal selector | 0 | * { |
Element selector | 1 | div { |
Pseudo-element selector | 1 | ::selection { |
Class selector | 10 | .my-class { |
Pseudo-class selector | 10 | :hover { |
Attribute selector | 10 | [href=’#’] { |
ID selector | 100 | #myID { |
Inline style attribute | 1000 | <div style = "color: red;"></div> |
!important rule | 10000 | .my-class { |