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:
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.
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.
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.
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.
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.
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.
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.
|