CSS position: absolute プロパティ値
position: absoluteCSS の配置プロパティの中でも最も強力な値の1つです。
絶対配置要素を通常のドキュメントフローから外し、スペースを占有しなくなり、その後その最も近い位置指定された祖先要素を基準に正確に配置します。
これはポップアップボックス、ドロップダウンメニュー、フローティングカードなどのUIコンポーネントを作成するための核となる技術です。
単語の意味:absoluteラテン語に由来しabsolutus「絶対的な、無条件の」という意味で、位置がドキュメントフローから完全に独立していることを表します。
基本構文
position: absolute組み合わせて使用する必要がありますtop、right、bottom、leftプロパティで位置を指定します。
/* 语法 */
position: absolute;
/* 常用组合 */
.box {
position: absolute;
top: 0; /* 顶部对齐 */
left: 0; /* 左侧对齐 */
/* 或者 */
top: 50%; /* 居中开始 */
left: 50%;
transform: translate(-50%, -50%); /* 完全居中 */
}
配置の原理
理解するabsolute配置の鍵は配置コンテキストの概念です。
配置コンテキスト
絶対配置された要素は上へ階層ごとに検索し、最初に見つかったstatic 以外の配置の祖先要素を参照とします。そのような祖先要素が見つからない場合は、初期包含ブロック(通常はページ)を基準に配置します。
検索プロセス
┌─────────────────────────────────────┐ │ 初始包含块 (body 或 html) │ │ ┌─────────────────────────────┐ │ │ │ parent (position: relative) │ ← 找到!作为参照 │ │ │ ┌─────────────────────┐ │ │ │ │ │ child (absolute) │ │ │ │ │ │ top: 0, left: 0 │ │ │ │ │ └─────────────────────┘ │ │ │ └─────────────────────────────┘ │ └─────────────────────────────────────┘
主な特徴
特徴1: ドキュメントフローから外れる
absolute要素はドキュメント空間を占有しない周囲の要素はその存在を無視して通常通りに配置されます。あたかも要素がドキュメントから「浮き上がった」かのようです。
特徴2: 位置指定された祖先要素を基準に配置
「位置指定された」とはposition値がstatic(指定できるのはrelative、absolute、fixed 或 sticky)。
特徴3: z-index が有効になる
とrelative同様に、absolute有効化するz-indexプロパティ。
特徴4: margin は padding と結合する
通常のドキュメントフローとは異なり、absolute要素の margin は隣接する要素の margin とは結合しません。
実例
実例を通して深く理解しましょうposition: absoluteの動作の仕組みを。
例1: 最も基本的な絶対配置
この例は、要素が親コンテナを基準にどのように配置されるかを示しています。
実例
<html>
<head>
<style>
/* 親コンテナは参照元となるために非 static の配置を設定する必要があります */
.container {
position: relative; /* 重要: 配置の参照元とする */
width: 300px;
height: 200px;
background-color: #f5f5f5;
border: 2px solid #3498db;
}
/* 絶対配置の要素 */
.absolute-box {
position: absolute;
top: 20px;
right: 20px;
width: 80px;
height: 40px;
background-color: #e74c3c;
color: white;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
親コンテナ (position: relative)
<div class="absolute-box">絶対配置</div>
</div>
<p>赤いボックスは、ページではなく親コンテナの右上隅を基準に配置されます。</p>
</body>
</html>
例2: 位置指定された祖先要素がない場合
親要素に配置が設定されていない場合、要素は上へ検索を続け、最終的にページを基準に配置されます:
実例
<html>
<head>
<style>
/* 注意: ここでは position: relative を設定していません */
.container-no-position {
width: 300px;
height: 150px;
background-color: #f0f0f0;
border: 2px solid #ccc;
padding: 20px;
}
/* 親要素に配置がないため、ページを基準に配置されます */
.floating-box {
position: absolute;
top: 10px;
left: 10px;
background-color: #2E7DCC;
color: white;
padding: 10px 20px;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container-no-position">
親コンテナ (配置なし)
<div class="floating-box">ページを基準に配置</div>
</div>
<p>親コンテナに position が設定されていないため、緑のボックスはページ全体(body)の左上隅を基準に配置されます。</p>
</body>
</html>
例3: 中央表示のポップアップボックスを作成する
これはabsolute最も一般的なユースケースの1つ:ポップアップウィンドウの作成です。
実例
<html>
<head>
<style>
/* オーバーレイ:ページ全体を覆う */
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
}
/* ポップアップコンテナ */
.modal {
position: relative; /* 配置コンテキストを作成 */
width: 400px;
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
overflow: hidden;
}
/* ポップアップのヘッダー */
.modal-header {
background-color: #3498db;
color: white;
padding: 15px 20px;
font-size: 18px;
font-weight: bold;
}
/* 閉じるボタン:絶対配置 */
.modal-close {
position: absolute;
top: 10px;
right: 15px;
cursor: pointer;
font-size: 24px;
line-height: 1;
}
/* ポップアップの本文 */
.modal-body {
padding: 20px;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="overlay">
<div class="modal">
<div class="modal-header">
通知メッセージ
<span class="modal-close">×</span>
</div>
<div class="modal-body">
これは絶対配置を使用して中央に配置したポップアップウィンドウです。
閉じるボタンは position: absolute を使用して右上隅に配置されています。
</div>
</div>
</div>
</body>
</html>
例4: 画像にテキストの透かしを追加する
絶対配置を使うと、画像の上にテキストやラベルを重ねることができます:
実例
<html>
<head>
<style>
.image-container {
position: relative; /* 配置コンテキストを作成 */
display: inline-block;
}
.image-container img {
width: 300px;
height: 200px;
display: block;
object-fit: cover;
}
/* 左上のラベル */
.badge {
position: absolute;
top: 10px;
left: 10px;
background-color: #e74c3c;
color: white;
padding: 4px 10px;
font-size: 12px;
font-weight: bold;
border-radius: 3px;
}
/* 右下の透かし */
.watermark {
position: absolute;
bottom: 10px;
right: 10px;
color: white;
font-size: 14px;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);
}
</style>
</head>
<body>
<div class="image-container">
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='200'%3E%3Crect fill='%233498db' width='300' height='200'/%3E%3Ctext x='50%25' y='50%25' text-anchor='middle' fill='white' font-size='24'%3Eサンプル%3C/text%3E%3C/svg%3E" alt="サンプル画像">
<span class="badge">厳選</span>
<span class="watermark">example.com</span>
</div>
</body>
</html>
例5: ドロップダウンメニュー
絶対配置はドロップダウンメニューを作成するための重要な技術です:
実例
<html>
<head>
<style>
.dropdown {
position: relative; /* 配置コンテキストを作成 */
display: inline-block;
}
/* トリガーボタン */
.dropbtn {
background-color: #3498db;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
/* ドロップダウンの内容 */
.dropdown-content {
position: absolute; /* 絶対配置 */
top: 100%; /* ボタンの下に表示 */
left: 0;
min-width: 160px;
background-color: white;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
border-radius: 4px;
display: none; /* 初期状態では非表示 */
}
/* マウスホバー時に表示 */
.dropdown:hover .dropdown-content {
display: block;
}
/* ドロップダウンの選択肢 */
.dropdown-content a {
display: block;
padding: 12px 16px;
text-decoration: none;
color: #333;
border-bottom: 1px solid #eee;
}
.dropdown-content a:hover {
background-color: #f5f5f5;
}
.dropdown-content a:last-child {
border-bottom: none;
}
</style>
</head>
<body>
<div class="dropdown">
<button class="dropbtn">ドロップダウンメニュー</button>
<div class="dropdown-content">
<a href="#">オプション1</a>
<a href="#">オプション2</a>
<a href="#">オプション3</a>
</div>
</div>
<p>マウスを上に重ねるとドロップダウンが表示されます</p>
</body>
</html>
例6: 3×3グリッド配置
絶対配置を利用すると、要素の3×3グリッド配置を簡単に実現できます:
実例
<html>
<head>
<style>
.box {
position: relative;
width: 300px;
height: 200px;
background-color: #f5f5f5;
border: 2px solid #ddd;
}
.corner {
position: absolute;
width: 80px;
height: 40px;
background-color: #3498db;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
border-radius: 4px;
}
/* 四隅 */
.top-left { top: 10px; left: 10px; }
.top-right { top: 10px; right: 10px; }
.bottom-left { bottom: 10px; left: 10px; }
.bottom-right { bottom: 10px; right: 10px; }
/* 四辺の中央 */
.top-center {
top: 10px;
left: 50%;
transform: translateX(-50%);
}
.bottom-center {
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.left-center {
left: 10px;
top: 50%;
transform: translateY(-50%);
}
.right-center {
right: 10px;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box">
<div class="corner top-left">左上</div>
<div class="corner top-center">上</div>
<div class="corner top-right">右上</div>
<div class="corner left-center">左</div>
<div class="corner right-center">右</div>
<div class="corner bottom-left">左下</div>
<div class="corner bottom-center">下</div>
<div class="corner bottom-right">右下</div>
</div>
</body>
</html>
注意点
重要な注意:絶対配置された要素はドキュメントフローから外れるため、スペースを占有しません。すべての兄弟要素が絶対配置を使用すると、コンテナが「崩壊」(高さが0になる)する可能性があります。親コンテナに明確な寸法を設定するか、別の方法で高さを確保してください。
位置指定祖先チェーン
絶対配置は上へ階層ごとに位置指定祖先を検索します:
元素的查找顺序: 1. 首先查找父元素是否有非 static 定位 2. 如果没有,继续查找祖父元素 3. 依此类推,直到 html 元素 4. 如果都没有,则相对于初始包含块(页面)定位
幅の問題
絶対配置された要素の幅はデフォルトでは内容によって決まります(インライン要素に似ています)。コンテナいっぱいに広げる必要がある場合は、幅を明示的に設定する必要があります:
.full-width {
position: absolute;
left: 0;
right: 0; /* 配合 left: 0 和 right: 0 使宽度100% */
/* 或者 */
width: 100%; /* 直接设置宽度 */
}
他の配置値との比較
| 配置値 | ドキュメントフローから外れるか | 配置の基準 | スクロールに応じて移動するか |
|---|---|---|---|
static |
否 | 通常のドキュメントフロー | である |
relative |
いいえ(スペースを保持) | 元の位置 | である |
absolute |
である | 最も近い位置指定祖先 | である |
fixed |
である | ビューポート(ブラウザウィンドウ) | 否 |
sticky |
いいえ(固定前) | td>ビューポート(固定後)はい(固定後は移動しない) |
よくあるユースケース
シナリオ1: 固定ヘッダーナビゲーション
.navbar {
position: fixed;
top: 0;
width: 100%;
height: 60px;
}
シナリオ2: 画像カルーセルのインジケーター
.carousel {
position: relative;
}
.indicators {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
シナリオ3: チャットウィンドウの吹き出し配置
.message-bubble {
position: absolute;
left: 20px;
max-width: 70%;
}
まとめ
position: absolute現代のWebレイアウトに欠かせない技術です:
- 要素ドキュメントフローから外れ、スペースを占有しない
- ~に対して最も近い位置指定された祖先要素位置指定
- ~と併用する必要があり
top、right、bottom、left使用する - アクティブにする
z-index、スタッキング順序を制御できる - ポップアップ、ドロップダウンメニュー、フローティングラベルを作成するためのコア技術です
- 親コンテナは~に設定する必要があります
relative位置指定コンテキストを作成するために
その他の拡張
CSS position プロパティ