Open In App
Related Articles

CSS calc() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The calc() function is an inbuilt function in CSS that is used to perform calculations based on CSS property.

Syntax: 

calc( Expression )

Parameters: This function accepts single parameter Expression which is mandatory. This parameter contains mathematical expressions which need to implement. The operators used by this parameters are: +, -, *, /

Example: The below program illustrates the calc() function in CSS:

html

<!DOCTYPE html>
<html>
 
<head>
    <title>calc function</title>
    <style>
        .geeks {
            position: absolute;
            left: 50px;
            width: calc(100% - 20%);
            height: calc(500px - 300px);
            background-color: green;
            padding-top: 30px;
            text-align: center;
        }
         
        .gfg {
            font-size: 40px;
            font-weight: bold;
            color: white
        }
         
        h1 {
            color: white;
        }
    </style>
</head>
 
<body>
    <div class="geeks">
        <div class="gfg">GeeksforGeeks</div>
        <h1>The calc() Function</h1>
    </div>
</body>
 
</html>

                    

Output: 

Understanding the working of the calc() function in CSS:

The calc() function in CSS does calculations based on the values of their parent element or the values provided by the user during the calculation. 

Let’s understand the work in more depth using some examples:

Example 1: This example shows the working of the calc() function in CSS.

HTML

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=">
    <title>Calc Function</title>
    <style>
        .parent{
            background-color: skyblue;
           
        }
        .heading {
         
            left: 30px;
            width: calc(50% - 100px);
            height: 100px;
            background-color: #579dcf;
            padding-top: 20px;
            text-align: center;
        }
         
        h2 {
            color: #e9e8e9;
        }
    </style>
</head>
 
<body>
    <br>
    <div class="parent">
        <div class="heading">
            <h2>
              Welcome to GEEKSFORGEEKS.
              </h2>
        </div>
    </div>
</body>
 
</html>

                    

In the above code example, the calc() function is used to give the width value to the heading. Here , we have used the value of the parent which is set to 100% of the screen width by default.  The calc(50% – 100px) means that the width of the heading will be equal to “50% of the width of the parent – 100 px”. Thus , we here use the calc() function with both the value from parent and a constant value.

Here is the output of the code:

Example 2: This example shows the working of the calc() function in CSS.

HTML

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=">
    <title>Calc Function</title>
    <style>
        .parent{
            background-color: skyblue;
            width: calc(80% - 2em);
        }
        .heading {
         
            left: 30px;
            width: calc(50% - 100px);
            height: 100px;
            background-color: #579dcf;
            padding-top: 20px;
            text-align: center;
        }
         
        h2 {
            color: #e9e8e9;
        }
    </style>
</head>
 
<body>
    <br>
    <div class="parent">
        <div class="heading">
            <h2>
              Welcome to GEEKSFORGEEKS.
              </h2>
        </div>
    </div>
</body>
 
</html>

                    

In the above code example, we have used the calc() function twice. First, using the calc() function we have defined the value of the parent div. Then, using the value of the parent div element and using the calc() function, we define the value of the heading div element. Thus, it is clear that calc() function present in the parent element can access the value produced by the child calc() function.

Here is the output of the code:

Supported Browser:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari


Last Updated : 04 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads