此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Width
- By default, a block box is as wide as the box it sits in — the whole page, if it sits straight in
<body>. width: 300px;makes it a fixed width instead.max-width: 600px;is gentler — at most this wide, but shrink on small screens.
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.

English
<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.
English
Now you try
- A box needs a width before
margin: 0 auto;can center it. - Set a width, then center the card.
探索
Width and the box
Width sets the content size; padding/border/margin add around it.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Give the card a fixed width. Add width: 300px; to the .card rule.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Center the card on the page. Add margin: 0 auto; to the .card rule.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Make a reading column that shrinks on small screens: give .page max-width: 600px; and centre it with margin: 0 auto;.