Fixing when CSS Grid has extra spacing
Key Takeaways
Explains how to fix extra spacing issues in CSS Grid layouts using grid alignment techniques
Full Transcript
so I have this layout that's right here but this weird thing is happening where if I come in and I add it to this grid layout area which has these four sections in it when I create the layout here I'm getting all of this extra space and this is really weird right well my friend end friends this actually is exactly the situation that somebody in my Discord server recently ran into and I know this is a problem other people do too so I want to look at the solution for it and to really understand what's happening the first thing I'm going to do is add an outline here we're going to do three pixels solid and we'll do chartreuse as a color cuz I'm into the weird color names these days and we can see that it's taking up all this height here and the reason it's doing that is because we're actually inside of this main layout here where I have the nav on one side and this going here and my main layout this could either be flexbox or grid it really doesn't matter either one of those would cause this to happen where my nav because I've given it a height of 75 VH uh is is really big so this side is stretching to match and because it's stretching to match grid does this thing where if we don't have any defined if we go and take a look at our grid let's turn that one on you can see that those rows that it's generating right now are going to fill up the entire space because we're in the stretch mode so the grid is stretching and then the rows they're just Auto height so those that auto height is just going to fill that available space and then the content is going to be in there and then we end up with all this extra space coming in here and it can be definitely a little bit annoying and to be able to fix this luckily it's really really easy we can just come on here and it doesn't really matter where we declare it but I like doing it after my uh template rows and columns I'm going to come in with an align content of uh start I about to do Center but we want to do start and we're going to see it pulls everything up and align content is something that I think not enough people know about you can see if I turn that off we turn off this my layout starts going more to what I originally wanted and I technically could have done something similar in the main layout too to pull everything up but I want to look specifically at doing it on the layout where the issue was happening in the first place and why it's working cuz you can see now my my entire grid has moved like that if I didn't align items instead of align content what's happening is well nothing's actually changing aign items start is the default so I'm going to switch this over to a center and we're going to see that these get centered within their cells so with grid anytime we're using the items keyword we're talking about the positioning of the item within the cell that it's already in and it won't impact the size of that cell whereas if we use an align content it's affecting the grid itself not the element itself cuz that element is still stretching and it's still that entire height and it's going to make those rows as small as they can just because I haven't declared any rows anywhere so it's just the the intrinsic rows being created and they're getting now as small as they can and because I'm doing allign content center it's centering the grid itself within the parent and as we said we want to use a start here instead and it pulls it up and if you'd like to go more in depth in how all of this works the Align content align items uh justify content and just justify items I've done a in-depth video with looking at grid and understanding it all and sort of making that mental model of how it works to make sure we're really getting it and I've also covered all of those alignment and justification properties in flexbox as well cuz flexbox is similar but a little bit different so if either one of those worlds confuses you you can find those two videos right here for your viewing pleasure and with that I would like to thank my enablers of awesome Tim and Simon as well as all of my other patrons and channel members for their monthly support and of course until next time don't forget to make your corner the internet just a little bit more awesome
Original Description
Ever use grid, only for your rows to be way too tall, and you can't figure out why? It's happened to all of us, but luckily it's easy to fix!
🔗 Links
✅ Codepen from this video: https://codepen.io/kevinpowell/pen/zxOJQOV
✅ Join my Discord community: https://kevinpowell.co/discord
✅ In-depth on Grid alignmen: https://youtu.be/WvoYYok1Ddo
✅ In-depth on Flexbox alignment: https://youtu.be/uIzaU98Ak1k
Get a YT membership: Join this channel to get access to perks:
https://www.youtube.com/channel/UCJZv4d5rbIKd4QHMPkcABCw/join
⌚ Timestamps
00:00 - Introduction
00:30 - Why this happens
01:24 - How to fix it
#css
--
Come hang out with other dev's in my Discord Community
💬 https://discord.gg/nTYCvrK
Keep up to date with everything I'm up to
✉ https://www.kevinpowell.co/newsletter
Come hang out with me live every Monday on Twitch!
📺 https://www.twitch.tv/kevinpowellcss
---
Help support my channel
👨🎓 Get a course: https://www.kevinpowell.co/courses
👕 Buy a shirt: https://teespring.com/stores/making-the-internet-awesome
📽️ Join as a channel member: https://youtube.com/@KevinPowell/join
💖 Support me on Patreon: https://www.patreon.com/kevinpowell or through YT memberships: Join this channel to get access to perks:
https://www.youtube.com/channel/UCJZv4d5rbIKd4QHMPkcABCw/join
---
My editor: VS Code - https://code.visualstudio.com/
---
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, make sure to follow me on Instagram and Twitter.
Bluesky: https://bsky.app/profile/kevinpowell.co
Codepen: https://codepen.io/kevinpowell/
Github: https://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!
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Kevin Powell · Kevin Powell · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How to create an awesome navigation bar with HTML & CSS
Kevin Powell
Improve your CSS by Keepin' it DRY
Kevin Powell
HTML & CSS for Beginners Part 6: Images
Kevin Powell
HTML & CSS for Beginners Part 7: File Structure
Kevin Powell
HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
Kevin Powell
HTML & CSS for Beginners Part 5: Links
Kevin Powell
HTML & CSS for Beginners Part 3: Paragraphs and Headings
Kevin Powell
HTML and CSS for Beginners Part 1: Introduction to HTML
Kevin Powell
HTML and CSS for Beginners Part 2: Building your first web page!
Kevin Powell
HTML & CSS for Beginner Part 8: Introduction to CSS
Kevin Powell
HTML & CSS for Beginners Part 9: External CSS
Kevin Powell
HTML & CSS for Beginners Part 10: Divs & Spans
Kevin Powell
HTML & CSS for Beginners Part 11: Classes & IDs
Kevin Powell
HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
Kevin Powell
HTML & CSS for Beginners Part 13: Background Images
Kevin Powell
HTML & CSS for Beginners Part 14: Style Text with CSS
Kevin Powell
HTML & CSS for Beginners Part 15: How to style links
Kevin Powell
HTML & CSS for Beginners Part 16: CSS selectors and Specificity
Kevin Powell
HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
Kevin Powell
HTML & CSS for Beginners Part 18: How Floats and Clears work
Kevin Powell
HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
Kevin Powell
HTML & CSS for Beginners Part 20: How to center a div
Kevin Powell
HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
Kevin Powell
HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
Kevin Powell
How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
Kevin Powell
How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
Kevin Powell
How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
Kevin Powell
How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
Kevin Powell
How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
Kevin Powell
Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Kevin Powell
End of the year upate and what's coming to my channel to start the new year
Kevin Powell
Create a CSS only Mega Dropdown Menu
Kevin Powell
CSS Tutorial: Outline and Outline Offset
Kevin Powell
CSS Blending Modes
Kevin Powell
Parallax effect | 2 different ways to add it with jQuery
Kevin Powell
CSS Units: vh, vw, vmin, vmax #css #responsive #design
Kevin Powell
How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
Kevin Powell
100 Subscribers speed coding bonus video
Kevin Powell
How to Create a Website - Complete workflow | Part 02: The Markup #HTML
Kevin Powell
How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
Kevin Powell
How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
Kevin Powell
How to Create a Website - Complete workflow | Part 05: Typography & Buttons
Kevin Powell
How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
Kevin Powell
How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
Kevin Powell
Redesigning & Coding My Website #CreateICG
Kevin Powell
How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
Kevin Powell
How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
Kevin Powell
How to Create a Website - Complete workflow | Part 09: The CTA and Footer
Kevin Powell
How to Create a Website - Complete workflow | Part 10: Making it responsive
Kevin Powell
How to Create a Website - Complete workflow | Part 11: Making it responsive con't
Kevin Powell
How to Create a Website - Complete workflow | Part 12: Putting the site online
Kevin Powell
Create a Custom Grid System with CSS Calc() and Sass
Kevin Powell
CSS em and rem explained #CSS #responsive
Kevin Powell
Should you use Bootstrap?
Kevin Powell
How to add Smooth Scrolling to your one page website with jQuery
Kevin Powell
Let's learn Bootstrap 4
Kevin Powell
How I approach designing a website - my thought process
Kevin Powell
Build a website with Bootstrap 4 - Part 1: The setup
Kevin Powell
Build a website with Bootstrap 4 - Introduction
Kevin Powell
Build a website with Bootstrap 4 - Part 2: Customizing Variables
Kevin Powell
Related Reads
📰
📰
📰
📰
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Dev.to · Ayush Shekhar
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Dev.to · Alejandro
HTML Canvas Cheat Sheet for Pixel Art & Image Effects
Dev.to · lemon
Draw Pixel Art with Vanilla JS — A Step-by-Step Guide
Dev.to · lemon
Chapters (3)
Introduction
0:30
Why this happens
1:24
How to fix it
🎓
Tutor Explanation
DeepCamp AI