Width and centering
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Width
- By default, a block box is as wide as the page.
width: 300px;makes it a fixed width instead.max-width: 600px;is gentler — at most this wide, but shrink on small screens.
日本語
幅
- デフォルトでは、ブロックボックスはページの幅と同じになります。
width: 300px;を使えば固定幅に設定できます。max-width: 600px;はより柔軟です — 最大でこの幅ですが、小さな画面では縮小します。
English
Centering a box
- To center a box (not just its text), give it a width, then
margin: 0 auto;. 0 automeans no top/bottom margin, and equal left/right margins.- Equal side margins push the box into the middle.
日本語
ボックスの中央配置
- テキストだけでなくボックスそのものを中央に配置するには、幅を指定した後に
margin: 0 auto;を与えます。 0 autoは上下のマージンをゼロにし、左右のマージンを等しくします。- 等しい側面マージンによってボックスが中央に移動します。
<style>
.card {
width: 280px;
margin: 0 auto;
padding: 16px;
background-color: #eef;
}
</style>
<div class="card">A centered card.</div>
English
Common mistakes
- Centre a block with a set width using
margin: 0 auto. - A width in
%is relative to the parent element.
日本語
よくあるミス
- 固定された幅を持つブロックを
margin: 0 autoで中央に配置します。 - ⟨
%⟩内の幅は親要素に対する相対値です。
English
Now you try
- A box needs a width before
margin: 0 auto;can center it. - Set a width, then center the card.
日本語
あなたも試してみよう
- ⟨
margin: 0 auto;⟩で中央配置をするには、まず幅が必要です。 - 幅を設定したら、カードを中央に配置します。
Explore · 探索
Width and the box
Width sets the content size; padding/border/margin add around it.
Give the card a fixed width. Add width: 300px; to the .card rule.
Center the card on the page. Add margin: 0 auto; to the .card rule.