HTML/CSS / LESSON 16 / HT16

HTML/CSS:位置指定(position)

1学習の目的

2基礎解説

基準使いどころ
static既定(通常の流れ)指定しない状態
relative元の位置基準を作る役
absolute親の relativeバッジ・重ね
fixed画面(ビューポート)固定ヘッダー
✅ 覚えるべき重要ポイント
  1. ⭐ absolute は「親の relative」を基準にする。親に position: relative が無いと、ページ全体を基準にして遠くへ飛んでいく——position で最も多いつまずきがこれ。
  2. relative は「元の位置を基準にずらす」。ずらしても元の場所は空いたままで、他の要素は詰めてこない。基準を作るためだけに使うことも多い。
  3. absolute と fixed は通常の流れから外れる。その要素は場所を取らなくなるので、下の要素が上に詰めてくる。
  4. fixed は画面に固定される。スクロールしても位置が変わらないので、常時表示のヘッダーや「トップへ戻る」ボタンに使う。
  5. 重なり順は z-index で決まる。数値が大きいほど手前に来る。ただしposition を指定した要素にしか効かない点に注意。

現場使用例:通知バッジ、画像の上のキャプション、固定ヘッダー、モーダルウィンドウ、「トップへ戻る」ボタン。

/* relative:元の位置からずらす(場所は残る) */
.moved {
  position: relative;
  top: 20px;
  left: 30px;
}

/* ⭐ absolute の基本形:親に relative を付ける */
.parent {
  position: relative;      /* これが基準になる */
  width: 300px;
  height: 200px;
}

.badge {
  position: absolute;      /* 親を基準に配置 */
  top: 10px;
  right: 10px;
}

/* ⚠ 親に relative が無いと画面基準になり、遠くへ飛ぶ */

/* 要素の外側にはみ出す配置(負の値) */
.notification {
  position: absolute;
  top: -8px;
  right: -8px;             /* 親の外にはみ出す */
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: red;
}

/* fixed:スクロールしても画面に固定 */
.header-fixed {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 100;            /* 他の要素より手前に */
}

/* 完全な中央配置(Flexboxが使えない場面で) */
.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* sticky:スクロールすると途中から固定 */
.sticky-nav {
  position: sticky;
  top: 0;
}

3基本ドリル(10問)

