The clamp() function helps you set a size that can grow and shrink but stays within limits. It keeps your design flexible and neat on different screen sizes.
Clamp function in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
clamp(minimum, preferred, maximum)
minimum is the smallest value allowed.
preferred is the ideal value that can grow or shrink.
maximum is the largest value allowed.
font-size: clamp(1rem, 2vw, 2rem);
width: clamp(200px, 50%, 600px);
padding: clamp(0.5rem, 2vw, 1.5rem);
This page shows a heading and paragraph that change size smoothly between minimum and maximum values using clamp(). The heading grows from 1.5rem to 3rem, and the paragraph text and box size also adjust nicely.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Clamp Function Example</title> <style> body { font-family: Arial, sans-serif; margin: 2rem; } h1 { font-size: clamp(1.5rem, 5vw, 3rem); color: #2a7ae2; } p { font-size: clamp(1rem, 2.5vw, 1.5rem); max-width: clamp(200px, 60%, 600px); background-color: #e0f0ff; padding: clamp(0.5rem, 1vw, 1rem); border-radius: 0.5rem; } </style> </head> <body> <h1>Responsive Heading</h1> <p>This paragraph uses clamp() to adjust its font size, width, and padding based on the screen size. Try resizing the browser window to see it change smoothly.</p> </body> </html>
Clamp works with any CSS length unit like rem, px, %, vw, vh.
It helps avoid writing many media queries for simple responsive sizing.
Use clamp() for better control over flexible layouts and typography.
Clamp sets a value that grows and shrinks but stays between a minimum and maximum.
It is great for responsive font sizes, widths, and spacing.
Clamp makes designs flexible and easier to maintain.
Practice
clamp() function do?Solution
Step 1: Understand the purpose of clamp()
The clamp() function sets a value that can grow or shrink but never goes below a minimum or above a maximum.Step 2: Compare options with clamp() behavior
Only It sets a value that stays between a minimum and maximum, adjusting responsively. describes this behavior correctly; others describe unrelated CSS features.Final Answer:
It sets a value that stays between a minimum and maximum, adjusting responsively. -> Option DQuick Check:
Clamp controls value limits = A [OK]
- Thinking clamp fixes a value to one size
- Confusing clamp with color or visibility properties
- Assuming clamp only sets minimum or maximum, not both
Solution
Step 1: Recall clamp() parameter order
The clamp() function takes three parameters: minimum value, preferred value, and maximum value, in that order.Step 2: Match parameters to options
Only clamp(min, preferred, max) follows the correct order: min, preferred, max.Final Answer:
clamp(min, preferred, max) -> Option AQuick Check:
Clamp syntax = min, preferred, max [OK]
- Swapping min and max values
- Putting preferred value first or last incorrectly
- Using clamp with only two parameters
font-size: clamp(1rem, 2vw, 3rem);
Assume 1rem = 16px and 1vw = 1% of viewport width.
Solution
Step 1: Calculate each clamp parameter in pixels
Minimum: 1rem = 16px; Preferred: 2vw = 2% of 500px = 10px; Maximum: 3rem = 48px.Step 2: Determine which value clamp chooses
Clamp picks the preferred value (10px) but keeps it between min (16px) and max (48px). Since 10px is less than min, clamp returns 16px.Final Answer:
16px -> Option BQuick Check:
Clamp picks value between min and max = 16px [OK]
- Using preferred value directly without limits
- Confusing vw units with rem
- Ignoring min and max boundaries
width: clamp(300px, 50%, 200px);
Solution
Step 1: Compare min and max values
The minimum is 300px, and the maximum is 200px. Minimum cannot be larger than maximum.Step 2: Validate clamp parameter rules
Clamp requires min ≤ preferred ≤ max. Here min > max, which is invalid.Final Answer:
Minimum value is larger than maximum value. -> Option AQuick Check:
Clamp min ≤ max rule violated = B [OK]
- Ignoring order of min and max values
- Thinking percentages can't be used as preferred
- Believing clamp accepts only two parameters
Solution
Step 1: Identify clamp parameter order
Clamp requires parameters in order: minimum, preferred, maximum.Step 2: Match values to parameters
Minimum padding is 1rem, preferred is 5vw (scales with viewport), maximum is 4rem.Step 3: Check options for correct order
Only padding: clamp(1rem, 5vw, 4rem); matches the correct order and values.Final Answer:
padding: clamp(1rem, 5vw, 4rem); -> Option CQuick Check:
Clamp(min=1rem, preferred=5vw, max=4rem) = A [OK]
- Mixing order of parameters
- Putting max before min
- Using fixed units for preferred value only
