7p EST / 6p CST / 4p PST
We can create a grid of rows and columns, and place our UI elements into specific areas of the grid
Our elements can occupy horizontal AND vertical space at the SAME TIME
"Web Layout" was a page with text, colored links, images, and text alignment to make things really interesting
...as in the HTML tag, which was used to create more structured layout because CSS was often not reliable across browsers
Goodbye tables!
They can be used together
Kevin Powell is a CSS Evangelist, Instructor and YouTuber
To Grid or to Flex? (CSS {In Real Life})
CSS Grid vs Flexbox | Which one should I use? (fullPage.js)
Whatโs the Difference Between Flexbox and Grid? (CSS Tricks)
CSS Grid vs Flexbox: How to decide (with examples) (Scrimba)
Clone or Fork: Grid Fundamentals
display: grid
Declare grid
property on a container
.container {
display: grid;
}
When grid
is declared on a container, the container becomes a
grid container
; elements inside the
container become
grid items
Create columns and rows (aka 'tracks') with
grid-template-columns
and
grid-template-rows
.container {
display: grid;
grid-template-columns: 100px 200px 150px;
grid-template-rows: 50px 100px 150px;
}
A grid track
is a column or row of the
grid
A grid cell
is an individual unit of
the grid
fr
fr
.container {
display: grid;
grid-template-columns: 1fr 1fr 1f;
}
1fr
each,
meaning they take up equal space
auto
Unit
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr auto;
}
auto
,
meaning it gets whatever space is left or is needed for its content
repeat()
repeat()
is a function to write shorter
code when there's a recurring pattern
repeat(number_of_times, dimension)
.container {
display: grid;
/* grid-template-columns: 1fr 1fr 1fr 1fr 1fr auto; */
grid-template-columns: repeat(5, 1fr) auto;
/* More than one unit can also be specified */
grid-template-columns: repeat(3, 1fr 50px)
}
gap
Creates space or margins (aka gutters) between tracks
.container {
display: grid;
grid-template-columns: repeat(5, 1fr) auto;
gap: 1rem;
/* gap is shorthand for:
row-gap: 1rem;
column-gap: 1rem;
*/
}
Clone or Fork:
Grid Fundamentals Exercise
Grid items are arranged along
grid lines
-- the dividing horizontal
and vertical lines that make up the grid
Article: Understanding CSS Grid: Grid Lines (Smashing Magazine)
Image: Understanding CSS Grid: Grid Lines (Smashing Magazine)
Clone or Fork:
Grid Sizing & Placement
Elements
(Chrome) or
Inspector
(Firefox) tool tab, select the
Layout
panel
Grid Overlays
(Chrome) or
Overlay Grid
(Firefox) option
Grid lines are used to position where an element or grid item should start and end
The following placement properties are used on
grid items
to position them on the
grid
.item1 {
grid-column-start: 1; /* Start at Column Line 1*/
grid-column-end: 3; /* End at Column Line 3 */
grid-row-start: 1; /* Start at Row Line 1 */
grid-row-end: 3; /* End at Row Line 3*/
}
A grid item
can start at any point on the grid--not just at
line 1
When this happens, the browser will shift other elements to maintain the flow of the grid
.item1 {
grid-column-start: 3;
grid-column-end: 5;
grid-row-start: 2;
grid-row-end: 6;
}
The placement properties can also be written in shorthand form:
grid-column: start / end
grid-row: start / end
.item1 {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
When a grid item
occupies only one cell, the
ending grid line number
can be omitted
.item1 {
grid-column: 3;
grid-row: 2;
}
An alternate way to place grid items across multiple cells is to use the
span
keyword
span
Syntaxgrid-column: span #_of_cells
grid-column: starting line # / span #_of_cells
grid-column: span #_of_cells / ending line #
.item1 {
grid-column: span 3;
/* Default starting position, then span 3 cells */
grid-row: 2 / span 2;
/* Start at grid-row line 2 and span down 2 cells */
}
Create a full-width span effect by setting the ending grid line number to
-1
.item1 {
grid-column: 1 / -1;
grid-row: 3;
}
Clone or Fork:
Grid Sizing & Placement Exercise