×

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 Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Clip

CSS Clip The Clip property in CSS describes the element’s visible area. It applies over the elements which are absolutely positioned (position: absolute ;). Generally, it is used when an image...

2 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

CSS Inline

What is CSS? CSS is an acronym for Cascading Style Sheets. CSS is the language that we use to style an HTML document. It specifies how HTML components should appear. CSS...

4 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 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 Overlay

CSS Overlay: It measures to cover-up something’s surface along with a coating. CSS overlay sets anything over the top area of other things. It creates a web-page more attractive. The...

8 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 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 Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 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.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Align

CSS Center Align Elements CSS center align is used to create align by using block element. You should specify margin: auto;properties. The element will take up the specified width and the remaining space....

2 minutes read.

How to center a button in CSS

How to center a button in CSS? In the tutorial, we will learn how to center a button in CSS. CSS (Cascading Style Sheets) gives an HTML web page the greatest possible...

4 minutes 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.