The Wayback Machine - https://web.archive.org/web/20241217192749/https://www.geeksforgeeks.org/css-margins-padding/
Open In App

CSS Margins

Last Updated : 03 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS margins are the invisible spaces around an element, separating it from its neighboring elements and the edges of the webpage (known as the viewport). These margins play an important role in controlling the layout and spacing of your webpage. In this guide, we’ll break down the key concepts of CSS margins, explaining what they are, how they work, and how to use them effectively in web design.

What is CSS Margin?

CSS margins create space around an element, pushing it away from other elements nearby. You can control the margin on each side of an element—top, right, bottom, and left—or use a shorthand method to set all margins at once. Margins can be defined using various units like pixels, percentages, and more. You can even set negative values, which pulls elements closer together.

layout_html

CSS Margin

Types of Margin Values

  • Pixels (px): The most common unit, specifying a fixed number of pixels.
  • Percentage (%): The margin is calculated as a percentage of the containing element’s width (for horizontal margins) or height (for vertical margins).
  • Other units: Less common units like em, rem, vh, and vw can also be used for relative sizing.
  • Auto: The browser calculates a suitable margin size, often used for centering elements.

Syntax

body {
    margin: value;
}

Margin Properties

Margin PropertyDescriptionExample
margin-topSets the top margin of an element.margin-top: 20px ;
margin-rightSets the right margin of an element.margin-right: 15px;
margin-bottomSpecifies the margin at the bottom of an element.margin-bottom: 30px;
margin-left:Determines the width of the margin on the left side.margin-left: 10px;
marginShorthand to set margins on all four sides.margin: 10px 20px;

Example of margin property with 4 values

margin: 40px 100px 120px 80px;
  • top margin = 40px
  • right margin = 100px
  • bottom margin = 120px
  • left margin = 80px

Example:  This example describes the margin property by specifying the four values.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            margin: 80px 100px 50px 80px;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
    <p> Margin properties </p>
</body>

</html>

Output:

Example of margin property with 3 values: 

margin: 40px 100px 120px; 
  • top = 40px
  • right and left = 100px
  • bottom = 120px

Example: This example describes the margin property by specifying the three values.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            margin: 80px 50px 100px;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
    <p> 
        Margin properties 
    </p>
</body>

</html>

Output:

Example of margin property with 2 values:

margin: 40px 100px; 
  • top and bottom = 40px;
  • left and right = 100px;

Example:  This example describes the margin property by specifying the double value.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            margin: 100px 150px;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
    <p>
        Margin properties
    </p>
</body>

</html>

Output:

Example of margin property with 1 value: 

margin: 40px; 
  • top, right, bottom and left = 40px

Example: This example describes the margin property by specifying the single value.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            margin: 100px 150px;
        }
    </style>
</head>

<body>
    <h1>
        GeeksforGeeks
    </h1>
    <p>
        Margin properties
    </p>
</body>

</html>

Output:

Understanding and effectively using CSS margins is a important skill for any web designer or developer. It not only enhances the visual appeal of the web layout but also improves the user experience. By mastering the use of margins, you can create more responsive and aesthetically pleasing web designs.

Supported Browsers

CSS Margins – FAQs

What are margins in CSS?

Margins in CSS are the spaces outside an element’s border, used to create space between the element and its surrounding elements.

What is margin auto in CSS?

margin: auto; is used to let the browser automatically calculate the margins. This is often used for centering elements horizontally within a container.

What is the shorthand property for margins?

The shorthand margin property sets all four margins in one declaration. For example, margin: 10px 20px 30px 40px; sets top, right, bottom, and left margins respectively.

How do you set equal margins on all sides?

Use the margin property with a single value. For example, margin: 20px; sets a 20px margin on all sides.

How do you set horizontal and vertical margins?

Use two values in the margin property. For example, margin: 10px 20px; sets a 10px vertical and 20px horizontal margin.



Next Article

Similar Reads

three90RightbarBannerImg