Grid & Media Queries
Last updated
Last updated
CSS Grid is a display
type that is useful for arranging items in a container into rows and columns!
By default, grid items are arranged in a single column
The fr
unit (the "fractional unit") is unique to grid. It evenly distributes the grid container's available width to each grid item without overflowing.
Grid Tracks are numbered starting at 1
and go between columns and rows.
Grid Properties
grid-template-columns
defines for the grid container the number of columns and the width of each column
grid-auto-rows
controls the size of newly created rows
grid-column
determines which column a grid item will start in (and, optionally, which column it will end in)
grid-row
determines which row a grid item will start in (and, optionally, which row it will end in)
grid-template-area
defines the layout and size of the grid-area
s (see below) within the grid container.
grid-area
assigns a grid-area name to a grid item
Media queries allow us to apply CSS in response to changes to the device's screen size (and other things too!). They are how we implement responsive web design, ensuring usability across all devices.
Breakpoints are points at which a responsive web design will shift. We set them using @media (min-width)
and @media(max-width)
Mobile-First Design is a strategy for designing our websites for mobile devices and using media queries to modify that design for progressively larger screens.
We've learned that Flexbox is a display
type that allows us to arrange items inside a container into rows OR columns.
Grid on the other hand is a display
type that allows us to arrange items inside a container into rows AND columns
By default, a grid will have only 1 column. We can add additional columns using grid-template-columns
:
The fr
unit (the "fractional unit") is unique to grid. It evenly distributes the grid container's available width to each grid item without overflowing.
We often simplify this using the repeat()
function:
The fr
unit (the "fractional unit") is unique to grid. It evenly distributes the grid container's available width to each grid item without overflowing.
Q: There are 9 elements but only 4 columns. What do you notice happens when the elements overflow? How is the row height determined?
Q: How would you adjust this to make columns of different sizes? For example, make the middle two columns twice as wide as the outer two.
Media queries allow us to apply CSS in response to changes to the device's screen size (and other things too!). Media queries are how we implement responsive web design, ensuring usability across all devices.
Breakpoints are points at which a responsive web design will shift. Below are commonly agreed upon breakpoints for web development. They don't target every specific use case or device, but the ranges provide broad coverage.
X-Small Devices (portrait phones)
< 576px
Small Devices (landscape phones)
≥ 576px
Medium Devices (tables)
≥ 768px
Large Devices (desktops)
≥ 992px
X-Large Devices (large desktops)
≥ 1200px
XX-Large Devices (larger desktops)
≥ 1400px
Challenge: At 700px
and 900px
, change the number of columns to 2 and then to 3.
In a grid, the position of grid-items can span multiple rows and/or columns
Q: How many rows and columns does this layout have?
Grid Tracks are numbered starting at 1
and go between columns and rows.
Grid track numbers can be used to span grid items across columns and/or rows using grid-row
and grid-column
To specify the starting row / column for a grid-item, simply provide the grid track number as the value.
To span across multiple rows/columns, the syntax is start-row / end-row
Challenge: Using media queries and grid-row
/grid-column
, produce the grid layout below for screen sizes above 992px
Using media queries and the display: flex
properties, achieve the responsive design below starting with the code found in 5-responsive-flexbox-challenge/
Q: What do you notice if different about the two layouts?
Controlling the Horizontal Alignment of Columns
justify-content
controls the horizontal alignment of the columns within the entire grid
justify-items
controls the horizontal alignment of each item within its own column
justify-self
controls the horizontal alignment of a single item within the grid.
Controlling the Vertical Alignment of Rows
align-content
controls the vertical alignment of the rows within the entire grid
align-items
controls the vertical alignment of each item within its own row
align-self
controls the vertical alignment of a single item within the grid.
Honestly, to learn about alignment, just visit but here are the bullets.