Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Functions – calc(), clam | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 25 of 50

·css

CSS Functions – calc(), clamp(), min(), max()

8 mins

Course progress50%

What you'll learn

Keep these ideas in mind as you work through the lesson.

2 points
  • `calc()` can mix units (`%` + `px`).
  • `clamp()` is perfect for responsive typography and spacing. * `min()`/`max()` are useful for grid gutters and container widths.

Functions give CSS programming‑like capabilities, letting you calculate values on the fly.

calc() – Arithmetic

Combine different units and operators.

.content { width: calc(100% - 2rem); }
.sidebar { height: calc(100vh - 4rem); }

Use spaces around +, -, *, / for readability – browsers ignore them.

clamp() – Fluid values with limits

font-size: clamp(1rem, 2.5vw, 2rem);

The browser picks the preferred middle value (2.5vw) but never goes below 1rem or above 2rem.

min() & max() – Pick extremes

.container { max-width:max(20rem, 50%); }

If 50% of the viewport is larger than 20rem, the max() returns 50%; otherwise 20rem.

Example – Fluid Padding Using clamp

section {
  padding: clamp(1rem, 2vw, 3rem);
}

On a small phone the padding stays at 1 rem, on a large desktop it grows to 3 rem, and in‑between it scales fluidly with the viewport width.


Pro Tip – Combine Functions

.card {
  width: min(calc(100% - 2rem), 400px);
  font-size: clamp(0.9rem, 1.5vw, 1.2rem);
}

min() ensures the card never exceeds 400 px, while its text stays fluid.


Browser Support

All four functions are widely supported in Chrome, Edge, Firefox, and Safari (including iOS). No vendor prefixes required.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS Custom Properties (Variables)Continue to lesson 26Logical Properties & Writing ModesBack to CSS Basics to Advanced curriculum