DEV Community

Soham
Soham

Posted on

What is calc() function in CSS ?

Explanation:
calc() function used to perform the calculation with specifying CSS property values. It can be used in length, frequency, time, and so on.
Syntax:
There are many syntax to define calc() function but the most popular is
calc() where = [ ‘+’ or ‘-’ ]
For more explanation look at this bellow example
HTML code:
<div id="ball">Soham</div>

CSS code :

#ball{
display:block;
margin:0 auto;
position:absolute;
width:calc(100% - 50px);
padding:10px;
text-align:center;
border:2px solid black;
background:red;
box-shadow:3px 3px 3px #000;
transition:1.5s;}

Output:

Image without hover

CSS code explanation:
Here I make a div which id is #ball. For styling the component add
border of 2px solid with black color
add padding 10px
For center the text add text-align: center
Now lets understand
width: calc(100% — 50px)
Above calc() property define that #ball element width 100% and have 50px padding from both side left and right
Now when hover on this #ball div it’s background color became yellow and width:calc(90% — 50%)
Code:
#ball:hover{
width:calc(90% - 50%);
background:yellow;
}

Output:

Image using hover

Here its width becomes 90% or less and 50% padding from both sides
Important Note:
calc(90% — 50%) you must give whitespace between 90% and 50% otherwise property does not work
For * and / operator whitespace optional

You can visit my code pen for full Code
👉https://codepen.io/Soham23/pen/oNGbBOb

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay