Lesson 3.8: CSS Grid – Two-Dimensional Layouts
CSS Grid is a two-dimensional layout system in CSS that allows you to design web pages using rows and columns. Unlike Flexbox (which is one-dimensional), Grid makes it easy to create complex layouts with precise control over both horizontal and vertical alignment.
1. Basic Concepts
-
Grid Container – The parent element that holds grid items.
-
Grid Items – The child elements inside the grid container.
2. Grid Properties for Container
a) grid-template-columns & grid-template-rows
-
Define the number and size of columns and rows.
b) gap
-
Adds spacing between grid items.
c) grid-auto-flow
-
Controls how items are placed automatically.
3. Grid Properties for Items
a) grid-column & grid-row
-
Specifies how many columns or rows an item spans.
b) justify-self & align-self
-
Align a single grid item within its cell.
4. Shorthand Property
-
grid-areacombines row and column placement in a single property.
5. Example: Simple 3×2 Grid
✅ Result:
-
A 3-column, 2-row grid with equal spacing and styled items.
Key Takeaways
-
Grid = two-dimensional layout (rows + columns).
-
grid-template-columnsandgrid-template-rowsdefine structure. -
gapadds spacing between items. -
grid-column,grid-row,grid-areacontrol item placement.
