Definition: Converts an HTML element into a grid container that arranges its children (grid items) in
rows and columns. This enables two-dimensional layout control with powerful alignment and sizing
capabilities. Default Value: display: block (not grid) Value: grid | inline-grid
β¦ Value 1: display: grid (Block-level Grid)
Creates a block-level grid container that takes full width available.
Definition: Defines the number of columns in the grid and specifies the size of each column. Controls
the width of columns and how space is distributed across the grid horizontally. Default Value: auto (single column) Values: px | em | % | fr | auto | repeat() | minmax() | auto-fit | auto-fill
β¦ Value 1: Fixed Pixel Width (px)
Each column has a fixed width regardless of container size.
100px
150px
120px
grid-template-columns: 100px 150px 120px;
β¦ Value 2: Fr Unit (Fractional)
Distributes available space proportionally using fr (fraction) units. 1fr = 1 equal
portion, 2fr = 2 portions.
1fr
2fr
1fr
grid-template-columns: 1fr 2fr 1fr;
β¦ Value 3: Percentage (%)
Each column takes a percentage of the container width.
25%
50%
25%
grid-template-columns: 25% 50% 25%;
β¦ Value 4: repeat() Function
Creates multiple columns with the same size. repeat(count, size) creates count columns each
of size.
1
2
3
4
grid-template-columns: repeat(4, 90px);
β¦ Value 5: minmax() Function
Sets minimum and maximum column width. Useful for responsive designs: minmax(min, max).
Definition: Defines the number of rows in the grid and specifies the height of each row. Controls the
vertical sizing of rows and how space is distributed across them. Default Value: auto (rows are sized by content) Values: px | em | % | fr | auto | repeat() | minmax()
Definition: Creates spacing (gutters) between grid rows and columns. Controls the distance between
grid items. The gap property is a shorthand for row-gap and column-gap. Default Value: 0 (no gap) Shorthand Values: gap: row-gap column-gap | gap: all-gaps Sub-properties: row-gap | column-gap
Different spacing for rows and columns: gap: row-gap column-gap
A
B
C
D
E
F
gap: 25px 10px; /* row-gap=25px, column-gap=10px */
β¦ Value 3: row-gap (Vertical Spacing Only)
Controls spacing between rows only.
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
row-gap: 30px; /* Large vertical spacing */
column-gap: 5px; /* Small horizontal spacing */
β¦ Value 4: column-gap (Horizontal Spacing Only)
Controls spacing between columns only.
1
2
3
4
column-gap: 20px; /* Large horizontal spacing */
row-gap: 8px; /* Small vertical spacing */
β¦ Value 5: Em Units (Relative Sizing)
Use em units for gap that scales with font size.
Em Units
Responsive
Design
gap: 1.5em; /* Scales with font-size */
β¦ Value 6: Rem Units (Root Font Size)
Use rem for consistent spacing based on root font size.
Rem Units
Consistent
Spacing
gap: 1rem; /* Based on root (html) font-size */
π justify-items
Definition: Aligns grid items horizontally (inline axis) within their grid cells. Controls how items
are positioned left-to-right inside each cell of the grid. Applies to all items in the grid container. Default Value: stretch (items fill available width) Values: start | center | end | stretch | auto
Visual comparison of all justify-items values side by side.
justify-items: start
β«
β«
β«
justify-items: center
β«
β«
β«
justify-items: end
β«
β«
β«
π align-items
Definition: Aligns grid items vertically (block axis) within their grid cells. Controls how items are
positioned top-to-bottom inside each cell. Applies to all items in the grid container on the vertical
axis. Default Value: stretch (items fill available height) Values: start | center | end | stretch | auto
Combine justify-items: center and align-items: center for perfect centering.
π― Centered
π― Centered
π― Centered
π― Centered
justify-items: center;
align-items: center;
/* Both horizontal and vertical centering */
π justify-content
Definition: Aligns the entire grid horizontally within its container (inline axis). Controls how the
grid as a whole is positioned when it doesn't fill the container's width. Different from justify-items which
aligns items within cells. Default Value: start Values: start | center | end | space-between | space-around | space-evenly
β¦ Value 1: justify-content: start
Aligns grid to the left side of container.
A
B
C
justify-content: start; /* Grid aligns to left */
β¦ Value 2: justify-content: center
Aligns grid to the center of container horizontally.
Definition: Aligns the entire grid vertically within its container (block axis). Controls how the
grid as a whole is positioned when it doesn't fill the container's height. Only works when the grid has less
height than the container. Different from align-items which aligns items within cells. Default Value: stretch Values: start | center | end | space-between | space-around | space-evenly | stretch
β¦ Value 1: align-content: start
Aligns grid to the top of container.
1
2
3
4
5
6
align-content: start; /* Grid aligns to top */
height: 300px; /* Container must be taller than grid */