Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - CSS Spacing


CSS Spacing - Margins and Padding

Margins and padding control the spacing around and inside HTML elements. They help organize your layout and make your content easier to read.


What is Margin?

The margin is the space outside an element's border. It pushes the element away from its neighbors.

Example

Using margin:

div {
  margin: 20px;
  background-color: lightblue;
}
Try it Yourself »

This adds 20px of space on all sides of the element.


What is Padding?

The padding is the space inside an element's border. It pushes the content away from the border.

Example

Using padding:

div {
  padding: 20px;
  background-color: lightgreen;
  border: 2px solid black;
}
Try it Yourself »

This adds space between the element's content and its border.


Individual Sides

You can set margins and padding for each side separately:

  • margin-top
  • margin-right
  • margin-bottom
  • margin-left
  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

Example

Different values for each side:

div {
  margin-top: 10px;
  margin-right: 30px;
  margin-bottom: 20px;
  margin-left: 40px;

  padding-top: 10px;
  padding-right: 15px;
  padding-bottom: 10px;
  padding-left: 15px;
}
Try it Yourself »

Shorthand Properties

You can use shorthand syntax for both margin and padding.

Example

Shorthand margin and padding:

/* top, right, bottom, left */
div { margin: 10px 20px 30px 40px; }

/* vertical | horizontal */
p { padding: 10px 20px; }

/* all sides equal */
h1 { margin: 20px; padding: 10px; }
Try it Yourself »

Margin Auto

You can center block elements horizontally by setting margin: auto;.

Example

Centering an element:

div {
  width: 300px;
  margin: auto;
  border: 1px solid black;
  padding: 10px;
}
Try it Yourself »

Margin Collapse

When two vertical margins meet, they may collapse into a single margin (the larger of the two).

Example

Collapsing margins:

<h1 style="margin-bottom: 50px;">Heading</h1>
<p style="margin-top: 20px;">Paragraph</p>
Try it Yourself »

Note: Only vertical (top and bottom) margins can collapse. Padding never collapses.


Padding and Background

Padding adds space inside the element and expands the background color or image along with it.

Example

Background expands with padding:

div {
  background-color: lightcoral;
  padding: 20px;
}
Try it Yourself »

Best Practices

  • Use margins for outer spacing between elements.
  • Use padding for inner spacing within elements.
  • Use shorthand to simplify code.
  • Use margin: auto; for centering blocks horizontally.
  • Remember: only vertical margins can collapse.

Next, you'll learn about the CSS Box Model - how margins, borders, padding, and content work together to form every element on a web page.

Next » CSS Box Model


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.