CSS clamp()
Published Feb 27, 2022
Contribute to Docs
The clamp() function restricts a given value between an upper and lower bound. In this way, it acts like a combination of the min() and max() functions.
Syntax
<property>: clamp(<lower bound>, <value>, <upper bound>)
The <value> is valid as long as it is between the <lower bound> and <upper bound>. This ensures that the <property> has a minimum and maximum <value>.
Each argument to clamp() can be a combination of CSS units, or evaluations of them, using the following:
- Absolute units:
10px - Relative units:
40vh - Math expressions:
40vw - 20px - Nested math function values:
min(20vw, 200px)
Example
.display-font {font-size: clamp(2rem, 3vw, 5rem);}
In the example above, .display-font elements will have their font-size clamped down to span 3% of the viewport’s width. However, it can be no more than 5rem and no less than 2rem.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn CSS on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- A full-stack engineer can get a project done from start to finish, back-end to front-end.
- Includes 51 Courses
- With Professional Certification
- Beginner Friendly.150 hours