Discover how to make your webpage layouts smart and effortless with grid rows and columns!
Why Grid rows and columns in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to arrange photos on a webpage in neat rows and columns, like a photo album. You try to place each photo by setting exact positions with margins and padding.
If you add or remove photos, you must adjust every margin and position manually. It's slow, confusing, and easy to make mistakes that break the layout.
CSS Grid lets you define rows and columns easily. You just tell the browser how many rows and columns you want, and it arranges items automatically, adjusting when you add or remove content.
img { margin-left: 10px; margin-top: 20px; /* manual spacing for each photo */ }.container { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: auto; gap: 1rem; }You can create flexible, clean layouts that adapt automatically to different screen sizes and content changes.
Online stores use grid rows and columns to show products in neat rows that adjust when you filter or add new items.
Manual positioning is slow and error-prone.
Grid rows and columns automate layout arrangement.
Layouts become flexible and easy to maintain.
Practice
grid-template-columns control in a grid layout?Solution
Step 1: Understand the property name
The property namegrid-template-columnssuggests it controls columns.Step 2: Recall grid layout basics
This property sets how many columns there are and their widths.Final Answer:
The number and width of columns in the grid -> Option AQuick Check:
grid-template-columns = columns control [OK]
- Confusing columns with rows
- Thinking it controls gaps
- Mixing it with background styles
Solution
Step 1: Identify valid CSS syntax for repeating columns
Therepeat()function is the correct way to repeat values in grid-template-columns.Step 2: Check each option
grid-template-columns: 100px, 100px, 100px; uses commas (invalid); B: '3x 100px' invalid; C: repeat(3, 100px) correct; D: '* 3' invalid CSS syntax.Final Answer:
grid-template-columns: repeat(3, 100px); -> Option AQuick Check:
Use repeat() for repeating columns [OK]
- Writing invalid CSS like '3x 100px'
- Using commas between column sizes
- Using multiplication syntax not supported in CSS
display: grid; grid-template-columns: 150px 1fr 2fr; grid-template-rows: 100px auto;
Solution
Step 1: Count columns from grid-template-columns
There are three values: 150px, 1fr, 2fr, so 3 columns.Step 2: Count rows from grid-template-rows
There are two values: 100px and auto, so 2 rows.Final Answer:
3 columns and 2 rows -> Option DQuick Check:
Count values in columns and rows properties [OK]
- Mixing up rows and columns count
- Assuming 'auto' adds extra rows
- Ignoring fractional units
display: grid; grid-template-columns: 100px, 200px, 100px; grid-template-rows: 50px 50px;
Solution
Step 1: Check syntax for grid-template-columns
Values should be space-separated, not comma-separated.Step 2: Verify other options
grid-template-rows is correct without commas; display: grid is valid; units can differ.Final Answer:
Commas should not be used between column sizes -> Option BQuick Check:
Grid values are space-separated, not comma-separated [OK]
- Using commas between grid sizes
- Changing display property incorrectly
- Thinking units must match
Solution
Step 1: Set 4 equal columns
Usingrepeat(4, 1fr)creates 4 columns each taking equal space.Step 2: Define 3 rows with first fixed and others flexible
grid-template-rows: 100px 1fr 1fr;sets first row fixed 100px, next two share remaining space equally.Final Answer:
grid-template-columns: repeat(4, 1fr); grid-template-rows: 100px 1fr 1fr; -> Option CQuick Check:
repeat() for columns, fixed + fractional rows [OK]
- Wrong number of columns or rows
- Using auto instead of 1fr for equal space
- Setting all rows fixed height
