Min and max functions help you set sizes that adapt to different screen sizes. They make your design flexible and look good everywhere.
Min and max functions in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
min(value1, value2, ...) max(value1, value2, ...)
min() picks the smallest value from the list.
max() picks the largest value from the list.
width: min(300px, 50vw);
font-size: max(1rem, 2vw);
padding: max(10px, 2%) min(20px, 5vw);
This example shows a blue box. Its width will never be more than 300 pixels or more than half the screen width, whichever is smaller. The font size grows but never gets smaller than 1rem. Padding also adjusts between minimum and maximum values. Resize the browser window to see the changes.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Min and Max Functions Demo</title> <style> body { font-family: Arial, sans-serif; margin: 2rem; } .box { width: min(300px, 50vw); height: 150px; background-color: #4a90e2; color: white; display: flex; align-items: center; justify-content: center; font-size: max(1rem, 3vw); padding: max(10px, 2%) min(20px, 5vw); border-radius: 0.5rem; box-sizing: border-box; text-align: center; } </style> </head> <body> <div class="box" role="region" aria-label="Example box using min and max functions"> Resize the browser window.<br /> The box width and font size change between minimum and maximum values. </div> </body> </html>
Use units like px, rem, vw, or % inside min() and max().
Min and max help make your site look good on phones, tablets, and desktops without extra code.
Test by resizing your browser window or using device simulation in browser DevTools.
min() picks the smallest value, max() picks the largest.
They help create flexible, responsive sizes for elements.
Use them to keep sizes between limits for better design on all devices.
Practice
min() do when used in a style rule?Solution
Step 1: Understand the purpose of
Themin()min()function compares all values inside it and picks the smallest one.Step 2: Compare with other functions
Unlikemax()which picks the largest,min()picks the smallest value.Final Answer:
It selects the smallest value from the given list of values. -> Option DQuick Check:
min()= smallest value [OK]
- Confusing min() with max()
- Thinking min() averages values
- Assuming min() multiplies values
max() function in CSS?Solution
Step 1: Recall CSS function syntax
CSS functions use parentheses () to enclose arguments, separated by commas.Step 2: Check each option's syntax
Only width: max(100px, 50%); uses parentheses and commas correctly:max(100px, 50%).Final Answer:
width: max(100px, 50%); -> Option AQuick Check:
Correct CSS function syntax uses parentheses [OK]
- Using square brackets or curly braces instead of parentheses
- Omitting commas between values
- Writing function name without parentheses
width: min(300px, max(50%, 200px));What will be the computed width if the container is 400px wide?
Solution
Step 1: Evaluate the inner
max()functionmax(50%, 200px)compares 50% of 400px (which is 200px) and 200px. Both are equal, so result is 200px.Step 2: Evaluate the outer
min()functionmin(300px, 200px)picks the smaller value, which is 200px.Final Answer:
200px -> Option BQuick Check:
min(300px, max(50%, 200px)) = 200px [OK]
- Confusing which function to evaluate first
- Miscalculating 50% of container width
- Choosing max value instead of min
height: min(100px max(50%, 150px));Solution
Step 1: Check syntax of
Arguments insidemin()argumentsmin()must be separated by commas. Here,100pxandmax(50%, 150px)are missing a comma.Step 2: Validate nesting and argument types
Nestingmax()insidemin()is allowed. Mixing units like px and % is valid in these functions.Final Answer:
Missing comma between 100px and max() arguments. -> Option CQuick Check:
Arguments in CSS functions must be comma-separated [OK]
- Forgetting commas between arguments
- Thinking nesting functions is invalid
- Believing mixed units cause errors here
min() and max() to achieve this?Solution
Step 1: Understand the requirement
The width should never be less than 150px (minimum) and never exceed 40% viewport width (maximum).Step 2: Analyze width: max(150px, min(40vw, 100%));
max(150px, min(40vw, 100%))means: pick the larger between 150px and the smaller of 40vw or 100%. This ensures width is at least 150px but no more than 40vw.Step 3: Check other options
Options B, C, and D do not correctly enforce the min and max limits as required.Final Answer:
width: max(150px, min(40vw, 100%)); -> Option AQuick Check:
Use max() for minimum size, min() for maximum size [OK]
- Swapping min() and max() roles
- Not nesting functions correctly
- Ignoring viewport units for responsiveness
