Simplify your CSS with these 3 grid layout solutions

Kevin Powell · Beginner ·🌐 Frontend Engineering ·3y ago

Key Takeaways

This video by Kevin Powell demonstrates the use of CSS grid to simplify layouts, including using grid template rows, grid template areas, and grid template columns to create responsive designs. The video also compares the use of Flexbox and Grid for layout solutions.

Full Transcript

CSS grid is often seen as this tool to create these big pieces of layouts with different pieces that can move around really easily and stuff like that and it is fantastic for that but there are very simple layout solutions that you can use it for as well so today we're going to be looking at three very awesome use cases for grid that are very simple and easy to set up and so let's jump right into it with this first example which is I will admit it's a bit of a bigger layout piece but it's something that I often see people coming up with more complex solutions for which is when we have a really short page and you need to have the footer stuck to the bottom I've seen people try all sorts of stuff for this and get frustrated by it so if we have the head the main and the footer right there and we want the footer to be on the bottom we could do this directly on the body but I know some people don't like putting grid directly on there so one solution for that is to have a div that just wraps all of the content in there and then you can choose that class could also put this directly on the body whatever approach you prefer now on the main layout I've given it a border right now just so we can visualize where it is on the page and as we can see it doesn't actually take up the full space that we need it to if we want to get that footer on the bottom one solution for that is to give a Min height of 100 VH people will say that VH is problematic which it can be on mobile so one alternative is to use dvh instead which is a newer unit that is a dynamic viewport height that will adjust automatically for those UI elements that can slide in and out on mobile because browser support isn't perfect for that one fallback is to do this where you'd have it with VH and dvh so browsers that don't understand dvh can use the VH browsers that do understand evh will use that instead the next step is to put a display grid on there and as you can see it now stretches all the elements out to fill up all the available space but it does leave us with the problem of the top of our header and footer being way too big we can solve this problem by using grid template rows Auto 1fr auto so the top row and the bottom row will match the height of the header end of our footer and the middle row gets 1fr to use up the rest of the available space and push that footer all the way to the bottom we can turn off our border now and we can see that it's working in this situation and if ever you're on a page that has a lot of content it's still going to work perfectly fine because we used a Min height rather than setting it at a fixed height another great use case for grid is stacking content so in my primary header I have this image here and we also have this H1 right here and I want to Stack these on top of each other and you might be thinking Kevin you could use a background image for that and you would be completely right but we'll look at one of the benefits of using grid to create Stacks like this the first thing we're going to do is take that primary header give it that display of grid and then give it a single grid template area I can then select all the direct children and place them on that grid area and as you can see the text is now on top of the image one problem is my text is sort of stuck at the top but we can fix that nice and easily with a place items Center one of the benefits of doing this is the image is actually in the markup meaning we can give it alt text which helps with accessibility and it also opens up more layout possibilities if we want to change things at different screen sizes for example if we remove our grid template areas and then replace the grid area here with a grid template columns then we can replace the grid area with a grid row and grid column that are just spanning the full size this has the exact same effect that we had before so you're probably asking what's the point by setting things up this way it's a little bit easier to set up a media query to change our layout at larger screen sizes so now it's small screen sizes it's stacked and then at larger screen sizes we can break into a two column layout now switching over to a new layout one place that I do see people using grid 4 regularly is on forms because it's a perfect use case for them but we can use grid within grid which is something that I don't see people doing as often as I think they could and one thing that's important here is in my form you can see I have labels and I have inputs and each one of those is wrapped inside of a form group jumping over to the CSS we can see the simple grid that I have set up for the Forum right now and just using my Dev tools I can turn on the visualizer for the grid and we can see just it's nice and simple to go from a 1 and 2 column grid and make things responsive but then inside each one of these cells we want to take advantage of the space that's there but then if we turn our attention inside those to the form groups we can use grid within there instead of the regular solution that I see because often what people do is on the label use a display block to get the consistency of having the label on the top and having the input on the bottom but we also have the problem now of the input not taking up all of the available space to it now we can solve that problem by using a width 100 and then it will fill up all the available space and of course we could add the text area at the bottom to have the same Solution on the bottom and then this does work well when we go between our different sizes but then we also have to worry about maybe the space in between and adding a margin somewhere and maybe there's other tweaks that you want to make as well so in my opinion it's very nice to delete all of that and then take advantage of the fact that we have a form group set up by putting a display grid on the form group we can do this where we get the exact same result we had before but we don't have to worry about the fact that we have different elements inside of them you can see my text area stretch the full width as well as my inputs and anything else that's in there would and the items are stacking on top of each other it's still nice and responsive and this is just happening because of the way that grid works on the children where they're now stretching to fill up that entire space the form group is taking up the whole cell and now the items inside are taking up all of the space within the form group now let's turn off that grid and we can turn on this visualizer for the one that's inside there and the reason I'm doing that is because now we can come in and add a gap to add spacing in between the elements if we need to add that space there instead of worrying about where we might be placing our margins or doing other things and of course it would open up the possibility of creating columns Within These if ever you needed to so using grid for these little micro layouts just to add a little bit of spacing to me is really handy and since we are looking at this form which is using a grid we might as well make it look a little bit better so in the HTML if we look at our form group that has our text area inside of it we can go and have something like a form group full width class added on there that we can then use to our advantage so now if I take a look at that that form group is taking up the entire thing because we're starting at grid line number one and going all the way to the negative one we saw the solution before with our stacked layout as well and just in case you're wondering what those numbers are referring to we can see that we have a one here a 2 here and a three here but at the bottom those numbers are reversed and we actually count in reverse order so a 1 over negative one just means go from the start to the end of our grid and so from Big layouts to small little layouts grid is fantastic at a lot of things and we can come up with very nice elegant Simple Solutions that give us a lot of control over our layouts and why I love grid a lot but that does not mean that we should discount flexbox both layout tools are very useful in different use cases and if you have trouble knowing which one you should use in which use case I have covered that in a previous video that you can check out right here and with that I'd like to thank my enablers of Awesome on patreon Jan Johnny Michael Patrick Simon and Tim as well as all my other patrons for their monthly support and of course until next time don't forget to make your corn on the internet just a little bit more awesome

