Understanding Width Height Margin and Padding
Learn how width, height, margin, and padding define element dimensions and spacing.
Understanding Width Height Margin and Padding is a free CSS Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.
The Box Model Concept
Every HTML element is rendered as a rectangular box. The CSS box model describes the four layers that make up this box from inside out: content → padding → border → margin.
width and height
width and height set the dimensions of the content area. By default, padding and border are added on top of this size.
<div class="box"></div>
padding
padding is transparent space inside the border, between the content and the border edge. It is part of the element's clickable/background-painted area.
<div class="card">Card content</div>
Padding Longhands
Each side can be set individually:
<div class="element">Padded element</div>
margin
margin is transparent space outside the border. It pushes other elements away. Negative margins pull elements closer or overlap them.
<div class="section">Centered section with bottom margin</div>
Margin Collapse
Vertical margins collapse between adjacent block elements: the actual gap is the larger of the two margins, not their sum. Horizontal margins never collapse.
<h2>Heading</h2>
<p>Paragraph below — margins collapse to 30px, not 50px.</p>
Preventing Margin Collapse
Margin collapse is prevented when:
- Elements are flex or grid children
- A parent has
overflow: hidden - A parent has
paddingbetween parent and first child - An inline element separates the siblings
auto Margins
Setting horizontal margins to auto on a block element with a fixed width centers it within its container. This is the classic centering technique.
<div class="centered">Centered block, max 800px wide</div>
min-width max-width min-height max-height
Constrain dimensions with minimum and maximum bounds to keep elements readable across screen sizes:
<div class="content">Readable content column capped at 65 characters wide.</div>
Calculating Total Element Size
In the default content-box model, total width = width + padding-left + padding-right + border-left + border-right. This can be surprising and is why border-box is preferred.
Logical Margin and Padding Properties
Modern CSS offers logical equivalents: margin-block, margin-inline, padding-block-start, etc. These adapt to writing direction automatically.
<div class="card">Logical padding-block/inline</div>
Quick Check
Which layers are included in a block element's total rendered width in the default box model?
Recap
The box model layers are content, padding, border, and margin. Padding is inside the border; margin is outside. Vertical margins collapse between adjacent siblings. Use margin: 0 auto with a fixed width to center block elements.
Learn CSS with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 40
- Lessons
- 159
Frequently Asked Questions
Is the “Understanding Width Height Margin and Padding” lesson free?
Yes — the full text of “Understanding Width Height Margin and Padding” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO. The CSS Academy course includes 4 lessons in total.
What will I learn in “Understanding Width Height Margin and Padding”?
Learn how width, height, margin, and padding define element dimensions and spacing. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CSS Academy?
No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Understanding Width Height Margin and Padding” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CSS Academy lesson?
Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding Width Height Margin and Padding
- Border Styles Width and Color
- Box-Sizing: Content-Box vs Border-Box
- Outline and Box-Shadow