margin-top

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

margin-topCSS のプロパティで、要素の上側のマージン領域を設定します。正の数を指定すると、隣との間が遠くなるように配置され、負の数を指定すると、近くなるように配置します。

試してみましょう

このプロパティは、置換要素ではないインライン要素、例えば <span><code> には効果がありません。

構文

css
/* <length> 値 */
margin-top: 10px; /* 絶対的な寸法 */
margin-top: 1em; /* 文字の寸法からの相対 */
margin-top: 5%; /* 直近のブロックコンテナーの幅からの相対 */

/* キーワード値 */
margin-top: auto;

/* グローバル値 */
margin-top: inherit;
margin-top: initial;
margin-top: revert;
margin-top: revert-layer;
margin-top: unset;

margin-top プロパティは auto キーワード、または <length><percentage> で指定されます。正の数、ゼロ、負の数が指定できます。

<length>

マージンの寸法を固定値で表したものです。

<percentage>

マージンの寸法を包含ブロックのインラインサイズ(writing-mode で横書き言語と定義されている場合は width)に対するパーセント値で示したものです。

auto

ブラウザーが適切な値を選択して使用します。 margin を参照してください。

公式定義

初期値0
適用対象table-caption, table, inline-table 以外の表の display 種別を除くすべての要素。 ::first-letter にも適用されます。
継承なし
パーセント値包含ブロックの幅に対する相対値
計算値指定されたパーセント値または絶対的な長さ
アニメーションの種類length

形式文法

margin-top = 
<length-percentage> |
auto |
<anchor-size()>

<length-percentage> =
<length> |
<percentage>

<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )

<anchor-name> =
<dashed-ident>

<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline

正と負の上マージンの設定

css
.content {
  margin-top: 5%;
}
.side-box {
  margin-top: 10px;
}
.logo {
  margin-top: -5px;
}
#footer {
  margin-top: 1em;
}

仕様書

Specification
CSS Box Model Module Level 3
# margin-physical

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
margin-top
anchor-size()
Experimental
auto

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.

関連情報