Original Description

🔥Simple Grid Layouts Cheatsheet: https://simple-grid-layout.kevinpowell.co/ Grid is often seen as useful for really large layouts that have a lot of different pieces, but there are other simple things that it can be super handy for as well! 🔗 Links ✅ How to pick between Flexbox and Grid: https://youtu.be/3elGSZSWTbM ✅ Getting started with CSS Grid: https://youtube.com/playlist?list=PL4-IK0AVhVjM41-Ezm5tmESVchNEi7aZU ⌚ Timestamps 00:00 - Introduction 00:20 - Keeping the footer on the bottom of a short page 02:14 - Stacking content 03:30 - Small things that need some spacing #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 💖 Support me on Patreon: https://www.patreon.com/kevinpowell --- 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. Twitter: https://twitter.com/KevinJPowell 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 How to create an awesome navigation bar with HTML & CSS
How to create an awesome navigation bar with HTML & CSS
Kevin Powell
2 Improve your CSS by Keepin' it DRY
Improve your CSS by Keepin' it DRY
Kevin Powell
3 HTML & CSS for Beginners Part 6: Images
HTML & CSS for Beginners Part 6: Images
Kevin Powell
4 HTML & CSS for Beginners Part 7: File Structure
HTML & CSS for Beginners Part 7: File Structure
Kevin Powell
5 HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
Kevin Powell
6 HTML & CSS for Beginners Part 5: Links
HTML & CSS for Beginners Part 5: Links
Kevin Powell
7 HTML & CSS for Beginners Part 3: Paragraphs and Headings
HTML & CSS for Beginners Part 3: Paragraphs and Headings
Kevin Powell
8 HTML and CSS for Beginners Part 1: Introduction to HTML
HTML and CSS for Beginners Part 1: Introduction to HTML
Kevin Powell
9 HTML and CSS for Beginners Part 2: Building your first web page!
HTML and CSS for Beginners Part 2: Building your first web page!
Kevin Powell
10 HTML & CSS for Beginner Part 8: Introduction to CSS
HTML & CSS for Beginner Part 8: Introduction to CSS
Kevin Powell
11 HTML & CSS for Beginners Part 9: External CSS
HTML & CSS for Beginners Part 9: External CSS
Kevin Powell
12 HTML & CSS for Beginners Part 10: Divs & Spans
HTML & CSS for Beginners Part 10: Divs & Spans
Kevin Powell
13 HTML & CSS for Beginners Part 11: Classes & IDs
HTML & CSS for Beginners Part 11: Classes & IDs
Kevin Powell
14 HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
Kevin Powell
15 HTML & CSS for Beginners Part 13: Background Images
HTML & CSS for Beginners Part 13: Background Images
Kevin Powell
16 HTML & CSS for Beginners Part 14: Style Text with CSS
HTML & CSS for Beginners Part 14: Style Text with CSS
Kevin Powell
17 HTML & CSS for Beginners Part 15: How to style links
HTML & CSS for Beginners Part 15: How to style links
Kevin Powell
18 HTML & CSS for Beginners Part 16: CSS selectors and Specificity
HTML & CSS for Beginners Part 16: CSS selectors and Specificity
Kevin Powell
19 HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
Kevin Powell
20 HTML & CSS for Beginners Part 18: How Floats and Clears work
HTML & CSS for Beginners Part 18: How Floats and Clears work
Kevin Powell
21 HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
Kevin Powell
22 HTML & CSS for Beginners Part 20: How to center a div
HTML & CSS for Beginners Part 20: How to center a div
Kevin Powell
23 HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
Kevin Powell
24 HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
Kevin Powell
25 How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
Kevin Powell
26 How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
Kevin Powell
27 How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
Kevin Powell
28 How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
Kevin Powell
29 How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
Kevin Powell
30 Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Kevin Powell
31 End of the year upate and what's coming to my channel to start the new year
End of the year upate and what's coming to my channel to start the new year
Kevin Powell
32 Create a CSS only Mega Dropdown Menu
Create a CSS only Mega Dropdown Menu
Kevin Powell
33 CSS Tutorial: Outline and Outline Offset
CSS Tutorial: Outline and Outline Offset
Kevin Powell
34 CSS Blending Modes
CSS Blending Modes
Kevin Powell
35 Parallax effect | 2 different ways to add it with jQuery
Parallax effect | 2 different ways to add it with jQuery
Kevin Powell
36 CSS Units: vh, vw, vmin, vmax #css #responsive #design
CSS Units: vh, vw, vmin, vmax #css #responsive #design
Kevin Powell
37 How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
Kevin Powell
38 100 Subscribers speed coding bonus video
100 Subscribers speed coding bonus video
Kevin Powell
39 How to Create a Website - Complete workflow | Part 02: The Markup #HTML
How to Create a Website - Complete workflow | Part 02: The Markup #HTML
Kevin Powell
40 How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
Kevin Powell
41 How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
Kevin Powell
42 How to Create a Website - Complete workflow | Part 05: Typography & Buttons
How to Create a Website - Complete workflow | Part 05: Typography & Buttons
Kevin Powell
43 How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
Kevin Powell
44 How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
Kevin Powell
45 Redesigning & Coding My Website #CreateICG
Redesigning & Coding My Website #CreateICG
Kevin Powell
46 How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
Kevin Powell
47 How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
Kevin Powell
48 How to Create a Website - Complete workflow | Part 09: The CTA and Footer
How to Create a Website - Complete workflow | Part 09: The CTA and Footer
Kevin Powell
49 How to Create a Website - Complete workflow | Part 10: Making it responsive
How to Create a Website - Complete workflow | Part 10: Making it responsive
Kevin Powell
50 How to Create a Website - Complete workflow | Part 11: Making it responsive con't
How to Create a Website - Complete workflow | Part 11: Making it responsive con't
Kevin Powell
51 How to Create a Website - Complete workflow | Part 12: Putting the site online
How to Create a Website - Complete workflow | Part 12: Putting the site online
Kevin Powell
52 Create a Custom Grid System with CSS Calc() and Sass
Create a Custom Grid System with CSS Calc() and Sass
Kevin Powell
53 CSS em and rem explained #CSS #responsive
CSS em and rem explained #CSS #responsive
Kevin Powell
54 Should you use Bootstrap?
Should you use Bootstrap?
Kevin Powell
55 How to add Smooth Scrolling to your one page website with jQuery
How to add Smooth Scrolling to your one page website with jQuery
Kevin Powell
56 Let's learn Bootstrap 4
Let's learn Bootstrap 4
Kevin Powell
57 How I approach designing a website - my thought process
How I approach designing a website - my thought process
Kevin Powell
58 Build a website with Bootstrap 4 - Part 1: The setup
Build a website with Bootstrap 4 - Part 1: The setup
Kevin Powell
59 Build a website with Bootstrap 4 - Introduction
Build a website with Bootstrap 4 - Introduction
Kevin Powell
60 Build a website with Bootstrap 4 - Part 2:  Customizing Variables
Build a website with Bootstrap 4 - Part 2: Customizing Variables
Kevin Powell

