Mastering CSS Margin and Padding Property with example

  • css
  • 55 Views
  • 6 Min Read
  • 8 Jul 2024

Hi, developer! In this article, we understand the properties of CSS, like margin and padding. Understanding this is necessary for us to create any layout well. In this, we have explained it using simple examples, which will make it easier for you to understand.

 

So let's start.

 

 

CSS Margin

 

 CSS Margin

The CSS margin property is used to create space around elements. With margins, you can control the spacing between text, images, and other elements on your webpage. Margins can be applied uniformly on all sides or individually adjusted for the top, right, bottom, and left sides, offering flexibility in layout design.

 

  • margin-top: Defines the top margin of an element.
  • margin-right: Defines the right margin of an element.
  • margin-bottom: Defines the bottom margin of an element.
  • margin-left: Defines the left margin of an element.

 

Margin size can be specified in units such as pixels (px), percentages (%), centimeters (cm), inches (in), and more.

 

Setting the margin value to "auto" will automatically adjust the margin size to maintain equal spacing.

 

Negative values can also be applied to margins using a minus sign, as the margin property allows for negative values.

 

Syntax:

 

margin: size;

 

If the margin property has four values, they will be applied to the top, right, bottom, and left margins, respectively.

         
margin: 50px 10px 100px 50px;

 

  • margin-top = 50px ;
  • margin-right = 10px;
  • margin-bottom = 100px;
  • margin-left = 50px;

 

CSS margin example

 

When you define margins for all four sides of an element using CSS, you can think of it as starting at the top and moving clockwise around the element, just like the hands of a clock. This sequence corresponds to specifying the values for the top, right, bottom, and left margins, respectively. It's a helpful analogy to remember when setting margins in your CSS code.

 
<head>
    <style>
        h1 {
            margin: 50px 10px 100px 50px;
        }
    </style>
</head>

<body>
    <h1>The margin four value</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium, deleniti!</p>
</body>

 

CSS margin four value

 

If the margin property has three values, then the margin will be set to something like this.

         
margin: 10px 20px 50px;

 

  • margin-top = 10px ;
  • margin-right and margin-left = 20px;
  • margin-bottom = 50px;

 

If the margin property has two values, then the margin will be set to something like this.

         
margin: 100px 200px;

 

  • margin-top and margin-bottom = 100px ;
  • margin-right and margin-left = 200px;

 

If the margin property has one value, then the margin will be set to something like this.

         
margin: 230px;

 

  • margin-top, margin-right, margin-bottom, margin-left = 230px. 

 

This gives the same 230px value around the h1 tag. We had understood the previous example of a clock, so this is how it works: (top, right, bottom, and left) 230px will evenly space around the element.

 

Margin individual property example

 
<head>
    <style>
        .top {
            background-color: thistle;
            margin-top: 100px;
        }

        .right {
            background-color: thistle;
            margin-right: 100px;
        }

        .bottom {
            background-color: thistle;
            margin-bottom: 100px;
        }

        .left {
            background-color: thistle;
            margin-left: 150px;
        }
    </style>
</head>

<body>
    <h2 class="top">The margin top</h2>
    <h2 class="right">The margin right</h2>
    <h2 class="bottom">The margin bottom</h2>
    <h2 class="left">The margin left</h2>
</body>

 

CSS margin four side

 

Margin auto value example

 
<head>
    <style>
        div {
            height: 200px;
            width: 300px;
            background-color: thistle;
            margin: 100px auto;
        }
    </style>
</head>

<body>
    <div>
        <h1>The auto value</h1>
    </div>
</body>

 

CSS margin auto value

 

Margin negative value example

 

In this example, the first text will move to the left side because we put the value on the left side as negative. And its text of positive value will appear.

 
<head>
    <style>
        .n-value {
            height: 200px;
            margin-left: -3%;
            margin-top: -15px;
            background: thistle;
        }

        .p-value {
            height: 200px;
            background: thistle;
        }
    </style>
</head>

<body>
    <h1 class="n-value">The margin negative value</h1>
    <h1 class="p-value">The margin positive value</h1>
</body>

 

CSS margin negative value

 

 

 CSS Padding

 

CSS padding

 

CSS padding adds space between an element's content and it's border. It can be set for each side individually (top, right, bottom, left). 

 

  • padding-top: Defines the top padding of an element.
  • padding-right: Defines the right padding of an element.
  • padding-bottom: Defines the bottom padding of an element.
  • padding-left: Defines the left padding of an element.

 

You can give the padding size in pixels (px), percentages (%), centimeters (cm), inches (in), and more.

 

Syntax

 

{ padding: size; }

 

If the padding property has four values, then the padding will be set to something like this.

         
padding: 20px 0px 20px 50px;

 

  • padding-top = 20px ;
  • padding-right = 0px;
  • padding-bottom = 20px;
  • padding-left = 50px;
 
<head>
    <style>
        h1 {
            padding: 20px 0px 20px 50px;
            border: 1px solid steelblue;
        }
    </style>
</head>

<body>
    <h1>The padding four value</h1>
    <p>Lorem ipsum dolor sit amet consenter adipisicing elit. Laudantium, deleniti!</p>
</body>

 

CSS padding four value

 

If the padding property has three values, then the padding will be set to something like this.

         
padding: 20px 100px 50px;

 

  • padding-top = 20px ;
  • padding-right and padding-left = 100px;
  • padding-bottom = 50px;

 

If the padding property has two values, then the padding will be set to something like this.

         
padding: 50px 200px;

 

  • margin-top and margin-bottom = 100px ;
  • margin-right and margin-left = 200px;

 

