↑
Original image by the amazing Lin ClarkThis workshop has been inadvertently brought to you by Philippine Airlines.
“Grid works from the container in, other layout methods start with the item”
—Rachel Andrew
inline-block
Item A
Item B
Item C
Item D
Item E
Item F
float
Item A
Item B
Item C
Item D
Item E
Item F
flex
Item A
Item B
Item C
Item D
Item E
Item F
“Grid is the only layout technique that establishes a relationship between rows and columns of grid items.”
Using grid-template-rows
and grid-template-columns
<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
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
versus auto-fit
Letting the browser figure out the math
A
B
C
D
E
F
grid-auto-row
and grid-auto-column
Controlling the size of implicit rows/columns
A
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
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
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
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(10em, 1fr));
}
Using the min-content
, max-content
and fit-content
properties
What's worse, lookin' jealous or crazy? Jealous or crazy?
Or like being walked all over lately, walked all over lately
I'd rather be crazy
Using grid-row-start
, grid-row-end
and grid-column-start
, grid-column-end
grid-row
and grid-column
shorthandsBy default, grid items will take up the space of 1 grid cell
🍏
🍊
🥥
🥑
🍇
🍌
span
keywordContent can span multiple grid cells
grid-area
shorthandgrid-area: <grid-row-start> / <grid-column-start> / <grid-row-end> / <grid-column-end>
Item A
Item B
Item C
Item D
Item E
Item F
Using grid-template-areas
and grid-area
Item A
Item B
Item C
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
justify/align-content
content-distribution properties
Values | justify-content |
align-content |
---|---|---|
center |
||
start |
||
end |
||
space-around |
||
space-between |
||
space-evenly |
Values | justify-content |
align-content |
---|---|---|
center |
||
start |
||
end |
||
space-around |
||
space-between |
||
space-evenly |
justify/align-self
self-alignment properties
justify/align-items
defaults for justify/align-self
flex
shorthand“Authors are encouraged to control flexibility using the flex shorthand rather than with its longhand properties directly, as the shorthand correctly resets any unspecified components to accommodate common uses.”
—CSS Flexible Box Layout Module Level 1
flex
keyword valuesinitial |
0 1 auto |
cannot grow but can shrink when there isn't enough space |
auto |
1 1 auto |
can grow and shrink to fit available space |
none |
0 0 auto |
cannot grow or shrink, AKA inflexible |
<positive-number> |
<positive-number> 1 0 |
can grow and shrink, extent of growth depends on flex factor |
“When a box is a flex item, flex is consulted instead of the main size property to determine the main size of the box.”
—CSS Flexible Box Layout Module Level 1
flex
syntax/* One value, unitless number: flex-grow */
flex: 3;
/* One value, width/height: flex-basis */
flex: 200px;
flex: 45em;
/* Two values: flex-grow | flex-basis */
flex: 1 25ch;
/* Two values: flex-grow | flex-shrink */
flex: 2 1;
/* Three values: flex-grow | flex-shrink | flex-basis */
flex: 2 3 30%;
auto
marginsjustify-content
helps distribute extra free space left over after flexible lengths and auto margins are resolved.
align-items
sets the default alignment for all flex items along the cross axis of the flex line. Over-ridable by align-self
.
align-content
aligns flex lines within the flex container if there is extra space along the cross-axis.
Using @supports
AKA feature queries
.selector {
/* Styles that are supported in old browsers */
}
@supports (property:value) {
.selector {
/* Styles for browsers that support the specified property */
}
}
See the Pen The Critical Request header by Chen Hui Jing (@huijing) on CodePen.
and
See the Pen Responsive header example by Chen Hui Jing (@huijing) on CodePen.
See the Pen Grid layout with overlaps by Chen Hui Jing (@huijing) on CodePen.
See the Pen Charles Hayter's colour diagrams by Chen Hui Jing (@huijing) on CodePen.
(for the rest of the questions not already covered)
“CSS is a team sport.”
—Me
Font used is Prospectus, by Dave Bailey