Margin - Individual Sides:-
CSS has the property for specifying the margin for each side of an element.
- margin_top
- margin-left
- margin-right
- margin-bottom
All the margin properties can have the following values:-
- auto - the browser calculates the margin
- length - specifies a margin in px, pt, cm, etc.
- % - specifies a margin in % .
- inherit - specifies that the margin should be inherited from the parent element
Note: Negative values are allowed.
The following example sets margins for all sides of a <p> element:-
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;}
Example:-
p {
margin: 100px 150px 100px 80px;}
The following example sets margins for all sides of a <p> element:-
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;}
Shorthand Property:-
You can write margin property in one line to short the code, You can write the margin property for the following individual property.
margin-top , margin-right, margin-bottom, margin-left
Example:-
p {
margin: 100px 150px 100px 80px;}
Auto Value:-
The auto property is used 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.
div {
width: 300px;
margin: auto;
border: 1px solid red;}
width: 300px;
margin: auto;
border: 1px solid red;}
inherit Value:-
This example lets the left margin be inherited from the parent element.
div.Header {
border: 1px solid red;
margin-left: 100px;}
p.first {
margin-left: inherit;}
border: 1px solid red;
margin-left: 100px;}
p.first {
margin-left: inherit;}
Margin Collapse:-
Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.This does not happen on left and right margins? Only top and bottom margins?
Look at the following example:-
h1 {
margin: 0 0 50px 0;}
h2 {
margin: 20px 0 0 0;}
margin: 0 0 50px 0;}
h2 {
margin: 20px 0 0 0;}
All CSS Margin Properties:-
Property | Description |
---|---|
margin | A shorthand property for setting the margin properties in one declaration |
margin-bottom | Sets the bottom margin of an element |
margin-left | Sets the left margin of an element |
margin-right | Sets the right margin of an element |
margin-top | Sets the top margin of an element |
0 comments:
Post a Comment
Thanks