HTML & CSS for Beginners Part 20: How to center a div

Kevin Powell · Beginner ·🌐 Frontend Engineering ·9y ago
Skills: HTML & CSS90%

Key Takeaways

This video tutorial by Kevin Powell covers the basics of HTML and CSS, specifically how to center a div container on a webpage using the margin: 0 auto trick, and also explores using percentages for dynamic width and max-width for limiting the size of the layout.

Full Transcript

alright guys super common problem a lot of you guys have websites and you're not sure how to keep something in the middle of it we want something like this instead of something like that and the other thing is when I change the size of the screen here we want it to adjust automatically I don't want to you know you can see in my margins here are changing so we're gonna look at how we can set that up to work properly on your site where it's always keeping the content in the middle and it's not keeping everything fullscreen like that [Music] luckily for us this is a really easy thing to do for this to work we have to add a little bit of HTML just one simple div to fix our problem and what we need to do is we have to have a div that's gonna hold all of our content in the middle of our page with websites we have nothing that defines the size of our page they scroll up and down pretty much to infinity as long as there's content we can scroll and there's no problems there but as far as the left and right goes you know you make a new document in lots of other things they ask us what we want for our dimensions we don't have that so it just fills up all the space so we have to create something that tells it what the dimensions are actually going to be so I'm gonna come in here and create a div class I'm gonna call it container another name you might hear or see people using is called wrapper and then all the way down to the bottom and close that and we'll just indent all this since we're all inside the container so some places you you might see wrapper that's doing the exact same thing as this container is really really popular though you'll see this used a lot for what we're about to do go over to our Styles and all we need to do over here dock container is I want to select my container and first time we gave it a background color just so we can visualize what's going on and I'll just make it white Save oh I forgot to save my HTML file there we go so my container is there and right now it's not doing anything yet it just has a white background to it and it's holding all of my content what I want is you next though is I want to give it a width so let's say my width is 600 pixels and now I have a white box that's 600 pixels and no matter what I do it's always 600 pixels the problem is it's stuck over on the left side over here it's not automatically adjusting to anything it's just it's staying on the left which makes sense HTML and stuff always starts on the left so I want to put it in the middle now this is where the little nice trick comes in and what it is is I want my margin left to be Auto and I want my margin right to the auto you can't use this trick anywhere you want this is a special thing it only really works when the left margin and the right margin or Auto you can't do this from margin top and margin bottom it's only margin left and margin right and what it's going to do is it's going to make sure the margin on the left and the margin on the right are always exactly the same so when i refresh voila it's in the middle and if I make this bigger it stays in the middle and if I make that smaller it stays in the middle so no matter what I do it's always in the middle so that's really fun and cool now we can take this a little bit of a step further one thing just to make it easier for you what you usually see this written as is margin zero space Auto so zero pixels top and bottom and auto left and right so just margin:0 auto this is the magic sauce this is what keeps your div or your container in the middle of the page so right away this is the big takeaway from this video a couple of other things we can do to fancy it up a little bit though right now my width is locked in at 600 pixels and that looks fine right now but it's really tiny over here so one thing you could do instead of putting pixels is I could use percentage so say I did 70% and this is what I used at the beginning of the video for when I was demoing it but we'd never wait back around so it's always going to be 70% of the total size so it's gonna grow and it's gonna shrink along with my window but these spaces are going to automatically adjust also so right now the spaces are still bigger and they get smaller yeah so that's a nice one there and the last last other thing that we will add into this is something called max width so my width is 70 pixels but I'm gonna say a max width of 1000 pixels so what this does it means I'm gonna save that and refresh so I have my width of 70% of the total screen it has my margin:0 auto to Center it but now if I'm on a small screen it's perfect and it's gonna go and go and you can see right here it stops growing this box has now hit a width of a thousand pixels so instead of being 70 percent we're saying it always be 70 but stop at a thousand so it can't get bigger than a thousand pixels so it will shrink down it will grow and then it will stop at a thousand pixels so we can sort of limit a little bit the total size of our layout and that's it nice and short and sweet thanks a ton for watching I hope you learned something with this one and I'll see you in the next video

Original Description

The last fundamental skill that we need before we can build a layout: Creating a container that can center our content inside the browser. This is a nice, short video that has a few nice tricks in it! --- Half Bit by Kevin MacLeod is licensed under a Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/) Source: http://incompetech.com/music/royalty-free/?keywords=%22half+bit%22 Artist: http://incompetech.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Kevin Powell · Kevin Powell · 22 of 60

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
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 center a div container on a webpage using HTML and CSS, and how to make the layout responsive using dynamic width and max-width. The tutorial covers the basics of HTML and CSS and provides a practical example of how to apply these concepts to a real-world problem.

Key Takeaways
  1. Create a new div container with a class
  2. Add a background color to the container for visualization
  3. Set the width of the container using pixels or percentages
  4. Use margin: 0 auto to center the container
  5. Add max-width to limit the size of the layout
  6. Test the layout on different screen sizes
💡 The margin: 0 auto trick is a simple and effective way to center a div container on a webpage, and using percentages for dynamic width and max-width can make the layout responsive and adaptable to different screen sizes.

Related AI Lessons

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →