Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO 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 CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

W3.CSS Margins

Margins

Margins create space outside an element.

W3.CSS provides classes for adding 16px margins around or beside elements.

What is a Margin?

A margin is the space outside the border of an element.

Margin

The white space around this green element is its margin.

Example

<div class="w3-container w3-green w3-margin">
  <h3>Margin</h3>
  <p>The white space around this element is its margin.</p>
</div>
Try it Yourself »

Padding vs Margin

Padding creates space inside an element.

Margin creates space outside an element.

Padding: Space inside the blue element.


Margin: Space outside the blue element.

Example

<div class="w3-padding">Padding</div>

<div class="w3-margin">Margin</div>
Try it Yourself »

Margin Classes

All W3.CSS margin classes use 16px margins.

Class Description
w3-margin Adds 16px margin on all sides
w3-margin-top Adds 16px top margin
w3-margin-right Adds 16px right margin
w3-margin-bottom Adds 16px bottom margin
w3-margin-left Adds 16px left margin
w3-section Adds 16px top and bottom margin

Margin on All Sides

The w3-margin class adds a 16px margin on all sides.

I have a 16px margin on all sides.

Example

<div class="w3-container w3-green w3-margin">
  <p>I have a 16px margin on all sides.</p>
</div>
Try it Yourself »



Margins on Individual Sides

Use directional margin classes when you only need space on one side.

w3-margin-top.


w3-margin-bottom.


w3-margin-left.


w3-margin-right.

Example

<div class="w3-margin-top">Top margin</div>
<div class="w3-margin-right">Right margin</div>
<div class="w3-margin-bottom">Bottom margin</div>
<div class="w3-margin-left">Left margin</div>
Try it Yourself »

The Section Class

Many HTML elements do not have a default top or bottom margin. Such elements will display without a margin between them.

First Section

Second Section

The w3-section class adds 16px top and bottom margins.

It is useful for separating sections of content.

First Section

Second Section

Note

Without w3-section, the two elements touch.

Example

<div class="w3-container w3-blue w3-section">
  <h2>First Section</h2>
</div>

<div class="w3-container w3-green w3-section">
  <h2>Second Section</h2>
</div>
Try it Yourself »

Spacing Between Cards

Margins are useful when separate components need space between them.

Design

Create clean interfaces.

Develop

Build responsive websites.

Deploy

Publish your work.

Example

<div class="w3-card w3-container">...</div>

<div class="w3-card w3-container w3-margin-top">...</div>

<div class="w3-card w3-container w3-margin-top">...</div>
Try it Yourself »

What You Have Learned

  • Margins create space outside an element
  • Padding creates space inside an element
  • w3-margin adds 16px margin on all sides
  • The directional margin classes add 16px margin to one side
  • w3-section adds 16px top and bottom margins
  • Margins are useful for separating sections and components

More Examples

Explore more ways to use W3.CSS margins.


Top Margin

The w3-margin-top class adds a 16px top margin to an element:

Example

<div class="w3-container w3-margin-top">
  <p>I have a 16px top margin.</p>
</div>
Try it Yourself »

Bottom Margin

The w3-margin-bottom class adds a 16px bottom margin to an element:

Example

<div class="w3-container w3-margin-bottom">
  <p>I have a 16px bottom margin.</p>
</div>
Try it Yourself »

Left Margin

The w3-margin-left class adds a 16px left margin to an element:

Example

<div class="w3-container w3-margin-left">
  <p>I have a 16px left margin.</p>
</div>
Try it Yourself »

Right Margin

The w3-margin-right class adds a 16px right margin to an element:

Example

<div class="w3-container w3-margin-right">
  <p>I have a 16px right margin.</p>
</div>
Try it Yourself »

×

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-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.