The CSS max() function returns the largest value from a list of comma-separated values. It is commonly used in responsive designs to ensure that an element's size does not go below a specified minimum value.
- Returns the largest value from the specified list of values.
- Allows combining different units such as pixels, percentages, and viewport units.
- Helps create flexible and responsive layouts by choosing the maximum value automatically.
Syntax:
max(value1, value2, ...);Parameters:
- values: A set of comma-separated values from which the largest value is selected.
Return Value:
- Returns the largest value from the specified set of comma-separated values.
Examples of CSS max() Function
Here are some examples discussed:
Example 1: When only 2 parameters are given.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<!--Driver Code Ends-->
<style>
.container {
display: flex;
justify-content: center;
align-content: center;
color: white;
font-size: 30px;
background-color: green;
width: max(100px, 400px);
height: max(100px, 400px);
}
p {
align-self: center;
}
</style>
<!--Driver Code Starts-->
</head>
<body>
<div class="container">
<p>Geeks for geeks</p>
</div>
</body>
</html>
<!--Driver Code Ends-->
Example 2: When more than two parameters are given in max() function.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<!--Driver Code Ends-->
<style>
.container {
display: flex;
justify-content: center;
align-content: center;
color: white;
font-size: auto;
background-color: green;
width: max(10px, max(max(10px, 40px),
max(10px, 150px)));
height: max(10px, max(max(10px, 40px),
max(10px, 100px)));
}
p {
align-self: center;
}
</style>
<!--Driver Code Starts-->
</head>
<body>
When nested max function is used
<div class="container">
<p>Geeks for geeks</p>
</div>
</body>
</html>
<!--Driver Code Ends-->