CSS Grid Sizing
When working with CSS Grid, one of the most important things to learn is how to size your rows and columns. Let’s go through the different ways you can do this step by step.
πΉ 1. Fixed Size (px / rem)
You can define row and column sizes using fixed units like px or rem.
-
First row → 100px high
-
Second row → 200px high
-
First column → 400px wide
-
Second column → 800px wide
π The problem with fixed sizes is that they are not responsive. No matter how small or large the screen is, they won’t adjust.
πΉ 2. Auto
The auto keyword makes the grid adapt automatically.
-
For columns →
autotakes up all the remaining available width. -
For rows →
autofits the height to the content.
π Column auto = responsive width
π Row auto = height depends on the content
πΉ 3. Fractional Unit (fr)
The fr unit defines flexible ratios.
-
Second row will always be twice as tall as the first one.
-
Second column will always be twice as wide as the first one.
π This is one of the best ways to build responsive layouts.
πΉ 4. Minmax()
The minmax(min, max) function allows you to set minimum and maximum limits for grid tracks.
-
The second column will never be smaller than 400px
-
It will never grow larger than 800px
π Perfect when you want responsiveness but within limits.
πΉ 5. Repeat()
Instead of writing the same values multiple times, you can use repeat().
This is the same as writing:
π Saves time and keeps your code clean.
πΉ 6. Grid-auto-rows / Grid-auto-columns
If you define fewer rows/columns than you have items, CSS Grid automatically creates new ones.
By default, these rows/columns adjust based on content. But you can control them:
π Any new row that goes beyond your template will now be 300px high.
πΉ 7. Debugging with Chrome DevTools
When you set display: grid on a container, Chrome DevTools shows a grid overlay.
From there, you can:
-
See row & column sizes
-
Display line numbers
-
Extend grid lines
-
Change overlay colors
π Super useful for debugging grid layouts.
✅ Summary
-
Fixed size → Not responsive
-
Auto → Columns fill space, rows fit content
-
fr → Ratio-based responsive sizing
-
minmax() → Responsive within limits
-
repeat() → Cleaner code, avoids repetition
-
grid-auto → Controls extra rows/columns

Comments
Post a Comment