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.
width عرض المحتوى، وتحدد height ارتفاعه. وإن تركت إحداهما، يحدد الصندوق حجمه بنفسه بما يناسب ما بداخله..box {
width: 240px;
height: 120px;
background-color: lightblue;
}
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.
width و height المحتوى وحده — أي الطبقة الأولى من الداخل التي رأيتها في درس نموذج الصندوق. وإن أضفت حشوًا أو حدودًا، يضيف المتصفح حجمهما فوق ذلك: فصندوق حجمه width: 200px مع حشو 20px على كل جانب يشغل فعليًا 240px على الصفحة. وهذا هو السبب الأشهر لتجاوز صندوق حدود العنصر الذي يحتويه..wrapper {
width: 300px;
border: 2px solid #333;
}
.box {
width: 100%;
padding: 20px;
background-color: lightblue;
}
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.
100% من عرض غلافه البالغ 300px، ثم يضيف 20px من الحشو على كل جانب فوق ذلك — فيفيض خارج حدود الغلاف نفسه.width: 100% مع أي حشو أو حدود هو الطريقة الكلاسيكية التي يكسر بها صندوق حدود العنصر الذي يحتويه. فالحشو يُضاف فوق تلك النسبة 100%، لا يُقتطع منها — فينتهي الصندوق أعرض من والده رغم أن 100% توحي بأنه سيلائمه تمامًا.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.
box-sizing: border-box المتصفح أن يحسب الحشو والحدود ضمن العرض الذي حددته، بدل إضافتهما فوقه. فيبقى الصندوق الآن داخل غلافه تمامًا، مهما كان حشوه.box-sizing: border-box مرة واحدة، في أعلى ملف الأنماط، لكل عنصر: * { box-sizing: border-box; }. عندها يتصرف كل حجم تكتبه في أي مكان من المشروع بالطريقة المتوقعة، ولا تضطر للتفكير في الأمر مرة أخرى.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.
max-width: 100% هي القاعدة الكلاسيكية للصورة المتجاوبة: تتقلص لتناسب صندوقًا ضيقًا، لكنها لا تكبر أبدًا أكثر من حجمها الحقيقي. وتعمل min-width و min-height و max-height بالطريقة نفسها، فتضبط حدًا أدنى أو أقصى بدل حجم ثابت.| Property | What it sets |
|---|---|
| width / height | An exact size for the content |
| min-width / min-height | A floor the box will not shrink below |
| max-width / max-height | A ceiling the box will not grow past |
| box-sizing: content-box | The default — padding and border add to the width you set |
| box-sizing: border-box | Padding and border are counted inside the width you set |
Every sizing property from this lesson.
Try it live / جرّب بنفسك
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?
width: 200px و padding: 20px على كل الجهات، مع box-sizing الافتراضية. كم يكون عرضه الفعلي؟The default box-sizing (content-box) adds padding on top of the width you set: 200 + 20 + 20 = 240px.
box-sizing الافتراضية (content-box) الحشو فوق العرض الذي حددته: 200 + 20 + 20 = 240px.
2.
Which value makes width include padding and border, instead of adding them on top?
width تشمل الحشو والحدود، بدل إضافتهما فوقها؟border-box measures the width you set from the outer edge of the border inward, so padding and border no longer add extra size.
border-box العرض الذي حددته من الحافة الخارجية للحدود إلى الداخل، فلا يضيف الحشو والحدود حجمًا إضافيًا بعد ذلك.
3.
What is max-width: 100% commonly used for?
max-width: 100% عادةً؟It lets an image shrink on a narrow screen while never growing past its own real size or its container's width.
Score / النتيجة: 0 / 3
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-heightof at least 100px للبطاقةmin-heightلا يقل عن 100px