Grid item placement in CSS - Mini Project: Build & Apply
<div> with class grid-container and inside it add three <div> elements with classes item1, item2, and item3. Each item should contain text: "Item 1", "Item 2", and "Item 3" respectively.Use a container div with class grid-container. Inside it, add three divs with classes item1, item2, and item3. Put the text "Item 1", "Item 2", and "Item 3" inside each respectively.
.grid-container that sets display to grid, defines grid-template-columns as three equal columns using 1fr, and grid-template-rows as two equal rows using 100px height each.Use display: grid; on .grid-container. Then set grid-template-columns to 1fr 1fr 1fr for three equal columns. Set grid-template-rows to 100px 100px for two rows of 100 pixels height.
.item1 and .item2. Place .item1 in column 1, row 1 using grid-column and grid-row. Place .item2 to span columns 2 and 3 in row 1 using grid-column with a span and grid-row.Use grid-column: 1; and grid-row: 1; for .item1. For .item2, use grid-column: 2 / span 2; to cover columns 2 and 3, and grid-row: 1;.
.item3 to place it in column 2, row 2 using grid-column and grid-row. Add background colors and center the text inside all items using display: flex, align-items: center, and justify-content: center.Use grid-column: 2; and grid-row: 2; for .item3. Add display: flex;, align-items: center;, and justify-content: center; to center the text inside the item.
Practice
grid-column: 2 / 4; do in a grid container?Solution
Step 1: Understand grid-column syntax
The syntaxgrid-column: start / end;means the item starts at the start line and ends before the end line.Step 2: Apply to given values
Here, start is 2 and end is 4, so the item covers columns 2 and 3 (lines 2 to 4).Final Answer:
It places the grid item starting at column line 2 and ending before column line 4. -> Option AQuick Check:
grid-column: 2 / 4 means columns 2 to 3 [OK]
- Thinking it includes the end line column
- Confusing span count with line numbers
- Assuming it skips columns between start and end
Solution
Step 1: Recall grid-row span syntax
The correct syntax to span rows isgrid-row: start / span number;.Step 2: Match with options
grid-row: 1 / span 3; uses1 / span 3, meaning start at line 1 and span 3 rows down.Final Answer:
grid-row: 1 / span 3; -> Option AQuick Check:
Start line then span count is correct [OK]
- Reversing start and span values
- Using dash instead of slash
- Placing span before start line
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 100px 100px;
.item {
grid-column: 3 / 5;
grid-row: 1 / 2;
}Solution
Step 1: Analyze grid-column placement
The item starts at column line 3 and ends before line 5, so it covers columns 3 and 4.Step 2: Analyze grid-row placement
The item starts at row line 1 and ends before line 2, so it is on the first row only.Final Answer:
The item spans columns 3 and 4 on the first row. -> Option CQuick Check:
grid-column: 3 / 5 and grid-row: 1 / 2 means columns 3-4, row 1 [OK]
- Confusing row numbers with column numbers
- Assuming end line is included
- Mixing up first and second row
.box {
grid-column: span 2 / 4;
grid-row: 1 / span 3;
}Solution
Step 1: Check grid-column syntax
The correct syntax isstart / endorstart / span count. Here,span 2 / 4is reversed; span should be second.Step 2: Check grid-row syntax
grid-row: 1 / span 3;is correct syntax to start at line 1 and span 3 rows.Final Answer:
The order of values in grid-column is incorrect; span should come second. -> Option DQuick Check:
Span always follows start line [OK]
- Placing span before start line
- Mixing up grid-column and grid-row syntax
- Assuming span can be first value
Solution
Step 1: Understand grid size and placement
The grid has 4 columns, so column lines go from 1 to 5 (one more than columns).Step 2: Check each option
- A:
grid-column: 2 / 6;tries to end at line 6, outside explicit grid (may create implicit tracks). - B:
grid-column: 2 / 4;ends at line 4, spans columns 2-3 only (2 columns). - C:
grid-column: 2 / span 3;starts at line 2, spans 3 columns to line 5, fits perfectly. - D:
grid-column: span 3 / 2;has incorrect syntax (span cannot precede start).
- A:
Final Answer:
grid-column: 2 / span 3; -> Option BQuick Check:
Start line then span within grid limits [OK]
- Using end line beyond grid size
- Reversing span and start line
- Assuming grid lines equal columns
Ready to prove it?
Pass the test and this pattern moves unknown
