In CSS, math functions allow you to perform mathematical calculations directly in your style declarations. They provide a convenient way to dynamically set values based on calculations, making it easier to create responsive and flexible designs. CSS math functions were introduced in CSS3 and are supported in modern web browsers.

There are four types of math functions in CSS:

  1. calc()
  2. min()
  3. max()
  4. clamp()

Let's explore each of these math functions with detailed examples:

1. calc() Function: The calc() function allows you to perform mathematical operations to calculate values for CSS properties. It supports addition (+), subtraction (-), multiplication (*), and division (/).

Example:

<!DOCTYPE html>
<html>
<head>
  <title>calc() Function Example</title>
  <style>
    div {
      width: calc(100% - 20px);
      height: calc(50vh - 50px);
      padding: 10px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div>This div has width 100% - 20px and height 50vh - 50px.</div>
</body>
</html>

 

In this example, we use the calc() function to dynamically calculate the width and height of the div element. The width is set to 100% of its parent's width minus 20 pixels, and the height is set to 50% of the viewport height minus 50 pixels. This allows the div to adjust its size based on the available space.

2. min() Function: The min() function selects the minimum value from a list of expressions and assigns it to the property.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>min() Function Example</title>
  <style>
    p {
      font-size: min(24px, 4vw);
    }
  </style>
</head>
<body>
  <p>This paragraph will have a font size of either 24px or 4vw, whichever is smaller.</p>
</body>
</html>

 

In this example, the min() function is used to set the font size of the paragraph. The font size will be the smaller value between 24 pixels and 4% of the viewport width (vw). This ensures that the font size doesn't become too large on smaller screens.

3. max() Function: The max() function selects the maximum value from a list of expressions and assigns it to the property.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>max() Function Example</title>
  <style>
    p {
      font-size: max(16px, 2vw);
    }
  </style>
</head>
<body>
  <p>This paragraph will have a font size of either 16px or 2vw, whichever is larger.</p>
</body>
</html>

 

In this example, the max() function sets the font size of the paragraph. The font size will be the larger value between 16 pixels and 2% of the viewport width (vw). This ensures that the font size doesn't become too small on larger screens.

4. clamp() Function: The clamp() function takes three values as arguments: the minimum value, the preferred value, and the maximum value. It selects the preferred value within the range of the minimum and maximum values.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>clamp() Function Example</title>
  <style>
    p {
      font-size: clamp(16px, 4vw, 24px);
    }
  </style>
</head>
<body>
  <p>This paragraph will have a font size of at least 16px, but not exceed 24px. It will also scale with viewport width between 4vw and 24px.</p>
</body>
</html>

 

In this example, the clamp() function is used to set the font size of the paragraph. The font size will be a preferred value of 4% of the viewport width (4vw) when the viewport is wider, but it won't exceed 24 pixels. If the viewport is narrower, the font size will be at least 16 pixels. This allows the font size to scale with the viewport width within a specific range.

CSS math functions are a powerful tool for creating responsive and dynamic designs. They enable you to apply complex calculations to your styles, making it easier to handle varying screen sizes and create visually engaging websites.

Latest Tutorial

Newly published tutorials

How to Make an AWS S3 Bucket Public

Learn how we can make an AWS S3 bucket public

Laravel 10 - Send mail using AWS SES

Learn to send Email using Laravel 10 with AWS SES service.

Laravel - How to upload file to AWS S3 Bucket

Explore how we can upload file using the laravel on Amazon S3 bucket.