Comparison Functions

This function contains all the comparison function which is used in the CSS Function. It contains max(), min(), and clamp() functions.

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

Example: In this example, we will use the max() CSS 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>
              Welcome to w3wiki
          </p>
    </div>
</body>
  
</html>


Output:

min() Function: The min() function in CSS is used to extract the minimum value from a set of comma-separated values. It can take two parameters and a min function can be used inside another min function if the comparison is to be made between multiple values.

Syntax:

min(value1, value2);
min(value1, min(value2, min(value3, value4)));

Example: In this example, we will use the CSS min() Function.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Document</title>
  
    <style>
        /* CSS for the html */
        * {
            font-family: 'Times New Roman', Times, serif;
            font-size: min(20px, 1000px);
            font-stretch: narrower;
            font-weight: 600;
        }
  
        .box {
            display: flex;
            color: white;
            background-color: green;
            justify-content: center;
            height: min(20vh, min(30vh, min(40vh, 50vh)));
            width: min(50vw, min(50vw, min(40vw, 50vw)));
        }
  
        h2 {
            align-self: center;
        }
    </style>
</head>
  
<body>
    When nested min function is
    used with different units
    <div class="box">
        <h2>Geeks for geeks</h2>
    </div>
</body>
  
</html>


Output:

clamp() Function: The clamp() method is used to clamp the value between an upper and lower bound. The minimum value comes in handy when the preferred value is smaller than the minimum value similarly maximum value comes in handy when the preferred value is more than the maximum value. It calculates the central value of maximum and minimum values.

Syntax:

clamp(value1, value2, value3)

Example:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        /* Setting clamp property of heading */
        h1 {
            font-size: clamp(2rem, 4vw, 4rem);
            color: #075e2d;
        }
  
        /* Setting clamp property of box */
        .box {
            width: clamp(150px, 50%, 400px);
            height: 8rem;
            background: #5fe863;
        }
    </style>
</head>
  
<body>
    <div class="container">
        <h1>Welcome To GFG</h1>
        <div class="box"></div>
    </div>
</body>
  
</html>


Output:

CSS Math Functions

In this article, we will learn about the CSS Math Functions. CSS Math Functions enable you to express CSS numeric values using mathematical expressions directly in your stylesheets. By using CSS Math Functions, we achieve greater flexibility and ease in creating responsive layouts and styling elements. We can use calc(), max(), and min() math functions to make our work easy.

CSS Math Functions are classified into the following types:

  • Basic arithmetic
  • Comparison functions
  • Trigonometric functions
  • Exponential functions
  • Sign-related functions
  • Stepped value functions

Similar Reads

Basic Arithmetic

The basic arithmetic CSS Math function is calc()....

Comparison Functions

...

Trigonometric functions

This function contains all the comparison function which is used in the CSS Function. It contains max(), min(), and clamp() functions....

Exponential functions:

...

Sign-related functions:

...

Stepped value functions:

...

Contact Us