Width and centering
تحتاج هذه الصفحة إلى متصفح حديث (يدعم 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 محتوى 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;.