Lesson 22 / الدرس 22

Sizing a box / تحديد حجم الصندوق

width and height set the size of an element's content — until padding and border push it wider. box-sizing: border-box fixes that by counting them inward instead of adding them on top.

تحددان width و height حجم محتوى العنصر — إلى أن يوسّعاه الحشو والحدود. وتُصلح box-sizing: border-box ذلك بحساب الحشو والحدود ضمن الحجم نفسه بدل إضافتهما فوقه.

Every box you built in the box model lesson has a size. width sets how wide the content is, and height sets how tall. Leave either one out and the box sizes itself to fit whatever is inside it instead.

.box {
    width: 240px;
    height: 120px;
    background-color: lightblue;
}
A box with an exact, fixed size. Change the numbers and watch it grow or shrink.

The surprise: padding and border add to that size

By default, width and height measure only the content — the innermost layer from the box model lesson. Add padding or a border, and the browser adds their size on top of that: a box declared width: 200px with 20px of padding on each side actually takes up 240px on the page. This is the single most common reason a box overflows its container.

.wrapper {
    width: 300px;
    border: 2px solid #333;
}

.box {
    width: 100%;
    padding: 20px;
    background-color: lightblue;
}
The box asks for 100% of its 300px wrapper, then adds 20px of padding on each side on top of that — so it spills out past the wrapper's own border.

box-sizing: border-box — measuring from the outside in

.box {
    box-sizing: border-box;
    width: 100%;
    padding: 20px;
}
box-sizing: border-box tells the browser to count padding and border inside the width you set, instead of adding them on top. The box now stays exactly within its wrapper, no matter how much padding it has.

min-width, max-width, min-height, max-height

img {
    max-width: 100%;
    height: auto;
}
max-width: 100% is the classic rule for a responsive image: it shrinks to fit a narrow box, but never grows past its own real size. min-width, min-height and max-height work the same way, setting a floor or a ceiling instead of a fixed size.
PropertyWhat it sets
width / heightAn exact size for the content
min-width / min-heightA floor the box will not shrink below
max-width / max-heightA ceiling the box will not grow past
box-sizing: content-boxThe default — padding and border add to the width you set
box-sizing: border-boxPadding and border are counted inside the width you set

Every sizing property from this lesson.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. A box has width: 200px and padding: 20px on all sides, with the default box-sizing. How wide does it actually render?

2. Which value makes width include padding and border, instead of adding them on top?

3. What is max-width: 100% commonly used for?

Your task / مهمتك

Fix the overflowing card below: give it box-sizing: border-box so its padding no longer pushes it past its wrapper, and add a min-height so it stays a reasonable size even with short text.

أصلح البطاقة التي تتجاوز حدودها أدناه: أعطها box-sizing: border-box كي لا يدفعها حشوها خارج غلافها، وأضف min-height كي تبقى بحجم معقول حتى مع نص قصير.

  • The card uses box-sizing: border-box تستخدم البطاقة box-sizing: border-box
  • The card no longer overflows its wrapper البطاقة لم تعد تتجاوز حدود غلافها
  • The card has a min-height of at least 100px للبطاقة min-height لا يقل عن 100px
How do you want to submit? / كيف تريد التسليم؟