selector {
property1: value;
property2: value;
property3: value;
}
Matches all the elements of that type on the page
main {
/* some properties */
}
div {
/* some properties */
}
p {
/* some properties */
}
<attr>
Targets elements that contain the <attr>
attribute, regardless of its value.
[data-colour] {
/* some properties */
}
<attr>=<val>
Targets elements where the <attr>
’s value is exactly <val>
.
[data-colour="green"] {
/* some properties */
}
<attr>~=<val>
Targets elements with the <attr>
attribute whose value is a list of white space separated words, one of which must be <val>
. <val>
itself cannot contain white space, and neither can it be an empty string.
[data-colour~="green"] {
/* some properties */
}
<attr>|=<val>
Targets elements with the <attr>
attribute whose value is exactly <val>
or starts with <val>
immediately followed by a “-”.
Primary use-case being for language subcode matching, like “en”, “en-US” and “en-UK”.
[data-colour|="green"] {
/* some properties */
}
<attr>^=<val>
A substring matching selector. Targets elements with the <attr>
attribute whose value starts with <val>
.<val>
cannot be an empty string.
[data-colour^="green"] {
/* some properties */
}
<attr>$=<val>
A substring matching selector. Targets elements with the <attr>
attribute whose value ends with <val>
.<val>
cannot be an empty string.
[data-colour$="green"] {
/* some properties */
}
<attr>*=<val>
A substring matching selector. Targets elements with the <attr>
attribute whose value contains an instance of <val>
.<val>
cannot be an empty string.
[data-colour*="green"] {
/* some properties */
}
:root
:nth-child(n)
:nth-last-child(n)
:nth-of-type(n)
:nth-last-of-type(n)
:first-child
:last-child
:first-of-type
:last-of-type
:only-child
:only-of-type
:empty
:link
:visited
:active
:hover
:focus
:enabled
:disabled
:checked
:not(selector)
:lang(zh)
:target
::first-line
, ::first-letter
, ::before
, ::after
Do something to the first letter, or first line. Just the first though. Not the others. Leave them lines alone.
Matches all the elements with the specified class attribute
<div class="example">
// some random markup
</div>
.example {
/* some properties */
}
Matches matches the element with the specified id attribute
<div id="example">
// some random markup
</div>
#example {
/* some properties */
}
Can be any level of descendant, multiple nesting
This is a <p> element in a <div> element. And this is a <strong> element within a <p> within a <div>.
div span {
/* some properties */
}
Immediate child element only
This is <p> element is a direct child of the <div> element.
div > p {
/* some properties */
}
Same parent, immediately following
This is a <p> element.
- This is a list item
- This is a list item
p + ul {
/* some properties */
}
Same parent, can be anywhere following
This is a <p> element.
This is another <p> element.
- This is a list item
- This is a list item
p ~ ul {
/* some properties */
}
!important
Don't use it if you can help it.
This paragraph has a class of cookie and an id of butterscotch.
This paragraph has a class of cookie.
Inline styles
IDs
Classes, attributes and pseudo-classes
Elements and pseudo-elements
ul {
// CSS properties
}
0, 0, 0, 1
.class-1 .class-2 p {
// CSS properties
}
0, 0, 2, 1
#id-1 .class-3 div {
// CSS properties
}
0, 1, 1, 1
The tie-breaker for equal specificity values
This paragraph has a class of cookie and an id of butterscotch.
This paragraph has a class of cookie.
98.68% of internet users are using a browser that supports Flexbox
93.26% of internet users are using a browser that supports Grid
For single dimension components
For two-dimensional layouts
flex: initial |
flex: 0 1 auto , cannot grow but can shrink when there isn't enough space |
flex: auto |
flex: 1 1 auto , can grow and shrink to fit available space |
flex: none |
flex: 0 0 auto , cannot grow or shrink, AKA inflexible |
flex: <positive-number> |
flex: <positive-number> 1 0 , can grow and shrink, extent of growth depends on flex factor |
Centre stuff in their parent container
When you need 1 thing by itself on the opposite side
To stick the header and footer at the top and bottom
Define your grid.
Place items in the grid.
<div class="grid1">
<div class="grid1__item">
<p>Item A</p>
</div>
<div class="grid1__item">
<p>Item B</p>
</div>
<div class="grid1__item">
<p>Item C</p>
</div>
<div class="grid1__item">
<p>Item D</p>
</div>
<div class="grid1__item">
<p>Item E</p>
</div>
<div class="grid1__item">
<p>Item F</p>
</div>
</div>
Item A
Item B
Item C
Item D
Item E
Item F
fr
unitRepresents a fraction of the free space in the grid container.
Item A
Item B
Item C
.container {
display: grid;
grid-template-columns: repeat(3, 3fr 2fr);
}
minmax()
functionDefines a size range for columns or rows in the grid.
Item A
Item B
Item C
repeat()
functionTo specify a large number of columns or rows that follow a similar pattern
Item
Item
Item
Item
Item
Item
Item
Item
auto-fill
vs. auto-fit
Allow browser to determine how many tracks to create depending on track size.
repeat(auto-fill, 100px);
repeat(auto-fit, 100px);
auto-fit
collapses empty tracks.
auto-fill
versus auto-fit
A
B
C
D
E
F
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(10em, 1fr));
}
.grid-container {
display: grid;
grid-template-columns: 150px 150px 150px; /* three columns */
grid-template-rows: 150px 150px; /* two rows */
}
.item {
grid-column: 5 / 6; /* grid-column-start: 5; grid-column-end: 6; */
grid-row: 2 / 3; /* grid-row-start: 2; grid-row-end: 3; */
}
The browser will create an implicit grid to hold items placed outside the grid.
grid-auto-flow
propertyAdjusting the direction and density of grid items
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
To create a cohesive and common box alignment model to share among all of CSS
Property | Axis | Aligns | Applies to |
---|---|---|---|
justify-content |
main/inline | content within element |
block containers, flex containers and grid containers |
align-content |
cross/block | ||
justify-self |
inline | element within parent |
block-level boxes, absolutely-positioned boxes and grid items |
align-self |
cross/block | absolutely-positioned boxes, flex items and grid items |
|
justify-items |
inline | items inside box |
block containers and grid containers |
align-items |
cross/block | flex-containers and grid-containers |
Source: CSS Box Alignment Module Level 3
align-content
justify-content
align-items
align-self
The justify-items
/justify-self
properties do not apply to flex items
align-content
justify-content
align-items
align-self
justify-items
justify-self
align-content
justify-content
align-items
align-self
The justify-items
/justify-self
properties do not apply to flex items
align-content
justify-content
align-items
align-self
justify-items
justify-self
Justify
justify-content
& align-content
justify-content
& align-content
justify-self
& align-self
justify-items
& align-items
align-items
& align-self
If time permits…
Allows us to change the shape and position of the affected content
without disrupting the normal document flow
rotate( <angle> ) |
Performs a 2D rotation by the angle specified around the element's origin |
translate( <translation-value> [, <translation-value> ]? ) |
Performs a 2D translation in the specified X and Y directions |
skew( <angle> [, <angle> ]? ) |
Performs a 2D skew by the angles specified |
scale( <number> [, <number> ]? ) |
Performs a 2D scale operation by the scaling vector specified |
.trf2d-cont {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-template-rows: repeat(3, 2em) repeat(3, minmax(10vmin, 3em)) 2em 2em 50vh;
grid-gap: 0.125em;
position: relative;
}
@media screen and (min-width: 480px) {
.trf2d-cont {
transform: rotate(-30deg) translateX(5vh);
}
}
@media screen and (min-width: 640px) {
.trf2d-cont {
transform: rotate(-30deg) translateY(-15vh);
}
}
Lets you create gradual transitions between the values of specific CSS properties
Lets you animate the values of CSS properties over time, using keyframes
Note: not all CSS properties are animatable
transition
is a shorthand property, covers the following (values are initial default values):
transition-delay: 0s
transition-duration: 0s
transition-property: all
transition-timing-function: ease
animation
is a shorthand property, covers the following (values are initial default values):
animation-name: none
animation-duration: 0s
animation-timing-function: ease
animation-delay: 0s
animation-iteration-count: 1
animation-direction: normal
animation-fill-mode: none
animation-play-state: running
Specified as one or more single animations, separated by commas.
See the Pen CSS Fool's Mate by Chen Hui Jing (@huijing) on CodePen.
See the Pen Magical kittencorn by Chen Hui Jing (@huijing) on CodePen.
Read them first, tutorials later.
CSS specifications: https://www.w3.org/Style/CSS/specs.en.htmlGo-to resource for all things web development.
MDN web docs: https://developer.mozilla.org/en-US/Font is Magnetic Pro by Olivier Gourvat