This video teaches how to use CSS grid to simplify layouts and create responsive designs. It covers the basics of grid template rows, grid template areas, and grid template columns, as well as the use of Flexbox and Grid for layout solutions. By following the steps outlined in the video, viewers can create simple and effective layouts for their web applications.

Key Takeaways
  1. Wrap content in a div and set the grid layout on the div or body
  2. Give the main layout a Min height of 100 VH or use dvh for dynamic viewport height
  3. Put a display grid on the main layout
  4. Use grid template rows to match the height of the header and footer and push the footer to the bottom
  5. Give the primary header a display of grid and a single grid template area
  6. Set up a grid template for a form
  7. Use grid within grid to create responsive layouts
  8. Add a gap to add spacing between elements
  9. Use a full-width class to create full-width form groups
💡 Grid layout solutions can be used for both big and small layouts, and Flexbox and Grid are both useful layout tools with different use cases.

Related Reads

📰
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Learn how to streamline frontend deployment with a disciplined branching and release process
Medium · Programming
📰
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Learn how to implement a disciplined branching and release process for frontend deployment to improve production visibility
Medium · DevOps
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Learn how to prepare for a Frontend SDE-2 interview at Wayfair, including online assessments, machine coding, and system design.
Medium · Programming
📰
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Learn how HTMX rebuilt a React SPA in a week, replacing 2 years of maintenance work, and discover the benefits of this alternative approach
Medium · Programming

Chapters (4)

Introduction
0:20 Keeping the footer on the bottom of a short page
2:14 Stacking content
3:30 Small things that need some spacing
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →