A revolution in responsive design?
04.01.2023
CSS clamp() is a fantastic feature in web design. It makes websites more adaptable to different screen sizes. This article explains what clamp() is and how to use it.
clamp()?CSS clamp() is a CSS function that helps make sizes—like fonts or spacing—flexible. Here’s the syntax:
clamp(MIN, VAL, MAX)
MIN is the smallest value, MAX is the largest, and VAL is the value that flexes, often based on screen size.
clamp() is often used to adjust font sizes so text stays readable on every device:
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
}
This keeps the h1 font size between 1.5rem and 3rem, scaling with the viewport.
You can also use clamp() to make padding or margins flexible:
.section {
padding: clamp(10px, 5%, 50px);
}
The padding varies between 10px and 50px, depending on the screen width.
clamp() helps ensure images and videos look right:
img {
width: 100%;
height: clamp(200px, 50vh, 400px);
}
This keeps image height between 200px and 400px.
clamp() is great, but don’t overdo it. Too many clamps can complicate your CSS. Always test across devices.
CSS clamp() is a useful tool for web designers. It makes websites more flexible and keeps them looking good on any device.