box-sizing: border-box explained
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
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
More on: HTML & CSS
View skill →Related Reads
📰
📰
📰
📰
Sifting Through Existence, OR, Why You Can’t Trust AI with Your Bibliography
Medium · AI
Your Writing Gets Flagged as AI Because It’s Average, Not Because It’s AI
Medium · AI
15 AI Connectors Every DevOps Engineer Should Use in 2026
Medium · DevOps
Empowering Creators in the Age of Large Language Models
Medium · AI
🎓
Tutor Explanation
DeepCamp AI