If the padding property has one value, then the padding will be set to something like this.

         
padding: 100px;

 

  • padding-top, padding-right, padding-bottom, padding-left = 100px. 

 

This gives the same 100px value around the h1 tag. We had previously understood the example of a clock, so this is how it works (top, right, bottom, and left) 100px will evenly space around the element.

 

Padding all sides as an individual example

 
<head>
    <style>
        .top {
            background-color: thistle;
            padding-top: 100px;
        }

        .right {
            background-color: thistle;
            padding-right: 100px;
        }

        .bottom {
            background-color: thistle;
            padding-bottom: 100px;
        }

        .left {
            background-color: thistle;
            padding-left: 150px;
        }
    </style>
</head>

<body>
    <h1 class="top">The Padding top</h1>
    <h1 class="right">The padding right</h1>
    <h1 class="bottom">The padding bottom</h1>
    <h1 class="left">The padding left</h1>
</body>

 

CSS padding four side value

 

 

The difference between margin and padding

 

Difference between margin and padding
 
Margin: It creates space around an element, like the area around a picture frame. You can adjust how much space there is between an element and its surroundings.
 
Padding: This is the space inside an element, like the space inside a picture frame. You can control how much space there is between an element's content and its border.
 
Margin can be set to auto: You can let the browser automatically adjust the margin of an element.
 
A negative margin is possible: You can set a negative value for the margin, pulling an element closer to other elements.
 
Padding cannot be set to auto or negative: Padding doesn't have the flexibility of auto or negative values. It's always positive, creating space within the element.

 

Look at the example of margin and padding.

 
<head>
    <style>
        .margin-box {
            background-color: thistle;
            width: 20%;
            margin: 20px;
            height: 10vh;
        }

        .padding-box {
            height: 10vh;
            padding: 20px;
            background-color: thistle;
            width: 20%;
        }
    </style>
</head>

<body>
    <div class="margin-box"> The margin example </div>
    <div class="padding-box"> The padding example </div>
</body>

 

CSS margin and padding

 

 

How to Remove the Browser's Default Margin and Padding

 

When you create a web project, most browsers set the default margin and padding for the entire page. If you want to remove this margin and padding from that page, first write it in your CSS file.

   
* {
    margin: 0;
    padding: 0;
}

 

CSS margin

 

With a set margin of 0 

 

CSS margin

 

Without a set margin of 0 

 

 

How to view and change the margin and padding of any element in the browser

 

By staying in the browser, you can find the margin and padding of any element.

 

To do this, open your browser (that browser in which your project is open) and right-click on the project page, then select Inspect. On the top left, there will be an option for an element inside Inspect, click on that, and your project file will appear. You can see the style of any element in the file by clicking on it. There will be a box on its side, if you don't see the box, do a right-side scroll down, and hovering over it will show you the selected element margin on the browser itself.

 

CSS margin and padding

 

In this image, the element is selected, which will show the style of the element.

 

You can also change the padding and margin in that box by double-clicking on the size, but this will not change your CSS file, the change will be maintained until you refresh the page.

 

CSS margin and padding

 

Padding is selected in this box so that the padding is visible on the element in the browser.

 

Conclusion

 

To control the space between or around any layout on a website, we need to understand CSS margin and padding. Many developers are confused between these two properties, but as you practice, you will better understand them.

 

I hope you completely understand. If you have any questions about this topic or related to web development, you can ask them in the question box below.

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!

About Author

Username

Meet Soni ( meetsoni )

Software Engineer

Joined On 13 Feb 2024

More from Meet Soni

Top 10 React Carousel Component Libraries for 2024

react-js

9 Jul 2024

Discover the top 10 React carousel libraries for 2024. Choose the best carousel based on i....

What is the spread operator in JavaScript with example?

javascript

9 Jul 2024

Learn how we can easily manipulate arrays, strings, and objects with the help of the sprea....

Top 4 Ways Centering a Div or Text Inside a Div using CSS

css

4 May 2024

Learn 4 techniques to make it easier to center a div container both horizontally and verti....

20 Amazing CSS Properties You Need to know - Part 2

css

4 May 2024

Learn about such properties of CSS with which you can easily make your website more attrac....

20 Amazing CSS Properties You Need to know - Part 1

css

9 May 2024

Learn 10 CSS properties that many developers do not know about. By understanding these all....

30 Keyboard Shortcuts You Might Not Know for Windows

programming

4 May 2024

Learn 30 Windows shortcut keys that will make it easier to use the system. In this post, w....

Popular Posts from Code Mafias

10 Fun Websites for Stress Relief and Relaxation During Coding Breaks

programming

11 Nov 2024

Top 10 fun websites for coders to relax during breaks. Recharge with interactive games, ar....

Mastering HTML: Top 12 Unique HTML Tags with Examples

html

4 May 2024

Through this article, learn those essential HTML tags that many developers do not know abo....

Top 60 Eye-Catching Button Designs Users Can’t Wait to Click - With Source Code

ui-ux

11 Oct 2024

Discover 60 eye-catching button designs with source code, crafted with HTML and CSS to enh....

How to Upload Code to GitHub: Quick Steps and Detailed Instructions for Beginners

github

16 Sep 2024

In order to upload (push) your project to GitHub, it involves multiple steps that need to ....

How to install MongoDB on windows in 2024

mongodb

2 May 2024

MongoDB is a Database Management System based on NoSQL (Not Only SQL) and utilizes JSON-li....

How to Implement Undo Functionality for Deleting Items in Your React App

react-js

28 Sep 2024

Learn how to implement undo functionality for deleting items in React. Follow a step-by-st....