Every heading, paragraph and picture on a page sits inside a box. Once you can see those boxes, spacing a page neatly gets much easier.
flag
What you'll learn
Why every element on a page is a box with four layers.
How to add space inside and outside a box with padding and margin.
How to draw borders and outlines, and how to set the width and height of a box.
waving_handLet's look at boxes
The browser puts every element in an invisible rectangle, called a box. Headings, paragraphs, pictures, even links each get one.
CSS lets you change four layers of every box. Together, those layers are called the box model.
filter_frames
Think of framed photos on a wall. The photo is the content. The paper around the photo, inside the frame, is the padding.
The wooden frame is the border. The empty wall that keeps two frames apart is the margin.
A chalk line drawn around a frame is like an outline: you can see it, but it takes up no wall space.
margin
border
padding
content
margin
outline
Now let's see the CSS ↓
margin
border
padding
content
margin
outline
From the inside out: content, padding, border, margin. Padding is inside the border, so it shows the box's background color. Margin is outside the border, so it stays see-through.
The same layers, measured
Here is one box drawn as plain colored layers, with the size of each layer on all four sides. Browsers show boxes this way in their developer tools.
margin1616
border33
padding1616
300 widecontent
1616
33
1616
content
padding
border
margin
All numbers are in pixels. The box itself is 3 + 16 + 300 + 16 + 3 = 338 pixels wide, and the margin keeps 16 more pixels of empty space around it.
The four layers in CSS
width: 300px;content size
padding: 16px;space inside
border: 3px solid;the line
margin: 16px;space outside
searchA closer look
One side or all four sides
padding: 16px; adds the same space on all four sides. To change one side, add the side's name: padding-top, padding-right, padding-bottom or padding-left. Margin works the same way.
With two values, like padding: 8px 16px;, the first is for the top and bottom, and the second is for the left and right.
With four values, they go around the box like a clock, starting at the top: top, right, bottom, left.
The three parts of a border
A border needs a thickness, a style and a color, like border: 3px solid #11141A;.
The style can be solid, dashed, dotted or double. The style is the part that makes the line appear, so a border without one stays hidden.
border-bottom draws a line on one side only, which is handy under headings.
Width and height
width and height set the size of the content area. Most of the time, it's best to leave height out, so the box grows to fit whatever is inside.
max-width is often better than width: the box can shrink on a small phone screen, and it stops growing at the number you set.
min-height works the other way: the box is at least that tall, and it still grows if it needs to.
How wide is the box really?
The width you set is only the content. Padding and border are added on top of it. A box with width: 300px;, padding: 16px; and a 3px border is 300 + 16 + 16 + 3 + 3 = 338 pixels wide. Margin then adds empty space around that.
Many websites add box-sizing: border-box;, which makes the width include the padding and border, so 300px stays 300px.
Centering a box
Give a box a width or max-width, then set margin-left: auto; and margin-right: auto;. The browser splits the leftover space evenly on both sides, so the box sits in the middle.
Outline or border?
An outline looks like a border, but it takes up no space, so nothing around it moves when you add it. Browsers use outlines to show which link or button is selected when you move around with the Tab key.
constructionLet's build it
Let's turn two plain Front Page Bytes stories into neat cards. Each story is a <div class="card"> box. Each code box opens on the CSS tab with the new lines highlighted. Click HTML to see the page being styled.
1Step 1: Start with two white boxes
Your code
body { background-color: #F0F7FF;}.card { background-color: white;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
A white background shows where each .card box is. Notice that the text touches the edges, and the two boxes almost touch each other. Let's fix that one layer at a time.
2Step 2: Add space inside: padding
Your code
body { background-color: #F0F7FF;}.card { background-color: white; padding: 16px;}
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
padding: 16px; adds 16 pixels of space inside each box, on all four sides. The white background fills the padding too, so the cards look roomier.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
border: 3px solid #11141A; draws a 3-pixel, solid, dark line around each box, just outside the padding.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
margin-bottom: 16px; adds empty space below each card, so they no longer touch. Margin is outside the border, so it shows the page's light blue, not white.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
max-width: 320px; keeps each card from getting wider than 320 pixels. margin-left: auto; and margin-right: auto; share the leftover space on both sides, so the cards sit in the middle.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card featured"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
In the HTML tab, the second card now also has the class featured. The new rule gives it a red dashed outline.
Compare the two cards: both are the same size, and nothing moved. That's the difference between an outline and a border.
sticky_note_2Quick notes
marginSpace outside the border
.card { margin: 16px; }
How to use
margin: 16px;The same space on all four sides.
margin: 10px 20px;10px on the top and bottom, 20px on the left and right.
margin-top / -right / -bottom / -leftSpace on one side only.
autoShares the leftover space. On the left and right, it centers a box that has a width.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
The text needs more room than 40px, but the box stays 40 pixels tall. The words spill out of the border and run into the card below.
tips_and_updatesTip: Leave height out so the box grows with its content, or use min-height instead.
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
The thickness and color are there, but there is no style like solid. Without a style, the browser draws no line at all.
tips_and_updatesTip: Add a style word, like solid: border: 3px solid #11141A;
<!DOCTYPE html><html lang="en"> <head> <title>Front Page Bytes</title> </head> <body> <div class="card"> <h2>New Phones Arrive This Week</h2> <p>Three new phones go on sale on Friday. Each one has a bigger battery.</p> </div> <div class="card"> <h2>Laptop Prices Drop</h2> <p>Many laptops cost less this month. Students can save up to ₱5,000.</p> </div> </body></html>
What you see
description(no title)
width: 100% makes the content as wide as the page. Then the padding and border are added on top, so each card is wider than the page and sticks out on the right.
tips_and_updatesTip: Use max-width instead, or add box-sizing: border-box; so the width includes the padding and border.
quizTest your knowledge
Edit the CSS on the left. Open the HTML tab to see the class names you can use. The mini browser updates as you type, and each goal gets a check mark when it's done.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Add padding inside the .weather boxes.not done yet
Give the .weather boxes a border.not done yet
Use margin-bottom to put space between the two boxes.not done yet
Limit how wide the boxes get with max-width.not done yet
Every element is a box with four layers, from the inside out: content, padding, border and margin.
Padding is space inside the border, and margin is space outside it. Both can be set for all four sides at once or for one side.
A border needs a thickness, a style and a color. An outline looks similar but takes no space. max-width and min-height set sizes that still fit any screen.
menu_bookReferences
Lesson content prepared by the WebCore F.E. team for beginner learners. The following references were used in preparing this lesson.
W3C. CSS Box Model Module Level 3 (official specification).
Open reference
W3C. CSS Backgrounds and Borders Module Level 3 (official specification).
Open reference
W3C. CSS Basic User Interface Module Level 4 (official specification, outline).
Open reference
LibreTexts. Web Site Design (2022, open textbook).
Open reference