Css Margins
Css Margins
CSS Margins
Margins are used to create space around elements, outside of any defined borders.
The CSS margin properties are used to create space around elements, outside of any defined
borders.
With CSS, you have full control over the margins. There are properties for setting the margin for
each side of an element (top, right, bottom, and left).
CSS Margins
All the margin properties can have the following values:
auto - the browser calculates the margin
inherit - specifies that the margin should be inherited from the parent element
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of
80px.</div>
</body>
</html>
Margin - Shorthand Property
To shorten the code, it is possible to specify all the margin properties in one property.
The margin property is a shorthand property for the following individual margin properties:
margin-top
margin-right
margin-bottom
margin-left
So, here is how it works:
<div>This div element has a top margin of 25px, a right margin of 50px, a bottom margin of
75px, and a left margin of 100px.</div>
</body>
</html>
The auto Value
You can set the margin property to auto to horizontally center the element within its container.
The element will then take up the specified width, and the remaining space will be split equally
between the left and right margins.
The inherit Value
<style>
div {
border: 1px solid red;
margin-left: 100px;
}
p.ex1 {
margin-left: inherit;
}
</style>
</head>
<body>
<div>
<p class="ex1">This paragraph has an inherited left margin
(from the div element).</p>
</div>