CSS Functions
calc(), clamp(), min(), max() — smart sizing and dynamic values. CSS functions are like the secret masala in biryani.
01Understanding CSS Functions
CSS functions allow dynamic calculations and value manipulations. Like the secret masala in biryani, functions make your CSS smart and flexible.
Analogy: Just like the secret masala in Hyderabadi biryani, CSS functions add that special touch — calc() for blending, clamp() for balance, min()/max() for constraints.
/* Math functions */
.box { width: calc(100% - 40px); }
h1 { font-size: clamp(1.5rem, 4vw, 3rem); }
.container { width: min(1200px, 100%); }02Mathematical Functions
Four essential math functions for dynamic values:
FunctionDescriptionExample
calc()Performs calculations with mixed unitswidth: calc(100% - 2rem)clamp()Clamps a value between min and maxfont-size: clamp(1rem, 2.5vw, 2rem)min()Selects the smallest valuewidth: min(800px, 100%)max()Selects the largest valuepadding: max(1rem, 2vw)/* calc() — arithmetic with any units */
.container { width: calc(100% - var(--gap, 2rem) * 2); }
.hero { height: calc(100vh - 60px); } /* Full minus navbar */
.title { font-size: calc(1rem + 2vw); } /* Scales with viewport */
/* clamp() — fluid typography */
h1 { font-size: clamp(1.75rem, 4vw, 3rem); }
body { font-size: clamp(1rem, 1.5vw, 1.25rem); }
/* min() — ensures value doesn't exceed maximum */
.container { width: min(1200px, 95%); }
/* max() — ensures value doesn't go below minimum */
.padding { padding: max(1rem, 2vw); }03Color Functions
Color functions create and manipulate colors:
/* RGB/RGBA — red, green, blue with alpha */
.box { background: rgb(249, 115, 22); }
.overlay { background: rgba(0, 0, 0, 0.5); }
/* HSL/HSLA — hue, saturation, lightness (more intuitive) */
.box { background: hsl(25, 95%, 53%); }
.overlay { background: hsla(25, 95%, 53%, 0.3); }
/* color-mix() — mix two colors (modern) */
.mix { background: color-mix(in srgb, #f97316, #3b82f6); } /* Blends orange + blue */04Practical Examples
Real-world patterns using CSS functions:
/* Responsive container */
.container { width: min(1200px, 95%); margin: 0 auto; }
/* Fluid typography scale */
h1 { font-size: clamp(2rem, 5vw, 3.5rem); }
h2 { font-size: clamp(1.5rem, 3vw, 2.5rem); }
p { font-size: clamp(1rem, 1.5vw, 1.125rem); }
/* Aspect ratio box */
.video { width: 100%; aspect-ratio: 16 / 9; } /* Modern: use aspect-ratio */
.video-old { width: 100%; height: calc(100% * 9 / 16); } /* Old method */
/* Full height minus fixed header */
.main { min-height: calc(100vh - 60px); }
/* Dynamic gap */
.grid { gap: clamp(0.5rem, 2vw, 2rem); }05Exercise — CSS Functions
Practice CSS functions below.
Try these experiments:
Done — Key Points:
- ✅
calc()performs math with mixed units (px + %, rem + vw, etc.). - ✅
clamp(min, preferred, max)= fluid values. Perfect for responsive typography. - ✅
min(a, b)picks smaller.max(a, b)picks larger. Great for constraints. - ✅
aspect-ratiois the modern way for fixed-ratio boxes (no padding hacks). - ✅
color-mix()blends two colors. CSS functions can be nested and combined.
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login