HT16-D01 relative で要素を元の位置から下に20px・右に30pxずらすHTMLを書け(幅100px・高さ50px・背景 #ccc)。 コーディング★☆☆無料
期待される結果

positionがrelative、位置がずれる

ヒント

position: relative; と top / left を組み合わせる。

模範解答
<style>
  .moved {
    position: relative;
    top: 20px;
    left: 30px;
    width: 100px;
    height: 50px;
    background-color: #ccc;
  }
</style>

<div class="moved">ずれた要素</div>
解説

relative はずらしても元の場所が空いたまま。他の要素は詰めてこないので、レイアウト全体には影響しない。

HT16-D02 position: absolute は何を基準に配置されるか。
A. 常に画面全体 B. 親要素(position 指定があるもの) C. body D. 元の位置
選択★☆☆無料
期待される結果

解答 == B

ヒント

最も近い position 指定のある祖先を探す。

模範解答
B
解説

親に position: relative が無いと、ページ全体が基準になる。「要素が思わぬ場所に飛んだ」原因の大半はこれ。

HT16-D03 親(幅300px・高さ200px・relative)の右上に、絶対配置で小さな四角(50×30px・赤)を置け(上10px・右10px)。 コーディング★☆☆無料
期待される結果

親の右上に配置される

ヒント

親に position: relative、子に position: absolute を指定する。

模範解答
<style>
  .parent {
    position: relative;
    width: 300px;
    height: 200px;
    background-color: #eee;
  }

  .child {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 50px;
    height: 30px;
    background-color: red;
  }
</style>

<div class="parent">
  <div class="child"></div>
</div>
解説

親の relative と子の absolute はセットで使う。この組み合わせが position の基本パターンになる。

HT16-D04 絶対配置の基準を作るコードを完成させよ。 穴埋め★☆☆無料
コード
<style>
  .parent {
    position: ____;
    width: 200px;
    height: 100px;
  }

  .child {
    position: absolute;
    top: 0;
    right: 0;
    width: 30px;
    height: 30px;
    background: red;
  }
</style>

<div class="parent"><div class="child"></div></div>
期待される結果

子が親の右上に配置される

ヒント

「相対的な」を意味する8文字の値。

模範解答
relative
解説

relative は「基準を作る」ためだけに使うことも多い。top や left を指定しなくても、子の absolute の基準になる。

HT16-D05 スクロールしても画面に固定される position の値はどれか。
A. relative B. absolute C. fixed D. static
選択★☆☆無料
期待される結果

解答 == C

ヒント

「固定された」という意味の英単語。

模範解答
C
解説

fixed は画面(ビューポート)を基準にする。ページをスクロールしても位置が変わらないので、常時表示のヘッダーに使う。

HT16-D06 通知バッジを作れ。親(200×100px・relative)の右上に少しはみ出す形で、赤い丸(24×24px、上 -8px・右 -8px)を配置すること。 コーディング★★☆広告解放
期待される結果

バッジが親の外にはみ出す

ヒント

負の値を指定すると親の外側にはみ出す。border-radius: 50% で丸くなる。

模範解答
<style>
  .parent {
    position: relative;
    width: 200px;
    height: 100px;
    background-color: #eee;
  }

  .badge {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    background-color: red;
    border-radius: 50%;
    color: white;
    text-align: center;
  }
</style>

<div class="parent">
  <div class="badge">3</div>
</div>
解説

負の値で親からはみ出せる。アプリの通知バッジやセール表示など、目立たせたい要素に使う定番の手法。

HT16-D07 要素の重なり順を指定するプロパティを補ってコードを完成させよ。 穴埋め★★☆広告解放
コード
<style>
  .front {
    position: relative;
    ____: 10;
    background: blue;
  }
</style>

<div class="front">手前</div>
期待される結果

z-index が10

ヒント

「重なりの番号」を意味する7文字のプロパティ(ハイフン含む)。

模範解答
z-index
解説

数値が大きいほど手前に来る。ただし position を指定した要素にしか効かない点に注意する。

HT16-D08 absolute を指定した要素は、レイアウト上どうなるか。
A. 元の場所が残る B. 通常の流れから外れ、場所を取らなくなる C. 幅が0になる D. 消える
選択★★☆広告解放
期待される結果

解答 == B

ヒント

relative との違いを考える。

模範解答
B
解説

下の要素が詰めて上に来る。relative は場所が残るが、absolute は浮いた状態になるので扱いが違う。

HT16-D09 画面の上部に固定されるヘッダーを作れ(fixed、幅100%、高さ50px、背景 #333、文字色白、z-index: 100)。 コーディング★★☆広告解放
期待される結果

positionがfixed、上部に固定

ヒント

position: fixed; top: 0; left: 0; width: 100%; と書く。

模範解答
<style>
  .header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 50px;
    background-color: #333;
    color: #ffffff;
    z-index: 100;
  }
</style>

<div class="header">固定ヘッダー</div>
<div style="height: 1000px; padding-top: 60px;">長いコンテンツ</div>
解説

本文に上余白を付けるのを忘れない。fixed は場所を取らないので、指定しないとヘッダーの下にコンテンツが隠れる。

HT16-D10 transform を使った中央配置で、親(300×200px・relative)のど真ん中に要素(100×40px)を置け。 コーディング★★☆広告解放
期待される結果

親の中央に配置される

ヒント

top: 50%; left: 50%; transform: translate(-50%, -50%); の3点セット。

模範解答
<style>
  .parent {
    position: relative;
    width: 300px;
    height: 200px;
    background-color: #eee;
  }

  .center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100px;
    height: 40px;
    background-color: #ccc;
  }
</style>

<div class="parent">
  <div class="center">中央</div>
</div>
解説

