CSS Grid - Comm244 Notes
CSS Grid - Comm244 Notes
CSS Grids:
There are three basic steps for creating a CSS Grid layout:
.grid-container {
display: grid
}
I'm using a class of 'grid-container' in this example, but we could use any element, class or id for the grid container.
Once we declare a grid container, all immediate children of that element become grid items. Only children, not descendents, are grid items.
Grid Terms
Grid lines
grid cell and grid areas
grid tracks (rows or columns)
web.simmons.edu/~grovesd/comm244/notes/week11/grid-basics.php 1/7
9/14/23, 1:44 PM CSS Grid | Comm244 Notes
Grid line
The horizontal and vertical dividing lines of the grid are called grid lines.
Grid cell
The smallest unit of a grid is a grid cell, which is bordered by four adjacent grid lines with no grid lines running through it.
Grid area
A grid area is a rectangular area made up of one or more adjacent grid cells.
Grid track
The space between two adjacent grid lines is a grid track, which is a generic name for a grid column or a grid row. Grid columns are said to go along the block axis,
which is vertical (as block elements are stacked) for languages written horizontally. Grid rows follow the inline (horizontal) axis.
Track sizes (rows and columns) can be specified using any of the following values:
web.simmons.edu/~grovesd/comm244/notes/week11/grid-basics.php 2/7
9/14/23, 1:44 PM CSS Grid | Comm244 Notes
Percentage values (%)
Fractional units (fr)
auto
min-content, max-content
minmax()
fit-content()
grid-template-rows
grid-template-columns
The following example will set the middle column to be twice the size of the first and third columns:
.grid-container {
display: grid
grid-template-columns: 1fr 2fr 1fr;
}
Best of all, fr units can be mixed with fixed units such as pixels and ems. This is great for maintaining fixed content areas for things like ads.
The browser first assigns space for the fixed measurements and then calculates the fr units.
.grid-container {
display: grid
grid-template-columns: 1fr 2fr 250px;
}
minmax()
The minmax() function can be used in place of a value to define both the minimum size and maximum size of a grid track. You cannot use fr units inside the minmax()
function.
.grid-container {
display: grid
grid-template-columns: min-content minmax(20em, 50em) max-content;
}
See also:
minmax() (MDN)
Track sizing and minmax() (MDN)
.grid-container {
display: grid
grid-template-columns: 1fr minmax(20em, 50em) 250px;
}
fit-content()
fit-content() will use a formula to allow a track to reflow to the minium size when needed but not display larger than the fit-content value.
In this example, the second column will get smaller as needed, but not expand greater than 300px.
.grid-container {
display: grid
web.simmons.edu/~grovesd/comm244/notes/week11/grid-basics.php 3/7
9/14/23, 1:44 PM CSS Grid | Comm244 Notes
grid-template-columns: 1fr fit-content(300px) 2fr;
}
See also fit-content() on MDN for a more precise definition and interactive example.
For example, imagine you wanted 10 columns that alternated between 1fr and 2fr. You could write this:
.grid-container {
display: grid
grid-template-columns: 1fr 2fr 1fr 2fr 1fr 2fr 1fr 2fr 1fr 2fr;
}
Using repeat() simplifies this. The first value is the number of times to repeat the pattern while the values after comma demonstrate the patter.
.grid-container {
display: grid
grid-template-columns: repeat(5, 1fr 2fr);
}
Using Firefox, you can see a visual overlay of any grid by using the developer tools in Firefox. Right now, Firefox has the best developer tools for CSS Grid.
web.simmons.edu/~grovesd/comm244/notes/week11/grid-basics.php 4/7
9/14/23, 1:44 PM CSS Grid | Comm244 Notes
grid-row-start
grid-row-end
grid-column-start
grid-column-end
The start and end values are identified by counting the grid lines in the grid.
In this example:
the header should stretch across all the columns and fit into 1 row
the header should stretch across all the columns and fit into 1 row
each aside should fill one grid column
main should fill the two right columns and the middle row
Using the criteria above, we can lay out the header with the following CSS:
header {
grid-column-start: 1;
web.simmons.edu/~grovesd/comm244/notes/week11/grid-basics.php 5/7
9/14/23, 1:44 PM CSS Grid | Comm244 Notes
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 2;
}
Grid lines can also be counted backwards, using negative numbers. The last line is -1, the second to last line is -2, etc.
When specifying elements to span across all columns or rows, it's easier and more versatile to use -1 for the end value. This avoids counting errors and will work even if
the grid lines change.
header {
grid-column-start: 1;
grid-column-end: -1;
grid-row-start: 1;
grid-row-end: 2;
}
Shorthand Properties
In additional to using the start and end properties for rows and columns, you can also use shorthand values instead to save typing.
grid-row
grid-column
header {
grid-column: 1 / -1;
grid-row: 1 / 2;
}
This still follows the general CSS rule for listing values in a clockwise order.
header {
grid-area: 1 / 1 / 2 / -1;
/* row-start / column-start / row-end / column-end */
}
CSS Grid allows you to name both grid lines and areas.
.grid-container {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: 100px 400px 100px;
}
Example grid »
The CSS property grid-template-areas allows you to name individual grid cells or groups of cells called areas in your grid. These names can be referenced later for
placing elements.
web.simmons.edu/~grovesd/comm244/notes/week11/grid-basics.php 6/7
9/14/23, 1:44 PM CSS Grid | Comm244 Notes
With grid-template-areas:
.grid-container {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: 100px 400px 100px;
grid-template-areas:
"header header header"
"ads main links"
"footer footer footer"
;
}
It helps to align the values on different lines to make the grid more clear
By itself, the page should change. You can hover use the Firefox developer tools to view the grid names.
main {
grid-area: main;
}
aside:first-of-type {
grid-area: links;
}
aside:last-of-type {
grid-area: ads;
}
footer {
grid-area: footer;
}
web.simmons.edu/~grovesd/comm244/notes/week11/grid-basics.php 7/7