×

CSS calc()

CSS calc(): The CSS calc() function is a CSS function of inbuilt type, which permits us to implement calculations. It is used for calculating the angle, integer frequency, number, time, percentage, and length. It also uses some arithmetic operators such as add (+), subtract (-), divide (/), and multiply (*).

It is an efficient concept of CSS because it permits us to incorporate the units, like pixels and percentages.

Syntax:

calc( Expression );  

Property Values

This function in CSS holds any individual parameter expression. The outcome of such an expression can be recycled as any value. It can be a simple expression with the use of the arithmetic operators (+, -, *, /), and it is essential to define.

Key Points

  • Some arithmetic operators like subtract (-) and add (+) must always be enclosed by the whitespace. Otherwise, the expression is treated as any invalid expression. For example, the expression calc(60%-4px) is invalid because this expression is interpreted as the percentage, pursued by any negative length. Let’s take another expression, calc(60%- 4px) is interpreted as any length and any subtraction operator.
  • But the operators / and * do not require any whitespace, although it is suggested to insert them for consistency.
  • The calc()function nesting is possible.

Example:

In the following example, we will use the function calc() to specify the height and the width of a div component. We will use the subtraction in the calc() function along with the similar units.

The expression’s result will be defined as the property value. Therefore, the height value is 275px, and the width value is 75%.

<!DOCTYPE html>
<html>
<head>
<title> CSS calc() function </title>
<style>
.exp
{
 width: calc(150% - 75%);
 height: calc(350px - 75px);
 background-color: lime;
 padding-top: 50px;
}
.exp1
{
 font-size: 30px;
 font-weight: bold;
 color: green;
}
h1
{
 color: green;
}
h2
{
 color: red;
}
</style>
</head>
<body>
<center>
<div class= "exp">
<div class= "exp1"> Welcome to this Page </div>
<h1> It is an illustration of CSS calc() function </h1>
<h2> width: calc(150% - 75%); </h2>
<h2> height: calc(350px - 75px); </h2>
</div>
</center>
</body>
</html>

From the above example, it is possible to define the height and width values directly. But the expression within this example contains similar units. If the units are different, it will be difficult to write the values directly.

Output:

CSS calc()

Let’s take another illustration where we will use some mixed units.

Example: using mix units

<!DOCTYPE html>
<html>
<head>
<title> CSS calc() function </title>
<style>
.exp
{
 width: calc(40% + 10em);
 height: calc(350px + 75px);
 background-color: lime;
 padding-top: calc(10% - 10px);
 padding-left: calc(10% + 10px);
}
.exp1
{
 font-size: 30px;
 font-weight: bold;
 color: green;
}
h1
{
 color: green;
}
h2
{
 color: red;
}
</style>
</head>
<body>
<div class= "exp">
<div class= "exp1"> Welcome to this Page </div>
<h1> It is an illustration of CSS calc() function </h1>
<h2> width: calc(40% + 10em); </h2>
<h2> height: calc(350px + 75px); </h2>
<h2> padding-top: calc(10% - 10px); </h2>
<h2> padding-left: calc(10% + 10px); </h2>
</div>
</body>
</html>

Output:

CSS calc()


Let’s take another illustration where we will use some mixed units.

Example: calc() nested function

<!DOCTYPE html>
<html>
<head>
<title> CSS calc() function </title>
<style>
.exp
{
 width: calc(calc(40em / 3) * 2);
 height: calc(350px + 75px);
 background-color: lime;
}
.exp1
{
 font-size: 30px;
 font-weight: bold;
 color: green;
}
h1
{
 color: green;
}
h2
{
 color: red;
}
</style>
</head>
<body>
<div class= "exp">
<div class= "exp1"> Welcome to this Page </div>
<h1> It is an illustration of CSS calc() nested function </h1>
<h2> width:  calc(calc(40em / 3) * 2); </h2>
<h2> height: calc(350px + 75px); </h2>
</div>
</body>
</html>

Output:

CSS calc()

Related Topics

CSS Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

CSS Float

CSS Float: The float property of CSS is the positioning property. This property is applied to elements to push them either to left or right. It permits for wrapping facility...

4 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

CSS Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

CSS calc()

CSS calc(): The CSS calc() function is a CSS function of inbuilt type, which permits us to implement calculations. It is used for calculating the angle, integer frequency, number, time,...

3 minutes read.

CSS Filter

CSS Filter CSS filter is used to apply visual effects on the images, text, and other factors of the webpage. This filter property in CSS allows us to admit the effects like...

7 minutes read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 minutes read.

CSS Letter-spacing

CSS Letter-spacing: Letter-spacing attribute is applied to manage the space among all letters within the block or element of the text. This property sets the behavior of the spacing between...

2 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

Css Text Orientation

Introduction: The text-orientation property of CSS is used to specify the orientation of the text characters in the line of content. This property applies only with the vertical writing-mode and not...

4 minutes read.

CSS Opacity

The CSS Opacity property is used to set the clarity of the image. It is defined as degree and specify the transparency of an element. Note: Opacity value will be less...

1 minute read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.