From the course: CSS Toolkit: Tips and Techniques for Layout and Styling
Unlock this course with a free trial
Join today to access over 24,700 courses taught by industry experts.
Consistent sizing with box-sizing: border-box - CSS Tutorial
From the course: CSS Toolkit: Tips and Techniques for Layout and Styling
Consistent sizing with box-sizing: border-box
- [Instructor] Let's say that you have an element like this second paragraph here, and you decide you want to add some padding inside to give the content a little breathing room. So I could go in here and say, padding 1.5 rim, and as a result, the box grows. Why is this happening? Well, if you set either the width or the height of a box, which I've done here with this width, then whenever you add padding or a border or both, you're adding to the overall width of the content box. So the entire box grows. And CSS, you have to imagine a box around all elements, and then the padding goes outside of that box. That's because the default box sizing for all HTML elements is set to content box, meaning the box size is applied to the content inside the box. The same thing happens if we apply a border. So I'll say border, one rim solid purple, and our box grows again. If you're working with a specific design and want things to line up just so, this is probably not what you want. So to solve this…
Contents
-
-
-
-
The vw unit and the classic scrollbar problem4m 41s
-
(Locked)
Consistent sizing with box-sizing: border-box2m 23s
-
(Locked)
Using logical box properties vs. top, right, bottom, left4m 9s
-
(Locked)
Changing writing mode3m 51s
-
(Locked)
Align elements horizontally and vertically in any container4m 32s
-
(Locked)
Centering psuedo-elements with absolute positioning4m 11s
-
(Locked)
Maintaining aspect ratio in containers and images2m 32s
-
(Locked)
Using float and clear in modern CSS2m 27s
-
(Locked)
Wrap text around custom shapes with shape-outside2m 23s
-
-
-
-
-