50%だけでは要素の左上が中央に来るので、transform で自身の半分だけ戻す。Flexboxが使えない場面での中央揃えの定番。

4実践シナリオ(5問)

HT16-S01 【画像の上のキャプション】親(relative)の下部に半透明の帯を絶対配置で重ねよ(親300×200px、帯は幅100%・高さ40px・背景 rgba(0,0,0,0.6)・文字色白)。 コーディング★★☆広告解放
期待される結果

帯が親の下部に配置される

ヒント

bottom: 0; left: 0; width: 100%; で下部いっぱいに配置する。

模範解答
<style>
  .photo {
    position: relative;
    width: 300px;
    height: 200px;
    background-color: #999;
  }

  .caption {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 40px;
    background-color: rgba(0, 0, 0, 0.6);
    color: #ffffff;
    padding: 10px;
    box-sizing: border-box;
  }
</style>

<div class="photo">
  <div class="caption">店内の様子</div>
</div>
解説

写真の上に文字を載せる定番の手法。HT10で学んだ rgba と組み合わせることで、写真が透けつつ文字が読める状態になる。

HT16-S02 【セールラベル】商品カード(relative、200×150px)の左上に「SALE」ラベル(absolute、上0・左0、背景赤・文字白・padding 4px 12px)を重ねよ。 コーディング★★☆広告解放
期待される結果

ラベルが左上に配置される

ヒント

top: 0; left: 0; で親の左上に配置する。

模範解答
<style>
  .card {
    position: relative;
    width: 200px;
    height: 150px;
    border: 1px solid #ddd;
  }

  .label {
    position: absolute;
    top: 0;
    left: 0;
    padding: 4px 12px;
    background-color: red;
    color: #ffffff;
    font-size: 0.875rem;
  }
</style>

<div class="card">
  <div class="label">SALE</div>
</div>
解説

ECサイトで頻出のパターン。カードのレイアウトを崩さずに、目立つラベルを重ねられる。

HT16-S03 【重なり順の制御】2つの要素を重ね、後ろに書いたほうを奥に表示せよ(z-index を使う。1つ目は z-index: 2、2つ目は z-index: 1、どちらも relative で重ねること)。 コーディング★★☆広告解放
期待される結果

z-indexが正しく設定される

ヒント

通常は後の要素が手前に来るが、z-index で逆転できる。

模範解答
<style>
  .box {
    position: relative;
    width: 120px;
    height: 120px;
  }

  .front {
    z-index: 2;
    background-color: #0066cc;
  }

  .back {
    z-index: 1;
    background-color: red;
    margin-top: -60px;
    margin-left: 40px;
  }
</style>

<div class="box front">手前</div>
<div class="box back">奥</div>
解説

z-index で表示順を逆転できる。HTMLの順序に関係なく、数値の大きいほうが手前に来る。

