Skip to content

The CSS calc() function

The Valley of Code

Your Web Development Manual

Learn how to work with the CSS calc() function

The calc() function lets you perform basic math operations on values, and it’s especially useful when you need to add or subtract a length value from a percentage.

This is how it works:

div {
	max-width: calc(80% - 100px)
}

It returns a length value, so it can be used anywhere you expect a pixel value.

You can perform

One caveat: with addition and subtraction, the space around the operator is mandatory, otherwise it does not work as expected.

Examples:

div {
	max-width: calc(50% / 3)
}
div {
	max-width: calc(50% + 3px)
}
β†’ Get my CSS Handbook
β†’ Read my CSS Tutorial on The Valley of Code
  • THE VALLEY OF CODE (+ PRO), your web development manual
  • I wrote 15+ free coding BOOKS, download them here
  • SOLOPRENEUR LAND the missing MBA for wannabe solopreneurs craving a life with more freedom, control, fulfillment and purpose (summer 2024)