Margin - The margin in CSS
The margin
feature in CSS is used to create spaces around the element, outside the borders. These properties will set the edge of each element (top, bottom, left, right).
The edges of the margin in CSS
CSS has properties defining margins for each edge in an element:
- margin-top
- margin-right
- margin-bottom
- margin-left
All margin properties can have the following values:
- auto - browser calculates the margin
- length - determines the margin according to px, pt, cm .
- % - determines the margin in% of the width of the element
- inherit - determine the margin inherited from the parent element
Negative values can be used. The following example creates different margins for the 4 sides of the element
:
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
Shortened feature of margin in CSS
To shorten the code, it is possible to include all properties in a single margin property, with its own properties:
- margin-top
- margin-right
- margin-bottom
- margin-left
If margin has 4 values
margin: 25px 50px 75px 100px;
- 25px upper margin
- right margin 50px
- margin below 75px
- left margin 100px
p {
margin: 25px 50px 75px 100px;
}
If the margin feature has 3 values
margin: 25px 50px 75px;
- 25px upper margin
- left and right margins 50px
- margin below 75px
p {
margin: 25px 50px 75px;
}
If margin has 2 values
margin: 25px 50px;
- upper margin and below 25px
- left and right margins 50px
p {
margin: 25px 50px;
}
If the margin property has 1 value
margin: 25px;
- All margins are 25px
p {
margin: 25px;
}
Default value
When setting the margin
property to auto,
value auto,
it will automatically center the element horizontally. The element occupies the predetermined width, the rest will be divided equally for the left and right margins.
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
Value inherit (inherited)
This example sets the left margin of the word section
class = 'ex1'> inherits from the parent element
div {
border: 1px solid red;
margin-left: 100px;
}
p.ex1 {
margin-left: inherit;
}
Collapse Margin
Sometimes the upper and lower margins of the elements overlap into a single margin equal to the size of the larger margin. This only happens with the top and bottom margins, not with the left and right margins.
h1 {
margin: 0 0 50px 0;
}
h2 {
margin: 20px 0 0 0;
}
In the above example, the lower margin of
is 50px and the upper margin of
is 20px. Usually the middle margin
and
will be the sum of both (i.e. 70px = 50px + 20px) but due to the alignment, this margin is only 50px.
Last lesson: Contour in CSS
The following article: Padding in CSS
You should read it
- Text file format
- The printer has black edges of paper, black 2 margins - Causes and solutions
- Form - Form in CSS
- The numbering of pages in Word follows a symmetric parity pattern similar to a printed book
- Box Model (Box Model) in CSS
- Guidelines alignment in Word
- Role of Section in text presentation
- Style HTML with CSS
May be interested
- Padding in CSSthe css padding feature is used to create space around the content of the element and within the borders in css.
- Height and width in CSSheight and width properties are used to set the height and width of elements in css. they can get the following values:
- Box Model (Box Model) in CSSelements in html can be considered boxes. in css, the term 'box model' is used to refer to design and layout.
- Outline in CSSoutline is a line around the element, located outside the border to highlight the element.
- Text - Text in CSSthe article explains how to format text in css.
- Tooltip creation tools are useful with CSSthere are some really interesting tooltip designs and designs that you can create with css using the tooltip script. let's find out through the following article!