LESSON 7.3
Margin, Padding, and White Space
The two CSS properties that control spacing
Learn how padding and margin create white space in your layouts, and how to use Chrome DevTools to inspect spacing.
💡 The Concept
White Space
White space (also called white space) is the empty, unmarked, or blank area between and around the design elements in a layout: text, images, buttons, and everything else. It improves readability, creates balance, and highlights key content.
White space is not always white. While often referred to as “white space,” it can be any color or even a background image. What matters is that it gives elements room to breathe.
- Improved Readability: Better spacing between lines and paragraphs allows for faster scanning.
- Increased Focus: Draws attention to specific, important elements.
- Good Alignment: Creates a cleaner and more organized look.
Apple always does a great job with white space. See the screenshot below for an example of how powerful white space can be.

The padding Property
Padding goes inside the border, between the content and the border. The simplest approach applies the same padding to all four sides at once:
img {
border: 5px solid purple;
padding: 20px;
}

You can also target each side independently:
img {
padding-top: 5px;
padding-right: 10px;
padding-bottom: 3px;
padding-left: 30px;
}
Padding Shorthand
The shorthand sets all four sides in one property. The values go clockwise starting at the top: top, right, bottom, left.
/* top | right | bottom | left */
img {
padding: 5px 10px 3px 30px;
}
/* Two values: top/bottom | left/right */
img {
padding: 10px 20px;
}
You can set padding using pixels (px), ems (em), or percent (%). Percent is especially useful for mobile design because the spacing scales with the screen width.
The margin Property
Margin adds space outside the border, pushing other elements away. Unlike padding, margin is always transparent and never takes on the element’s background color.
p {
margin: 20px;
}
You can set each side independently:
p {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
}
Margin Shorthand
Like padding, margin uses the same clockwise shorthand: top, right, bottom, left.
/* top | right | bottom | left */
p {
margin: 10px 20px 10px 20px;
}
/* Two values: top/bottom | left/right */
p {
margin: 10px 20px;
}
/* All four sides the same */
p {
margin: 10px;
}
Centering with margin: 0 auto
Setting left and right margins to auto is a classic way to center a block element horizontally. The browser splits the available space evenly on both sides:
img {
display: block;
margin: 0 auto;
}
The element needs a set width for this to work, and it must be a block element. Use display: block on elements like <img> that are inline by default.
Inspecting Margin and Padding in Chrome
As a web developer, you will spend a lot of time using Chrome Developer Tools to track down spacing issues. Right-click any element, choose Inspect, and hover over the HTML in the Elements panel. Chrome highlights padding in green and margin in salmon (a light peach color) directly on the page.

🎬 Video Demonstration
This video demonstrates how to set padding and margin on elements.
Exercise: Try It Yourself
Step 1. Open JSFiddle and paste this into the HTML panel:
<h1>Spacing Demo</h1>
<img src="https://webdevstudents.com/wp-content/uploads/2025/09/axylotl.png" alt="pink axolotl in a tank" width="100" height="100">
Step 2. Paste this into the CSS panel:
img {
background-color: lightblue;
border: 5px solid steelblue;
}
Step 3. Add padding: 20px to img. Watch the space grow inside the border.
Step 4. Add margin: 30px to img. Watch the space push other elements away. Your JSFiddle now looks like the screenshot below. Note the lightblue background color shows the padding.

Step 5. Now try centering the image. Add display: block to img and change the margin to 0 auto. Images are inline by default, so display: block is needed for centering to work.

❓ Frequently Asked Questions
They serve different purposes. Padding is space inside the border and takes on the element’s background color, making it part of the element’s visual design. Margin is space outside the border and is always transparent, pushing other elements away. You need both because sometimes you want spacing that is part of an element’s appearance (such as padding inside a button), and sometimes you want spacing that separates elements from each other without changing how they look.
Three key differences. First, background color: padding is filled with the element’s background color, while margin is always transparent. Second, borders: padding sits inside the border and margin sits outside it. Third, collapsing: vertical margins between two block elements can merge into one (the larger of the two wins), but padding never collapses. This margin collapsing behavior is one of the most common sources of layout surprises for new developers.
Above the fold refers to the content visible without scrolling. The tension is real: generous white space improves readability and draws attention to what matters, but too much of it can push important content out of view. The key is hierarchy. Use white space to frame and highlight your most important message, not to decorate the page. If your main call to action is hidden below the fold because of excessive spacing, the spacing is working against you. The goal is strategic spacing that guides the eye to what matters most.
Yes. Padding and margin can be applied to almost any HTML element: paragraphs, headings, images, divs, buttons, and more. Inline elements like links and spans accept left and right padding and margin, but top and bottom padding and margin behave differently and may not push surrounding content the way you expect. For full control over all four sides, use block or inline-block elements.
Background color fills the content area and the padding, but not the margin. The margin is always transparent, so it shows whatever is behind the element (usually the page background). This is why adding a background color to an element makes the padding visible as a colored band around the content, while the margin remains invisible.
★ Key Takeaways
Padding and margin are the two main tools for adding space in CSS. Padding goes inside the border; margin goes outside it. Together they create the white space that makes layouts easier to read and more visually balanced. In the next lesson you will see how these properties fit into the CSS Box Model.
Google Ads
The costs of this website are partially offset by revenue from Google Ads.Tools of the Trade
HTML/CSS Lessons
-
-
-
1.3. HTML Tag Preview
-
-
-
2.2. The HTML Framework
-
-
2.4. HTML Headings
-
-
-
-
-
-
3.01. Introduction to CSS
-
3.02. How CSS Cascades
-
-
-
-
3.05. CSS Colors
-
3.06. RGBA Colors
-
-
3.08. Font and Text Properties
-
3.09. Classes and IDs
-
-
-
-
-
4.3. HTML Embedded Images
-
-
-
-
-
-
5.1. CSS Borders
-
5.2. CSS Border Radius
-
5.3. CSS Gradients
-
5.4. CSS Box Shadows
-
5.5. CSS Text Shadows
-
7.1. The Display Property
-
-
-
7.4. The Box Model
-
-
-
-
-
-
9.1. Website Tables
-
9.2. HTML Forms
-
-
9.4. Special Form Fields
-
9.5. Embedding Multimedia
-
-
9.7. Keyboard Shortcuts
-
10.1. What Is a Framework?
-
10.2. Font Awesome Icons
-
-
10.4. Bootstrap Grid System
-
-
10.6. Bootstrap Navigation
-
-
11.2. Purchasing a Domain Name
-
11.3. Website Hosting
-
11.4. Nameservers and DNS
-
11.5. FTP
-
11.6. GitHub Pages
