Lesson 17 / الدرس 17
calc(), min(), max() and clamp() / الدوال calc() و min() و max() و clamp()
Four functions that do arithmetic in CSS. They let one declaration cover every screen, so a heading can grow with the window and still refuse to become absurd at either end.
أربع دوال تُجري حسابات داخل CSS. تتيح لإعلان واحد أن يغطي كل الشاشات، فيكبر العنوان مع النافذة ويرفض أن يصبح سخيفًا عند أي من الطرفين.
Lesson 16 gave you units. On their own each one answers a single question — how many pixels, what share of the parent, how much of the window. These four functions let you combine them, and that is where they stop being a list of options and start being useful.
calc() — arithmetic across units
calc() — حساب عبر الوحدات.main {
width: calc(100% - 2rem);
}
% or in rem, only in both at once. Keep spaces around the - and + — calc(100%-2rem) is invalid and silently does nothing.
% ولا بـ rem، بل بكليهما معًا. وأبقِ مسافات حول - و+ — فـ calc(100%-2rem) غير صالحة ولا تفعل شيئًا بصمت.
min() and max() — a ceiling and a floor
min() وmax() — سقف وأرضية
These two are named the opposite way round from how they behave, which trips up everybody once. min(90%, 60rem) takes whichever value is smaller — so it acts as a maximum width: 90% on a phone, capped at 60rem on a wide screen. max() takes the larger, so it sets a floor.
min(90%, 60rem) تأخذ القيمة الأصغر — فتعمل عمل الحد الأقصى للعرض: 90% على الهاتف، ومحدودة بـ 60rem على الشاشة العريضة. أما max() فتأخذ الأكبر، فتضع أرضية..page {
width: min(90%, 40rem);
margin-inline: auto;
}
width, a max-width and a media query. Drag the preview: the box follows the width until it hits 40rem, then stops and stays centred.
width وmax-width واستعلام وسائط. اسحب المعاينة: يتبع الصندوق العرض حتى يبلغ 40rem، ثم يتوقف ويبقى موسّطًا.
clamp() — floor, ideal, ceiling
clamp() — أرضية ومثالي وسقف
clamp(min, ideal, max) is the one you will reach for most. Give it a smallest value, a value that grows, and a largest. It is what makes type fluid: a heading that scales with the window but never shrinks past readable or grows past ridiculous — no media query in sight.
clamp(min, ideal, max) هي التي ستلجأ إليها أكثر ما تلجأ. أعطها أصغر قيمة، وقيمة تنمو، وأكبر قيمة. وهي ما يجعل الخط سائلًا: عنوان يتمدد مع النافذة لكنه لا يصغر إلى ما دون المقروء ولا يكبر إلى حد السخف — دون أي استعلام وسائط.h1 {
font-size: clamp(1.75rem, 5vw, 3.5rem);
}
font-size: 4vw يستمر في الصغر على هاتف ضيق حتى يصبح غير مقروء، والقارئ الذي كبّر النص من إعدادات متصفحه لا يستفيد شيئًا، لأن vw تتجاهله. وأرضية rem داخل clamp() هي ما يحمي الاثنين.width: min(100% - 2rem, 60rem) صالحة، ويمكنك داخل هذه الدوال كتابة العمليات الحسابية مباشرة دون لفّها بـ calc() — فالحساب مسموح هناك أصلًا.| Function | Takes | Use it for |
|---|---|---|
| calc(100% - 2rem) | Arithmetic across units | Full width minus a fixed gutter |
| min(90%, 40rem) | The smaller value | A maximum width |
| max(50%, 20rem) | The larger value | A minimum width |
| clamp(1.75rem, 5vw, 3.5rem) | Floor, ideal, ceiling | Fluid type and spacing |
Which one answers which question.
Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What does width: min(90%, 40rem) do?
width: min(90%, 40rem)؟It picks whichever is smaller at the time, so the larger value becomes an effective ceiling. The names describe the arithmetic, not the visual effect.
2.
Why does calc(100%-2rem) not work?
calc(100%-2rem)؟Without the spaces the parser cannot tell subtraction from a negative number, so the whole declaration is dropped — silently, which is what makes it confusing to find.
3.
In clamp(1.5rem, 4vw, 3rem), what is the 1.5rem for?
clamp(1.5rem, 4vw, 3rem)، ما وظيفة 1.5rem؟Floor first, ideal second, ceiling last. The floor is what stops fluid type becoming unreadable on a narrow phone.
Score / النتيجة: 0 / 3
Your task / مهمتك
Build an article card that needs no media query. Give the card a width with min() so it centres and caps on a wide screen, size the heading with clamp() so it scales between two sensible limits, and use calc() once for a width that subtracts a fixed gutter.
ابنِ بطاقة مقال لا تحتاج أي استعلام وسائط. أعطِ البطاقة عرضًا بـ min() بحيث تتوسط وتتوقف عند حد على الشاشة العريضة، واضبط حجم العنوان بـ clamp() ليتمدد بين حدين معقولين، واستخدم calc() مرة واحدة لعرض يطرح هامشًا ثابتًا.
-
The card width uses
min()عرض البطاقة يستخدمmin() -
The heading uses
clamp()with aremfloor العنوان يستخدمclamp()بأرضيةrem -
calc()appears once, with spaces around the operatorcalc()تظهر مرة، بمسافات حول المعامل -
There is no
@mediaanywhere لا يوجد@mediaفي أي مكان