CSS Functions
CSS
What Are CSS Functions?
CSS functions perform calculations directly in CSS to make styles more flexible and responsive.
1. calc() — Do Math in CSS
Allows you to add, subtract, multiply, or divide values with different units.
- Syntax:
width: calc(100% - 50px);
- Example
2. clamp() — Responsive Values with Limits
Defines a value that can grow or shrink, but stays within a min and max.
- Syntax:
font-size: clamp(min, preferred, max);
- Example
This sets font size based on screen width (5vw) but never smaller than 1rem or larger than 3rem.
3. min() — Choose the Smaller Value
Sets a value based on the smallest of two or more values.
- Example:
Use this to limit width on large screens while being fluid on smaller screens.
4. max() — Choose the Larger Value
Sets a value based on the largest of two or more values.
- Example:
Ensures a minimum width, even on small screens.
Function | What it does | Example |
---|---|---|
calc() | Math with units | width: calc(100% - 50px) |
clamp() | Responsive value with min & max bounds | font-size: clamp(1rem, 4vw, 3rem) |
min() | Chooses the smallest of given values | width: min(90%, 600px) |
max() | Chooses the largest of given values | width: max(200px, 20%) |