The Wayback Machine - https://web.archive.org/web/20250115224759/https://www.geeksforgeeks.org/css-max-function/
Open In App

CSS max() Function

Last Updated : 30 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Output:

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

HTML
<!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>

Output:

Supported Browsers:

  • Chrome 79 and above
  • Edge 79 and above
  • Internet Explorer not supported
  • Firefox 75 and above
  • Opera 66 and above
  • Safari 11.1 and above

CSS max() Function – FAQs

What does the max() function do in CSS?

The max() function allows you to set a value based on the maximum of two or more arguments, which can be used for responsive designs to ensure elements do not shrink below a certain size.

How do you use the max() function in CSS?

Use max() in CSS properties like width or height, for example: width: max(50%, 300px);, to select the larger value between 50% and 300px.

Can max() be combined with other CSS functions?

Yes, max() can be combined with functions like calc() or min() for more complex calculations.

What are common use cases for the max() function in CSS?

The max() function is used to ensure minimum sizes for elements in responsive designs, preventing elements from becoming too small on large screens.

Is the max() function supported in all browsers?

The max() function is supported in most modern browsers, but it is always good to check for the latest browser compatibility updates.


Dive deep into the world of JavaScript with our JavaScript Full Course Online, offering everything from the basics to advanced techniques. Learn how to build interactive websites, optimize your code, and earn a certification to showcase your skills.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Push your limits and stay motivated as you work towards mastering JavaScript. Join now and get started on your JavaScript learning journey!


Next Article

Similar Reads

three90RightbarBannerImg