How to set max-columns using auto-fit or auto-fill @KevinPowell
How to set max-columns using auto-fit or auto-fill  @KevinPowell
Uploaded April 2025 | Updated December 2026, 3 hours ago
It takes a little bit of work, but we can relatively easily take control of CSS Grid’s auto-fit (or auto-fill) by using a `calc()` to help set an upper limit on how many columns it creates.

πŸ”— Links
βœ… The code from this video: codepen.io/kevinpowell/pen/GgRwqxJ
βœ… The second version using attr(): codepen.io/kevinpowell/pen/ZYEBEre
βœ… auto-fit and auto-fill explained: youtube.com/watch?v=bj0Z_GncIwY
βœ… I’ve got free and premium courses: kevinpowell.co/courses

⌚ Timestamps
00:00 - Introduction
00:50 - Basic setup with auto-fit
03:00 - Having to do some math to make it work
05:28 - Making it more readable
06:30 - Making it stack at smaller sizes
09:10 - Preventing overflow
12:54 - An improvement we can make with a new CSS feature

#css

βœ‰ Keep up to date with everything I'm up to kevinpowell.co/newsletter
πŸ’¬ Come hang out with other devs in my Discord Community discord.gg/nTYCvrK
⭐ Are you a beginner? HTML & CSS for absolute beginners is for you: learn.kevinpowell.co
πŸŽ“ Start writing CSS with confidence with CSS Demystified: [https://cssdemystified.com](https://cssdemystified.com/)
πŸš€ Already mastered CSS? Check out my advanced course, Beyond CSS: https://www.beyondcss.dev/

---

Help support my channel
πŸ‘¨β€πŸŽ“ Get a course: kevinpowell.co/courses
πŸ‘• Buy a shirt: cottonbureau.com/people/kevin-powell
πŸ’– Support me on Patreon: patreon.com/kevinpowell or through YT memberships: youtube.com/@KevinPowell/join

---

πŸ§‘β€πŸ’» My editor: VS Code - code.visualstudio.com

🌈 My theme: One Dark Pro Var Night
πŸ”€ My font: Cascadia Code

---

I'm on some other places on the internet too!

If you'd like a behind the scenes and previews of what's coming up on my YouTube channel:

Bluesky: https://bsky.app/profile/kevinpowell.co
Codepen: codepen.io/kevinpowell
Github: github.com/kevin-powell

---

And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!
How to set max-columns using auto-fit or auto-fillThree CSS patterns I use in every projectMost image resets are missing this declarationStop fighting with CSS, forever2 common CSS layout misconceptionsEasier Site-Wide Theming with Style QueriesThe heading selector Ive been waiting so long for is finally here! (sort of)They said theyd crush meThe future of CSS looks very differentCSS Working Group just killed Masonry:focus gets focus states wrongThis selector targets only external links
Kevin Powell |

How to set max-columns using auto-fit or auto-fill

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER