CSSのbackground-position:割合、中央、端からの距離

CSSのbackground-positionは背景画像の位置を決めます。centerでは画像と基準領域の中心が揃います。パーセントは領域の寸法と画像の表示寸法の差に掛けます。

キーワードと端からの距離で配置する

CSSのbackground-position仕様の初期値は0% 0%です。centerだけなら縦横が中央になり、leftだけなら左揃えで縦は中央です。12px 8pxのように長さを2つ指定すると左と上からの距離になります。

値 配置 表示例
left top 基準領域の左上
center 画像と基準領域の中心を合わせる
right bottom 基準領域の右下
right 12px bottom 8px 右から12px、下から8px

右や下から一定の間隔を空けるには、端を示すキーワードの後に長さを書きます。right 12px bottom 8pxなら右に12px、下に8pxの間隔です。正の値はその端から内側へ移動させます。

パーセントの位置を計算する

横方向の移動量は(基準幅 − 画像の表示幅)× 割合です。縦方向は高さを使います。領域が240px、画像が80pxなら、50%は80pxです。background-originが基準を選び、background-sizeが計算に使う画像寸法を決めます。

幅が同じなら横に余る空間が0になり、割合を変えても同じ位置です。coverで画像が領域より広くなると、差は負になります。右寄りに設定すると画像の右側がより多く見えます。

実行例で移動量を確かめる

コードをUTF-8のbackground-position-example.htmlとして保存し、ブラウザーで開きます。固定サイズの背景画像としてグラデーションを使うため、外部ファイルは不要です。

<!doctype html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>背景画像の位置比較</title>
  <style>
    body { font: 1rem/1.5 system-ui, sans-serif; margin: 1rem; }
    h1 { overflow-wrap: anywhere; }
    .samples {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 244px), 1fr));
      gap: 1rem;
      max-width: 58rem;
    }
    .tile {
      box-sizing: border-box;
      width: min(100%, 244px);
      height: 104px;
      border: 2px dashed #607d8b;
      background-color: #e6f2ff;
      background-origin: content-box;
      background-clip: content-box;
      background-repeat: no-repeat;
      background-image: linear-gradient(#006c9c, #006c9c);
      background-size: 80px 40px;
    }
    .start { background-position: 0% 0%; }
    .half { background-position: 50% 0%; }
    .end { background-position: 100% 0%; }
    .edge { background-position: right 12px bottom 8px; }

    .layers {
      background-image: linear-gradient(#006c9c, #006c9c),
        linear-gradient(#b95820, #b95820);
      background-size: 48px 24px, 36px 24px;
      background-position: left 8px top 8px, right 12px bottom 8px;
    }
  </style>
</head>
<body>
  <main>
    <h1>背景画像の位置比較</h1>
    <div class="samples">
      <div>
        <p><code>0% 0%</code></p>
        <div class="tile start" aria-hidden="true"></div>
      </div>
      <div>
        <p><code>50% 0%</code></p>
        <div class="tile half" aria-hidden="true"></div>
      </div>
      <div>
        <p><code>100% 0%</code></p>
        <div class="tile end" aria-hidden="true"></div>
      </div>
      <div>
        <p><code>right 12px bottom 8px</code></p>
        <div class="tile edge" aria-hidden="true"></div>
      </div>
      <div>
        <p>2枚の背景レイヤー</p>
        <div class="tile layers" aria-hidden="true"></div>
      </div>
    </div>
  </main>
</body>
</html>

内容領域が240pxなら、先頭3つの横方向の移動量は0px、80px、160pxです。4つ目は右に12px、下に8pxの間隔があります。50% 0%をcenterにすると縦方向も中央になります。

.start { background-position: 0% 0%; }
.half { background-position: 50% 0%; }
.end { background-position: 100% 0%; }
.edge { background-position: right 12px bottom 8px; }

.tileの外側の幅を244pxから204pxに変えると、内容幅は200pxになります。先頭3つが0px、60px、120pxになることを確かめ、元に戻します。狭い画面では実際の内容幅を使って計算します。

複数の背景画像に位置を指定する

background-imageに画像を複数指定した場合は、同じ順番で位置を並べます。1番目は青い画像を左と上から離し、2番目はオレンジ色の画像を右と下から離します。

.layers {
  background-image: linear-gradient(#006c9c, #006c9c),
    linear-gradient(#b95820, #b95820);
  background-size: 48px 24px, 36px 24px;
  background-position: left 8px top 8px, right 12px bottom 8px;
}

位置が1つだけなら、すべてのレイヤーで繰り返されます。画像の順番を変えるときは、対応する寸法と位置も並べ替えます。

位置が変わらないときに調べる

割合を変えても見た目が同じなら、画像の表示寸法と基準ボックスを比較します。background-repeatを一時的にno-repeatにすると1枚の開始位置を確認できます。

開発者ツールでbackground-positionとbackground-sizeの計算値を見ます。空の要素では高さも確認します。後続のbackground一括指定で位置が初期化されることもあります。