box-sizing: border-box explained

Kevin Powell · Beginner ·🛠️ AI Tools & Apps ·7y ago
Skills: HTML & CSS80%

Key Takeaways

The video explains the CSS property box-sizing: border-box, its benefits, and how it simplifies element sizing by including padding and border in the width and height calculations. The speaker, Kevin Powell, also discusses best practices for applying this property to all elements using the universal selector or inheritance.

Full Transcript

so what's the deal with this box sizing border box that you see pretty much used everywhere ever no that's what we'll be looking at in this video if you new here my name is Kevin and here at my channel we learned how to make the web and have it look good while we're at it with weekly tips tricks and tutorials and on Fridays we try and do it in five minutes I haven't done a five minute Friday in a little while so let's just jump right into it though if you've been following me for any length of time or if you've followed any other tutorials or just seen CSS on other websites you've probably seen box-sizing border box and it's usually set to Universal selectors something like this but why does everyone do it it's you know what's the point of it well to understand box sizing we have to understand the box model so just a really quick refresher to make sure you're comfortable with that in CSS elements are made out of the content box itself as well as the padding the border and a margin and by default box sizing is set to content box so that means that when we give something a width or height we're not setting the size of the element we're setting the size of the content box so let's look at this here where I have my content box and I've given it a width of 500 pixels so you can see it is 500 pixels and I've put this brown box underneath is just acting like a scale I might as well zoom in this so it's not actually 500 pixels so zoomed in but it makes it a bit easier to see what's gonna happen on this content box if I come and add some padding of say 25 pixels and we're gonna see it's actually and yet wider than this box so it's now my 500 pixels plus 25 on this side plus 25 on that side so it's actually 50 pixels wider and if I come and add a border on here the border of let's say 5 pixels solid red just so we can see it and that's also adding to the total size of my box so now we're getting bigger and bigger so even if I take my padding off of that actually and we can see that it's still sticking off the side here because the border is giving it this extra width that's the default behavior and honestly it's a bit annoying having that behavior on there because if I'm gonna come down to my border box box here which is this one underneath obviously and it will set our box sizing to border box I still have a width of 500 pixels on here but now if I come and add padding of 25 pixels and hit save it's adding it inside by changing the box sizing to border box we're now including both the padding and the border in there as well and I'm only using widths now but Heights this the same thing would apply to it so here we can also come and add a border of 5 pixels solid red and hit save and that border is also on the inside instead of being on the outside so it's a nice way to the border box by resetting the box sizing on it it just makes things a lot easier to control and a lot more predictable you don't have to start doing math or adding things up not that it's difficult but it's just an extra layer of complexity that you don't really need so setting a width or setting a height and the instances where you need one and knowing that the padding or the border are just included in that just makes it so much easier and it's also important that you are doing it on the before and after because the universal selector by itself like this will not select su elements so just to make sure that it's applying to everything you'll probably want to do it like that and you might have also seen it done something like this where instead of being done like that you'll actually see here it's being set to work box sizing inherit and then what people do is on the HTML they set the box sizing to border box so it's always anything you create it's going to inherit you know everything is inheriting it and it's inheriting it from my HTML here so it's going to win and this is always going to take into account this and the reason you might see this come up is sometimes something might actually explicitly be set to this box sizing content box for some reason or maybe you're importing something from another project where the default was never changed and it screws up things you could explicitly set content box on that and even though I have this on a universal selector this is overwriting it so the children that would be in here and everything dealing with this would still be using the box sizing content box whereas the rest of my site would be using the border box so it's considered a little better practice it depends on your project whether you want to and I have put a link to a CSS tricks article down below if you do want a little bit more information on this specifically and that's it I hope you like this video I hope this enlightened you a little bit if you've been seeing box sizing and you did weren't really sure what it was doing I couldn't work without it or I could because I did for a long time but I'm so happy that we have this and it makes it just so much easier to work as a default so I hope you liked the video if you did please hit that thumbs up to let me know leave a comment down below if you have any comments or questions come and join us in the community there's a link to it in the description below it's a discord server come ask questions hang out just have a good time a big thank you to my patrons for helping support everything I do here if you don't know of a patreon it's a really cool service where you can support creators who give you lots of value so you can check that out there is a link to the in the description as well a big thank you to Lauren who's my supporter of awesome just thank you so much for the super generous support and I think that's it so until next time don't forget to make your quantum e internet just a little bit more awesome

Original Description

You probably see box-sizing: border-box used all over the place. I use it in my tutorials, it's used in most written and video content I see. In this video, I take a look at what border-box is, and why it's really awesome. CSS-Tricks article I mentioned in the video: https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/ --- Come and hangout, ask questions, suggest ideas, and meet some awesome people over in The Community: https://discord.gg/nTYCvrK I have a newsletter! https://www.kevinpowell.co/newsletter New to Sass, or want to step up your game with it? I've got a course just for you: https://www.kevinpowell.co/learn-sass --- My Code Editor: VS Code - https://code.visualstudio.com/ How my browser refreshes when I save: https://youtu.be/h24noHYsuGc --- Support me on Patreon: https://www.patreon.com/kevinpowell 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. Instagram: https://www.instagram.com/kevinpowell.co/ Twitter: https://twitter.com/KevinJPowell Codepen: https://codepen.io/kevinpowell/ Github: https://github.com/kevin-powell
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

The video teaches the benefits of using box-sizing: border-box in CSS to simplify element sizing and layout control. It explains how this property includes padding and border in width and height calculations, making it easier to design and develop web pages.

Key Takeaways
  1. Understand the box model and its components
  2. Apply box-sizing: border-box to elements using the universal selector
  3. Use inheritance to apply box-sizing: border-box to all elements
  4. Control element sizing by including padding and border in width and height calculations
💡 Using box-sizing: border-box simplifies element sizing and layout control by including padding and border in width and height calculations, making it a best practice for web development.

Related Reads

Up next
Your Competitors Are Using This: The AI SEO Toolkit for Brand Mentions
Matt Tutorials
Watch →