- W3.CSS Margins
- W3.CSS Margin Classes
- Margin
- Example
- Margin Top
- Example
- Margin Bottom
- Example
- Margin Left
- Example
- Margin Right
- Example
- Sections
- I am Blue
- I am Green
- CSS margin Property
- Definition and Usage
- Browser Support
- CSS Syntax
- Property Values
- Margin Collapse
- Example
- More Examples
- Example
- Example
- Example
- CSS Margins
- CSS Margins
- Margin — Individual Sides
- Example
- Margin — Shorthand Property
- Example
- Example
- Example
- Example
- The auto Value
- Example
- The inherit Value
- Example
- All CSS Margin Properties
- margin
- Интерактивный пример
- Составные свойства
- Синтаксис
- Значения
- Формальное определение
W3.CSS Margins
The w3-margin class adds 16px margin to all sides of an element.
W3.CSS Margin Classes
W3.CSS provides the following margin classes:
Class | Defines |
---|---|
w3-margin | Adds a 16px margin to all sides of an element |
w3-margin-top | Adds a 16px top margin to an element |
w3-margin-right | Adds a 16px right margin to an element |
w3-margin-bottom | Adds a 16px bottom margin to an element |
w3-margin-left | Adds a 16px left margin to an element |
w3-section | Adds a 16px top and bottom margin to an element |
Margin
The w3-margin class adds a 16px margin to all sides of an element:
The w3-margin class adds 16px margin to all sides of an element.
Example
Margin Top
The w3-margin-top class adds a 16px top margin to an element:
The w3-margin-top class adds a 16px top margin to an element.
Example
Margin Bottom
The w3-margin-bottom class adds a 16px bottom margin to an element:
The w3-margin-bottom class adds a 16px bottom margin to an element.
Example
Margin Left
The w3-margin-left class adds a 16px left margin to an element:
The w3-margin-left class adds a 16px left margin to an element.
Example
Margin Right
The w3-margin-right class adds a 16px right margin to an element:
The w3-margin-right class adds a 16px right margin to an element.
Example
Sections
Many HTML elements do not have a default top or bottom margin. Such elements will display without a margin between them:
I am Blue
I am Green
The w3-section class can be used to separate elements.
It adds 16px top and bottom margin to any HTML element:
CSS margin Property
Set the margin for all four sides of a
element to 35 pixels:
More «Try it Yourself» examples below.
Definition and Usage
The margin property sets the margins for an element, and is a shorthand property for the following properties:
If the margin property has four values:
- margin: 10px 5px 15px 20px;
- top margin is 10px
- right margin is 5px
- bottom margin is 15px
- left margin is 20px
If the margin property has three values:
- margin: 10px 5px 15px;
- top margin is 10px
- right and left margins are 5px
- bottom margin is 15px
If the margin property has two values:
If the margin property has one value:
Note: Negative values are allowed.
Default value: 0 Inherited: no Animatable: yes, see individual properties. Read about animatable Try it Version: CSS1 JavaScript syntax: object.style.margin=»100px 50px» Try it Browser Support
The numbers in the table specify the first browser version that fully supports the property.
CSS Syntax
Property Values
Value Description Demo length Specifies a margin in px, pt, cm, etc. Default value is 0. Negative values are allowed. Read about length units Demo ❯ % Specifies a margin in percent of the width of the containing element Demo ❯ auto The browser calculates a margin Demo ❯ initial Sets this property to its default value. Read about initial inherit Inherits this property from its parent element. Read about 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 horizontal (left and right) margins! Only vertical (top and bottom) margins!
Look at the following example:
Example
In the example above, the
element has a top and bottom margin of 20px.
More Examples
Example
Set the margin for a
element to 35 pixels for top and bottom, and to 70 pixels for right and left:
Example
Set the margin for a
element to 35 pixels for top, 70 pixels for right and left, and to 50 pixels for bottom:
Example
Set the margin for a
element to 35 pixels for top, 70 pixels for right, 50 pixels for bottom, and to 90 pixels for left:
CSS Margins
Margins are used to create space around elements, outside of any defined borders.
CSS Margins
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).
Margin — Individual Sides
CSS has properties for specifying the margin for each side of an element:
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 % of the width of the containing element
- inherit — specifies that the margin should be inherited from the parent element
Tip: Negative values are allowed.
Example
Set different margins for all four sides of a
element:
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:
If the margin property has four values:
- margin: 25px 50px 75px 100px;
- top margin is 25px
- right margin is 50px
- bottom margin is 75px
- left margin is 100px
Example
Use the margin shorthand property with four values:
If the margin property has three values:
- margin: 25px 50px 75px;
- top margin is 25px
- right and left margins are 50px
- bottom margin is 75px
Example
Use the margin shorthand property with three values:
If the margin property has two values:
- margin: 25px 50px;
- top and bottom margins are 25px
- right and left margins are 50px
Example
Use the margin shorthand property with two values:
If the margin property has one value:
Example
Use the margin shorthand property with one value:
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.
Example
The inherit Value
Example
div <
border: 1px solid red;
margin-left: 100px;
>All CSS Margin Properties
Property Description margin A shorthand property for setting all 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 margin
CSS свойство margin определяет внешний отступ на всех четырёх сторонах элемента. Это сокращение, которое устанавливает все отдельные поля одновременно: margin-top , margin-right , margin-bottom и margin-left .
Интерактивный пример
Составные свойства
Данное свойство является сокращением для следующих CSS свойств:
Синтаксис
/* Применяется ко всем четырём сторонам */ margin: 1em; margin: -3px; /* по вертикали | по горизонтали */ margin: 5% auto; /* сверху | горизонтально | снизу */ margin: 1em auto 2em; /* сверху | справа | снизу | слева */ margin: 2px 1em 0 auto; /* Глобальные значения */ margin: inherit; margin: initial; margin: unset;
Свойство margin может быть задано с использованием одного, двух, трёх или четырёх значений. Каждое значение имеет тип , или является ключевым словом auto . Каждое значение может быть положительным, отрицательным или равным нулю.
- Когда определено одно значение, такое значение определено для всех четырёх сторон.
- Когда определены два значения, то первое значение определяет внешний отступ для верхней и нижней стороны, а второе значение определяет отступ для левой и правой стороны.
- Когда определены три значение, то первое значение определяет внешний отступ для верхней стороны, второе значение определяет внешний отступ для левой и правой стороны, а третье значение определяет отступ для нижней стороны.
- Когда определены четыре значения, они определяют внешние отступы для верхней стороны, справа, снизу и слева в рассмотренном порядке (по часовой стрелке).
Значения
Размер отступа как фиксированное значение.
Размер отступа в процентах относительно ширины родительского блока.
Браузер выбирает подходящее значение для использования. Например, в некоторых случаях это значение может быть использовано для центрирования элемента.
Формальное определение
- margin-bottom : 0
- margin-left : 0
- margin-right : 0
- margin-top : 0
- margin-bottom : процент, как указан, или абсолютная длина
- margin-left : процент, как указан, или абсолютная длина
- margin-right : процент, как указан, или абсолютная длина
- margin-top : процент, как указан, или абсолютная длина