The calc function in CSS is a powerful tool that allows you to perform mathematical calculations when specifying CSS property values. It accepts a mathematical expression as its argument and returns the result of the calculation as the value of the CSS property. This is useful for creating more dynamic and flexible layouts, especially in responsive web design, as it enables the combination of different unit types in calculations.
Syntax The basic syntax of the calc() function is as follows:
property: calc(expression);
The expression can include the following mathematical operators:
- + (addition)
- – (subtraction)
- * (multiplication)
- / (division)
It is important to note that whitespace is required around the + and – operators within the calc() function. For example, calc(50% – 10px) is correct, while calc(50%-10px) is invalid. Multiplication (*) and division (/) operators do not require spaces around them. You can also use parentheses to specify the order of operations if needed.
What is the use of function in Calc?
- Combining different units: One of the most important benefits is the ability to perform calculations with different types of CSS units, such as percentages, pixels, ems, rems, and viewport units. This was previously difficult or impossible in CSS without resorting to preprocessors or JavaScript.
- Dynamic layouts: calc() helps in creating layouts where the size or position of an element depends on other factors, like the size of the parent container or the viewport.
- Responsive design: It is particularly useful in responsive web design where you need elements to adapt to various screen sizes.
Example1:
Calculating Width with Percentage and Fixed Units Imagine you want an element to occupy 100% of its parent’s width but with a fixed amount of padding on both sides. You can use calc() for this:
.header {
width: calc(100% - 40px); /* 100% width minus 40px (20px left + 20px right padding) */
padding: 20px;
background-color: steelblue;
color: white;
}
The header’s content area always spans the full width minus the specified padding, regardless of the parent’s width.
Example 2:
Centering Elements You can use calc() along with absolute positioning to center an element both horizontally and vertically:
.center {
position: absolute;
top: calc(50% - 25px); /* 50% from the top minus half of the element's height (50px / 2) */
left: calc(50% - 25px); /* 50% from the left minus half of the element's width (50px / 2) */
width: 50px;
height: 50px;
background: red;
}
Example 3:
Adjusting Width Based on Other Element’s Properties If you have two sibling elements where the second element’s width should be the remaining space after considering the first element’s width, margin, and padding, calc() is very helpful:
.sidebar {
width: 30%;
margin-right: 2em;
padding: 1em;
float: left;
background-color: lightgray;
}
.main-content {
width: calc(70% - 3em); /* Remaining 70% minus the sidebar's 2em margin and 1em padding */
float: left;
background-color: white;
}
Example 4:
Responsive Font Sizes with vw Units You can combine relative units like vw (viewport width) with fixed units or ems to create font sizes that scale with the viewport but also have a minimum or base size:
:root {
font-size: calc(0.5em + 1vw); /* Base font size of 0.5em plus 1% of the viewport width */
}
This will make the font size adjust smoothly as the browser window is resized, preventing it from becoming too small on smaller screens or excessively large on very wide screens.
The calc() function is a fundamental part of modern CSS, enabling more sophisticated and responsive web layouts by allowing mathematical operations directly within your stylesheets. While older browsers might have required vendor prefixes like-moz-calc() in the past, it is now widely supported without prefixes.
CSS3 Topics