CSS max() Function

The max() function in CSS is used to set the maximum of the numbers given. It is used to return the largest value from a set of comma-separated values. It can accept length, frequency, integer, angle, and time types of values.

Syntax:

max(value1, value2...);

Parameters: It takes any number of parameters.

  • Values: It is the set of comma-separated values from which the largest one is chosen.

Return Value: This function returns the largest value from a set of comma-separated values.

Example 1: This example demonstrates the above mentioned function when only 2 parameters are given.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <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>
</head>
  
<body>
    <div class="container">
        <p>Geeks for geeks</p>
    </div>
</body>
  
</html>

chevron_right


Output:

Example 2: When more than two parameters are given in max() function.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <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>
</head>
  
<body>
    When nested max function is used
    <div class="container">
        <p>Geeks for geeks</p>
    </div>
</body>
  
</html>

chevron_right


Output:

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.