How to use CSS Clamp In CSS
We can utilize CSS Clamp to make text content flexible within containers. It takes three parameters: value1, value2, and value3. value1 represents the minimum allowed value, value2 is the preferred value, and value3 is the maximum allowed value.
Syntax:
clamp(value1, value2, value3)
Example: The below code will explain the use of clamp to make font size responsive according to the screen size within container.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content=
"width=device-width, initial-scale=1.0" />
<title>Font Size with Clamp</title>
<style>
body {
display: flex;
justify-content: center;
}
.container {
width: 80%;
border: 1px solid #ccc;
text-align: center;
}
/* Using clamp to adjust font size */
.text {
font-size: clamp(18px, 3vw, 26px);
}
</style>
</head>
<body>
<div class="container">
<p class="text">
This text will adjust its font size based
on the width of its container. Resize the
window to see the effect.
</p>
</div>
</body>
</html>
Output:
How to make CSS Font Size Fit Container Effectively ?
Making CSS font size fit a container effectively involves ensuring that the text scales appropriately based on the dimensions of the container. This is crucial for maintaining readability and a consistent user experience regardless of the device being used.
Table of Content
- Using Viewport
- Using Media Queries
- Using CSS Clamp