The box model
Here's a secret that makes CSS layout make sense: every single element on a page is a rectangle. Headings, paragraphs, images, links, all of them are boxes. Even when something looks round, there's a rectangle around it.
Understanding how those boxes work is called the box model, and it's the key to controlling space on your pages.
The four layers of a box
Every box has four layers, from the inside out:
- Content is the text, image or whatever is inside
- Padding is the space between the content and the border
- Border is a line around the padding (it can also be invisible)
- Margin is the space outside the border, pushing other boxes away
A useful way to remember the difference: padding is inside the box, margin is outside it. If the box has a background color, the padding gets that color too, but the margin is always transparent.
.card {
padding: 24px;
border: 2px solid #dddddd;
margin: 16px;
background-color: white;
}You can see all four layers on any element. Open the developer tools with Inspect and look for the Computed tab. At the top there's a colorful diagram of the element's box, with each layer labeled. Hover over it and the browser highlights the layers on the page.
You want more space between the text inside a card and the card's border. Which property do you change?
Select one answer
Setting each side separately
Padding, margin and border can be set for each side on its own:
.box {
margin-top: 32px;
margin-bottom: 8px;
padding-left: 16px;
}Or with a shorthand that goes clockwise from the top, like a clock: top, right, bottom, left.
.box {
padding: 10px 20px 10px 20px; /* top right bottom left */
padding: 10px 20px; /* top and bottom, then left and right */
padding: 10px; /* all four sides */
}The two value version is the one you'll use most.
Borders
A border needs three things: a width, a style and a color.
.card {
border: 1px solid #dddddd;
border-radius: 12px;
}The style is usually solid, but dashed and dotted exist too. border-radius rounds the corners, and it works even without a visible border. Give a square image border-radius: 50% and it becomes a circle, which is perfect for a profile photo.
Make sizes behave: box-sizing
By default, if you give a box width: 300px and then add padding and a border, the box ends up wider than 300 pixels. The padding and border are added on top. That surprises everyone.
The fix is one rule that almost every developer puts at the top of their stylesheet:
*,
*::before,
*::after {
box-sizing: border-box;
}The * selector picks every element. With border-box, the width you set includes the padding and border, which is what you'd expect. Add it to your stylesheet and you'll never have to think about it again.
With box-sizing: border-box, an element has width: 200px and padding: 20px. How wide is it on the page?
Select one answer
Block and inline elements
Not every box behaves the same way. There are two main kinds:
- Block elements, like
p,h1,sectionanddiv, start on a new line and stretch across the full width available. - Inline elements, like
a,strongandspan, sit inside a line of text and are only as wide as their content.
Inline elements ignore width, height and top and bottom margin. If you need those on an inline element, like a link that should look like a button, change its display:
.button {
display: inline-block;
padding: 12px 24px;
}inline-block keeps it in the line but lets it have a size and padding like a block.
Centering a box
A classic question: how do you center something? For a block element with a width, set the left and right margins to auto:
main {
max-width: 800px;
margin: 0 auto;
}max-width means "at most 800 pixels, but smaller if the screen is smaller". The auto margins split the leftover space evenly on both sides, which centers the box. This is how most websites keep their content in a nice readable column in the middle of a wide screen.
What does margin: 0 auto do on a block element with a max-width?
Select one answer
Use the box model to give your page some breathing room:
- Add the
box-sizing: border-boxrule at the top ofstyles.css - Give your
mainamax-widtharound800pxand center it withmargin: 0 auto. Add some padding so the text doesn't touch the edges on small screens - Make your profile image round with
border-radius: 50% - Give each
sectionsome space below it withmargin-bottom - Style your contact form's submit button with padding, a background color and rounded corners
Open the Computed tab in the developer tools on a few elements and look at their box diagrams.