VOOZH about

URL: https://www.geeksforgeeks.org/css/css-calc-function/

⇱ CSS calc() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSS calc() Function

Last Updated : 11 Jun, 2026

The CSS calc() function is used to perform mathematical calculations to determine property values. It allows combining different units such as pixels, percentages, ems, and rems in a single expression, making layouts more flexible and responsive.

  • Performs mathematical operations such as addition (+), subtraction (-), multiplication (*), and division (/).
  • Allows combining different CSS units in a single calculation.
  • Commonly used for responsive layouts, spacing, sizing, and positioning.

Syntax:

calc( Expression )

Parameter:

  • expression: A mathematical expression used to calculate a CSS value. It can contain numbers, units, and the operators +, -, *, and /.

Operators:

  • + : Adds values.
  • - : Subtracts values.
  • * : Multiplies values.
  • / : Divides values.

Example: Illustrates the calc() function in CSS

 Understanding the working of the calc() function in CSS

The calc() function in CSS does calculations based on the values of their parent element or the values provided by the user during the calculation. 

Let's understand the work in more depth using some examples:

Example 1: The calc() function sets the heading width to 50% of its parent element's width minus 100 pixels, combining relative and fixed values in a single calculation.

  • The calc() function is used to set the width of the heading dynamically.
  • 50% represents half of the parent element's width, while 100px is a fixed value.
  • calc(50% - 100px) sets the heading width to 50% of the parent width minus 100 pixels, combining a relative value with a constant value.

Example 2: The calc() function combines percentage and fixed values to set responsive widths for the parent container and heading.

Comment