Свойство CSS margin-top
Пример
Установите верхнее поле для элемента <p> равным 25 пикселям:
p.ex1 {
margin-top: 25px;
}
Другие примеры «Попробуйте сами» ниже.
Определение и использование
Свойство margin-top
устанавливает верхнее поле элемента.
Примечание. Допустимы отрицательные значения.
Значение по умолчанию: | 0 |
---|---|
Унаследовано: | нет |
Анимация: | да, см. отдельные свойства . Читать об анимации |
Версия: | CSS1 |
Синтаксис JavaScript: | объект .style.marginTop="100px" |
Поддержка браузера
Цифры в таблице указывают на первую версию браузера, которая полностью поддерживает это свойство.
Property | |||||
---|---|---|---|---|---|
margin-top | 1.0 | 6.0 | 1.0 | 1.0 | 3.5 |
Синтаксис CSS
margin-top: length|auto|initial|inherit;
Значения свойств
Value | Description | Play it |
---|---|---|
length | Specifies a fixed top margin in px, pt, cm, etc. Default value is 0px. Negative values are allowed. Read about length units | |
% | Specifies a top margin in percent of the width of the containing element | |
auto | The browser calculates a top margin | |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
Свернуть поля
Верхнее и нижнее поля элементов иногда сворачиваются в одно поле, равное наибольшему из двух полей.
Этого не происходит на горизонтальных (левых и правых) полях! Только вертикальные (сверху и снизу) поля!
Посмотрите на следующий пример:
Пример
p.a {
margin: 30px 0;
}
p.b {
margin: 20px 0;
}
В приведенном выше примере элемент <p class="a"> имеет верхнее и нижнее поле по 30 пикселей. Элемент <p class="b"> имеет верхнее и нижнее поля по 20 пикселей.
Это означает, что вертикальное поле между <p class="a"> и <p class="b"> должно составлять 50 пикселей (30 пикселей + 20 пикселей). Но из-за коллапса полей фактические поля составляют 30 пикселей!
Дополнительные примеры
Пример
Установите верхнее поле для элемента <p> равным 10% от ширины контейнера:
p.ex1 {
margin-top: 10%;
}
Пример
Установите верхнее поле для элемента <p> равным 2 em:
p.ex1 {
margin-top: 2em;
}
Связанные страницы
Учебное пособие по CSS: CSS Margin
Справочник по HTML DOM: свойство marginTop