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
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.