Javatpoint Logo
Javatpoint Logo

CSS Margin 0 Auto

We will discuss the CSS margin in this article.

CSS Margin

The CSS margin is the CSS property that is utilized to create space outside the border of the element. The "margin" property can have one value, two values, three values, or four values.

When the margin has one value:

It sets the equal margins on all sides.

Syntax:

When the margin has three values:

Its first value sets the top margin.

Its second value sets the equal right & left margins.

Its third value sets the bottom margin.

Syntax:

When the margin has four values:

It is the shorthand property that can be utilized to set different margins on all sides.

Syntax:

In CSS, there are individual properties for margin. We can set the margin individually for individual sides, i.e., top, bottom, left, and right.

Margin-top

It is utilized to set the margin from the top.

Syntax:

Margin-right

It is utilized to set the margin from the right.

Syntax:

Margin-bottom

It is utilized to set the margin from the bottom.

Syntax:

Margin-left

It is utilized to set the margin from the left.

Syntax:

margin-left: length;

Auto Margin

It is utilized to set the element horizontally centered.

Syntax:

Illustrations of the CSS Margin

Let us understand the CSS margin with the help of the demonstrations.

Illustration 1:

We will utilize the CSS margin property and give it one value, two values, three values, or four values.

Code:

Output:

Here is the output where we can witness the utilization of the CSS margin property.

CSS Margin 0 Auto

Illustration 2:

We will fix the margin of distinct sides utilizing applying the individual CSS margin property.

Code:

Output:

We can witness the gap on different sides of unique elements in the output downwards with the utilization of the individual CSS margin property.

CSS Margin 0 Auto

Illustration 3:

In this demo, we will learn to utilize the CSS margin property by setting its value to "auto. "

Code:

Output:

We can witness the utilization of the CSS margin to "auto" in the output provided downwards.

CSS Margin 0 Auto

margin: 0 auto

It keeps the 0 margin at the top & bottom and auto margin at the right and left.

Illustrations of the CSS Margin when the Value is Set to 0 and auto:

We will understand what happens when the margin value is 0 and auto.

Illustration 1:

We will comprehend the CSS margin property when the value is assigned to 0 and auto.

Code:

Output:

Here is the output in which we can witness the <div> element horizontally centered utilizing the CSS margin property.

CSS Margin 0 Auto

Illustration 2:

We will be setting the margin of a table to 0 and auto.

Code:

Output:

Here is the output in which we can witness the table centered horizontally utilizing the CSS margin property.

CSS Margin 0 Auto

Illustration 3:

We will be setting the margin of the list to 0 and auto.

Code:

Output:

Here is the output in which we can witness the list centered horizontally utilizing the CSS margin property.

CSS Margin 0 Auto

Browsers Support

Following are the browsers that support the CSS margin property:

  • Microsoft Edge
  • Opera
  • Safari
  • Google Chrome
  • Firefox

Conclusion

In this article, we have understood the CSS margin 0 auto. The 0 and auto are the values that are given to the CSS margin property to center the HTML element horizontally. We have understood the CSS margin property with the help of demonstrations.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA