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

.main {
    width: calc(100% - 2rem);
}
The thing no unit can do alone: mix them. "The full width, less a fixed gutter" is not expressible in % or in rem, only in both at once. Keep spaces around the - and + — calc(100%-2rem) is invalid and silently does nothing.

min() and max() — a ceiling and a floor

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.

.page {
    width: min(90%, 40rem);
    margin-inline: auto;
}
One line that replaces a 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.

clamp() — floor, ideal, ceiling

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.

h1 {
    font-size: clamp(1.75rem, 5vw, 3.5rem);
}
Never smaller than 1.75rem, never larger than 3.5rem, and 5% of the window width in between. Drag the preview and watch the heading move between its two limits.
FunctionTakesUse it for
calc(100% - 2rem)Arithmetic across unitsFull width minus a fixed gutter
min(90%, 40rem)The smaller valueA maximum width
max(50%, 20rem)The larger valueA minimum width
clamp(1.75rem, 5vw, 3.5rem)Floor, ideal, ceilingFluid type and spacing

Which one answers which question.

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

Preview / المعاينة

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

1. What does width: min(90%, 40rem) do?

2. Why does calc(100%-2rem) not work?

3. In clamp(1.5rem, 4vw, 3rem), what is the 1.5rem for?

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 a rem floor العنوان يستخدم clamp() بأرضية rem
  • calc() appears once, with spaces around the operator calc() تظهر مرة، بمسافات حول المعامل
  • There is no @media anywhere لا يوجد @media في أي مكان
How do you want to submit? / كيف تريد التسليم؟