HT16-S04 【トップへ戻るボタン】画面の右下に固定されるボタンを作れ(fixed、下20px・右20px、50×50px、円形、背景 #0066cc、文字白、z-index: 50)。 コーディング★★★広告解放
期待される結果

fixedで右下に配置

ヒント

bottom と right を指定する。border-radius: 50% で円形になる。

模範解答
<style>
  .to-top {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 50px;
    height: 50px;
    background-color: #0066cc;
    color: #ffffff;
    border: none;
    border-radius: 50%;
    z-index: 50;
  }
</style>

<div style="height: 1500px;">長いコンテンツ</div>
<button class="to-top">↑</button>
解説

スクロールしても右下に留まる。長いページでは、ユーザーが上に戻る手間を大きく減らせる。

HT16-S05 【全画面オーバーレイ】画面全体を覆う半透明の黒(fixed、上下左右0、背景 rgba(0,0,0,0.7)z-index: 200)の中央に、白いボックス(300×200px)を配置せよ。 コーディング★★★広告解放
期待される結果

オーバーレイと中央のボックス

ヒント

オーバーレイ自体を Flexbox にすると、中身を簡単に中央配置できる。

模範解答
<style>
  .overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .modal {
    width: 300px;
    height: 200px;
    background-color: #ffffff;
    border-radius: 8px;
    padding: 20px;
    box-sizing: border-box;
  }
</style>

<div class="overlay">
  <div class="modal">
    <h3>お知らせ</h3>
    <p>モーダルウィンドウの中身です。</p>
  </div>
</div>
解説

これがモーダルウィンドウの基本構造。fixed で画面を覆い、その中を Flexbox で中央揃えにする——position と Flexbox の組み合わせ技。

5仕上げ課題

HT16-FINAL 【商品カードと固定要素】position を使い分けて、実用的なUIを作れ。

① 固定ヘッダー.header
position: fixed、上0・左0、幅100%、高さ 56px
・背景 #0066cc、文字色白、z-index: 100

② 商品カード.card
position: relative、幅 240px、高さ 180px、枠線 1px solid #ddd
・中に3つの絶対配置要素:
 - .label左上(上0・左0)、背景赤・文字白・padding 4px 12px
 - .badge右上にはみ出す(上 -8px・右 -8px)、24×24px、円形、背景 #ff6600
 - .caption下部いっぱい(下0・左0、幅100%)、背景 rgba(0,0,0,0.6)、文字白、padding 8px、box-sizing

③ トップへ戻るボタン.to-top
position: fixed、下 20px・右 20px、48×48px、円形、z-index: 50

HTML:header、div.card(中に3要素)、button.to-top
※ カードには margin-top: 80px を付けて、固定ヘッダーに隠れないようにすること
総合★★★広告解放
期待される結果

すべてのpositionと配置が正しい

ヒント

カードに position: relative を付けないと、中の absolute 要素が画面基準になって飛んでいく。badge の負の値でカードの外にはみ出す。

模範解答
<style>
  * {
    box-sizing: border-box;
  }

  body {
    margin: 0;
  }

  .header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 56px;
    background-color: #0066cc;
    color: #ffffff;
    z-index: 100;
    display: flex;
    align-items: center;
    padding: 0 20px;
  }

  .card {
    position: relative;
    width: 240px;
    height: 180px;
    border: 1px solid #ddd;
    margin-top: 80px;
    margin-left: 20px;
  }

  .label {
    position: absolute;
    top: 0;
    left: 0;
    padding: 4px 12px;
    background-color: red;
    color: #ffffff;
  }

  .badge {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    background-color: #ff6600;
    color: #ffffff;
    border-radius: 50%;
    text-align: center;
  }

  .caption {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    color: #ffffff;
    padding: 8px;
  }

  .to-top {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 48px;
    height: 48px;
    background-color: #0066cc;
    color: #ffffff;
    border: none;
    border-radius: 50%;
    z-index: 50;
  }
</style>

<div class="header">ゼロカラショップ</div>

<div class="card">
  <div class="label">SALE</div>
  <div class="badge">3</div>
  <div class="caption">ノートPC 128,000円</div>
</div>

<button class="to-top">↑</button>
解説

このカードには、position の使い方が凝縮されている。ラベルは左上、バッジは右上にはみ出し、キャプションは下部いっぱい——3つとも同じ親を基準にしながら、まったく違う位置に配置されている

ここで最も重要なのは .card { position: relative; } の1行だ。もしこれを書き忘れると、3つの絶対配置要素はすべてページ全体を基準にしてしまい、画面の左上や右上へ飛んでいく。カードのどこにも表示されない——position で最も多い失敗がこれで、原因が分かりにくい。

.badgetop: -8px; right: -8px; という負の値にも注目してほしい。親の枠の外にはみ出して配置できるので、通知の数や「NEW」といった表示を目立たせられる。overflow: hidden が親に付いていると切れてしまう点だけ注意する。

そして .card { margin-top: 80px; } だ。fixed の要素は場所を取らないため、これが無いとカードがヘッダーの下に潜り込んで見えなくなる。固定ヘッダーを使うなら、本文に必ず上余白を確保する——セットで覚えておく。

次章では装飾と動きを扱う。影・グラデーション・トランジションを加えると、ページが一気に「完成品」らしくなる。