CSS Introduction

CSS Tutorial What is CSS CSS Syntax CSS Selector How to include CSS CSS Comments

CSS Attributes

CSS Background CSS Border CSS Display CSS Float CSS Font CSS Color CSS Hover CSS Important CSS Line-height CSS Margin CSS Opacity CSS Filter CSS Images CSS Overflow CSS Padding CSS Position CSS Vertical align CSS White space CSS Width Word-wrap in CSS Box-shadow in CSS Text-transform in CSS CSS Outline CSS Visibility CSS Counters CSS Clear fix CSS Icons CSS Justify-content Text-decoration in CSS CSS Lists CSS nth selector CSS Sticky CSS Background-clip CSS Checkbox-style CSS Letter-spacing CSS Navigation bar CSS Overlay CSS Root CSS Specificity CSS Text-indent CSS Text-stroke CSS Zoom CSS Order CSS Descendent selector CSS Clip CSS calc() CSS Background-blend-mode CSS radio-button CSS Superscript and subscript CSS Text-effects CSS Text-align CSS Variables CSS Page-break-before CSS Page-break-inside CSS Page-break-after CSS Content property CSS Word-spacing CSS Animation CSS @keyframes rules CSS Pseudo-classes CSS Pseudo-elements CSS Radial-gradient CSS Translate CSS Gradients CSS z-index CSS Loaders CSS Units CSS Transition CSS Masking CSS Arrow CSS Pagination

Misc

What is Bootstrap CSS What is CSS used for Difference between HTML and CSS How to center a table in CSS What is a CSS File How to center a button in CSS How to change background color in CSS How to change the font in CSS How to change font size in CSS How to resize an image in CSS How to get rid of bullet pioints in CSS Is CSS a programming language Grid Vs Flexbox in CSS How to edit CSS in WordPress How to use google fonts in CSS

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()



ADVERTISEMENT
ADVERTISEMENT