CSS Grid Layout Flashcards
What CSS property enables Grid layout on an element?
display
What does grid-template-columns
do?
It defines the number and size of the columns in the grid.
What happens if there are more grid items than defined columns/rows?
The grid creates additional implicit rows or columns to fit them.
How do you make an item span two columns?
Use grid-column: span 2;
or grid-column: 1 / 3;
What is the shorthand for grid-row
and grid-column
?
grid-area: row-start / column-start / row-end / column-end;
What’s the difference between auto-fill
and auto-fit
?
auto-fill
keeps empty tracks; auto-fit
collapses them if empty.
What does this CSS do?
grid-template-columns: repeat(3, 1fr);
Creates 3 equal-width columns. Creates 3 equal-width columns.
What is the default value of justify-items
and align-items
?
stretch
— items fill the entire cell by default.
What does this do?
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
Creates responsive columns that are at least 200px wide and grow to fill available space, wrapping when needed.
How do you position an item at column 2, row 3?
grid-column: 2;
grid-row: 3;
orgrid-area: 3 / 2;
How can you make the center column twice as wide as the others in a 3-column layout?
grid-template-columns: 1fr 2fr 1fr;
Does setting margin on a grid item affect gap?
No, gap
is separate from margins and doesn’t collapse with them.
When would you use min-content
or max-content
in a grid?
Use min-content
to shrink to the smallest size without overflow; max-content
to expand to the largest natural size of the content.
Make a 3-column layout with equal-width columns.
.container { display: grid; grid-template-columns: 1fr 1fr 1fr; }
Make the first item span 2 columns.
.item1 { grid-column: span 2; }
How to create columns that are at least 150px wide and auto-wrap?
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
Imagine there is a grid, how can you place .item3
in the second column and third row.
.item3 { grid-column: 2; grid-row: 3; }
Create a .container
grid and nest another grid inside .card.
<div class="container"> <div class="card"> <h3>Title</h3> <p>Content</p> <button>Buy</button> </div> </div>
Use subgrid to align columns between parent and child.
.parent { display: grid; grid-template-columns: 100px 1fr 1fr; } .child { display: grid; grid-column: 1 / 4; grid-template-columns: subgrid; }
Is this a valid declaration?
header { grid-area: 1 / 1 / 1 / 3; }
Invalid — row-start equals row-end, so height is 0.