CSS Grid Overview
Written
— Updated
- Source: https://scotch.io/tutorials/deep-dive-into-css-grid-2
- And more added over time
- Also See
- http://1linelayouts.glitch.me/ has a lot of nice CSS Grid examples
- Task Manager Example
- Definitions
- “Grid lines“ are like lines between cells on a spreadsheet.
- “Grid tracks” are the rows and columns in the grid, where the content actually goes.
- Cells are the individual spaces in the tracks where content goes.
- Areas are cells combined together when they span more than one row or column.
display:grid
sets the element to be a container for a grid layout.- Row and Column Sizing
fr
units- These units are new for Grid layout.
fr
stands for fraction, and1fr
represents one unit of space out of the sum of thefr
units. - So
3fr 4fr 3fr
would be 10 in all with the space allocated proportionally. - When fixed units are used alongside fractions, the fixed units get their space and the rest is allocated proportionally to the fractions.
- These units are new for Grid layout.
minmax(min, max)
sets flexible sizing, with the size clamped to the values given.width: auto
tells the item to use as much space as possible given the constraints of the other items. As a minimum, it is similar towidth:min-content
.width: fitcontent(200px)
works likeauto
but with a maximum size- If a CSS grid column does not have an explicit width then its min-width ends up similar to
auto
. This can sometimes cause undesired issues, like with large images or<pre>
tags, and so using something likeminmax(0px, 1fr)
can help here.
- Grid Layout
grid-template-columns
defines the grid column layout- It takes a variety of values.
100px 100px 100px
defines three columns each of 100px width, for example
grid-template-rows
works just likegrid-template-columns
but for rows- If either rows or columns is omitted, the widest/longest element in the row or column is used for all elements.
grid-template
is shorthand for rows, columns, andgrid-template-areas
grid-auto-columns
defines the size of columns not explicitly specified in the template. Likewise forgrid-auto-rows
grid-auto-flow
defines how grid cells are added into the grid.row
is the default which means that they go horizontally filling a row first.column
can also be used to go vertically.- Grid items can use
grid-row-start
andgrid-row-end
to define starting and ending grid indexes to span multiple grid spaces. grid-row
andgrid-column
are shorthand for the start and end propertiesgrid-column: 3 / 5
would start at column 3 and end at column 5grid-area: rowstart / columnstart / rowend / columnend
can also be used- You can use negative numbers to indicate offsets from the end:
1 / -1
would span the entire row or column. - Instead of absolute widths you can use
span
to indicate widths.3 / span 2
to start at 3 with a width of 2, orspan 2/ 5
to end at 5 and go back 2 from there.
grid-template-area
- This allows you to name different areas of the grid
- When cells have the same name, CSS combines them into a single area that spans multiple cells.
- You can then use
grid-area: header
to indicate that an element should go in a particular grid spot. - A
.
can indicate that a cell should be empty. grid-template
combines the areas with the sizes/ 100
- The size at each line is the height of the row, and then the sizes after the slash at the end are the column widths.
- You can also define the rows and columns separately
}
- So in this case the
main
area is named and the[main-start]
and[main-end]
delimiters specify its location and size.
repeat
just repeats some layout some number of times.repeat(3, 1fr 2fr)
expands to1fr 2fr 1fr 2fr 1fr 2fr
repeat
can be interspersed with other values toorepeat(auto-fill, values)
repeats the values as much as possible without overflowing.auto-fit
works likeauto-fill
but empty tracks are collapsed.grid-template-columns: repeat(auto-fill, minmax(50px, 1fr))
is a commonly-used technique to fit as many equally-spaced columns as possible, keeping a minimum width too.
grid-row-gap
andgrid-column-gap
define spacing between cellsorder
sets the order in which cells appear if you want to do so explicitly.grid-auto-flow: dense
can allow the grid system to place cells in the first empty grid spot, even if it’s before other items in the logical order.