Skip to content
Related Articles
Open in App
Not now

Related Articles

CSS | calc() Function

Improve Article
Save Article
Like Article
  • Last Updated : 01 Jul, 2022
Improve Article
Save Article
Like Article

The calc() function is an inbuilt function in CSS which 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 expression which need to implement. The operators used by this parameters are: +, -, *, /
Below program illustrates the calc() function in CSS:
Program: 
 

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 calculation based on the values of their parent element or the values provided by the user during the calculation. 

Let’s understand the working in more depth using some example :

Example 1:

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:

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 26
  • Edge 12
  • Internet Explorer 9
  • Firefox 16
  • Opera 15
  • Safari 7

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!