Box Model: CSS3 Box-Sizing & Calc()
What This Tutorial Covers
border-box Behavior
Width includes padding and border.
calc() Math
Mix percentages and pixels in width calculations.
Layout Predictability
Avoid the surprises of content-box sizing.
Noble Desktop's Web Design Certificate teaches Figma, HTML, CSS, and responsive design.
Delve into mobile and responsive web design with this in-depth tutorial, covering topics such as the CSS3 box model, box sizing, and the CSS3 calc() function. The tutorial also offers a detailed exercise that allows you to apply your knowledge and develop crucial skills.
Return to your code editor.
Add the following bold code to switch to the new CSS3 box model:
.primary,.secondary { float: left; width: 50%; padding: 40px; box-sizing: border-box; }The border-box value switches to a new box model, which applies the 50% width to the total width. Padding is added inside the box, instead of outside.
- Save the file.
- Reload your browser. The columns should once again be next to each other!
- The space between the divs is too large. This is because we set padding to 40px around each div, making a combined 80px center gutter. Let’s cut the center padding in half, so that it looks even all the way around.
- Return to your code editor.
We’ve already created some empty rules for you, so all you need to do is add the padding. Add the following code as shown below in bold:
.primary { padding-right: 20px; }.secondary { padding-left: 20px; }- Save the file.
- Reload your browser to see the spacing now looks even.
Return to your code editor.
