Calculations in CSS

Learn how to perform math in CSS using the calc() function.

The calc function

The calc() function lets us perform simple calculations in CSS. We can work with addition (+), subtraction (-), multiplication (*), and division (/) operators. The most useful feature of this function is the ability to mix units. For example, we could use it to subtract pixels from the viewportviewport width:

main {   
   width: calc(100vh - 80px); 
}

In this example, the main element has its height determined by a calculation of 100vh (viewport height percentage) minus 80px (pixels).

Get hands-on with 1200+ tech skills courses.