
CSS Animations with @keyframes and animation
29/07/2023
Intermediate and Advanced CSS Exercises: 7 Practical Tasks
01/08/2023
CSS math functions calculate values for properties. This table summarizes the functions covered here and related operations. The later examples show how to use them in style rules.
| Property | Description | Values and examples |
|---|---|---|
abs() | Returns the absolute value of a number | abs(-10) = 10 |
sin() | Returns the sine of an angle | sin(45deg) ≈ 0.71 |
cos() | Returns the cosine of an angle | cos(60deg) = 0.5 |
tan() | Returns the tangent of an angle | tan(30deg) ≈ 0.58 |
sqrt() | Returns the square root of a number | sqrt(16) = 4 |
pow() | Raises a number to a power | pow(2, 3) = 8 |
min() | Returns the smallest of the values | min(10, 5) = 5 |
max() | Returns the largest of the values | max(10, 5) = 10 |
random() | Returns a random value within a given range where supported | random(0, 1) → a value between 0 and 1 |
round(down, …) | Use round(down, value, step) to round toward negative infinity | round(down, 3.8, 1) = 3 |
round(up, …) | Use round(up, value, step) to round toward positive infinity | round(up, 3.2, 1) = 4 |
round() | Rounds a value to an interval using the requested strategy | round(nearest, 3.7, 1) = 4 |
clamp() | Clamps a preferred value between a minimum and a maximum | clamp(2px, 5px, 4px) = 4px |
calc() | Computes a CSS value using arithmetic | width: calc(100% - 20px) |
attr() | Reads an HTML attribute for use in CSS | content: attr(data-text) |
1. CSS math functions
The following sections cover arithmetic, minimum and maximum values, rounding, and reading values from HTML attributes.
2. calc()
Use calc() for arithmetic, including calculations that combine compatible units such as lengths and percentages.
Example 1 — addition: Add 100px and 20px to get 120px.
width: calc(100px + 20px); /* Result: 120px */
Example 2 — subtraction: Subtract 10px from 80% of the available width.
width: calc(80% - 10px); /* 80% of the available width minus 10px */
Example 3 — multiplication: Double 50% of the available width.
width: calc(2 * 50%); /* Twice 50% of the available width */
Example 4 — multiple operations: Add 100px to 10% of the available width, then divide the result by three.
width: calc((100px + 10%) / 3); /* Sum first, then divide by three */
Practical example:
Use calc() to create one fixed-width box and one box that fills the remaining width.
3. min()
Use min() to select the smallest of the values after the browser resolves their units.
Example 1: Choose the smaller of 200px and 50% of the available width.
width: min(200px, 50%); /* Smaller of the two widths */
Example 2: Choose the smallest of 100px, 150px, and half of a definite containing-block height.
height: min(100px, 150px, 50%); /* Smallest of the three heights */
Example 3: Choose the smaller of 16px and 1.2 times the inherited font size.
font-size: min(16px, 1.2em); /* Smaller of the two font sizes */
4. max()
Use max() to select the largest of the supplied values.
Example 1: Choose the larger of 200px and 50% of the available width.
width: max(200px, 50%); /* Larger of the two widths */
Example 2: Choose the largest of 100px, 150px, and half of a definite containing-block height.
height: max(100px, 150px, 50%); /* Largest of the three heights */
Example 3: Choose the larger of 16px and 1.2 times the inherited font size.
font-size: max(16px, 1.2em); /* Larger of the two font sizes */
5. round()
Use round() to round a value to a multiple of an interval. Specify nearest, up, down, or to-zero as the strategy.
Example 1: Round 3.7rem to the nearest 1rem to get 4rem.
width: round(nearest, 3.7rem, 1rem); /* Result: 4rem */
Example 2: Round −2.3em to the nearest 1em to get −2em. Apply the result to a property that accepts negative values, such as a margin.
margin-left: round(nearest, -2.3em, 1em); /* Result: -2em */
Example 3: To round a unitless number to two decimal places, use an interval of 0.01. Here 1.2345 becomes 1.23.
line-height: round(nearest, 1.2345, 0.01); /* Result: 1.23 */
6. attr()
attr() reads an HTML attribute. An untyped call is commonly used in content. For numeric declarations, specify a unit or type and provide a fallback. Check support for typed attr() in your target browsers.
Example 1: Interpret data-width as a pixel value, with a fallback of 200px if the attribute is unavailable or invalid.
width: attr(data-width px, 200px);
Example 2: Read data-height as a pixel value and double it within calc().
height: calc(attr(data-height px, 100px) * 2);
Example 3: Use the string value of data-content as generated text for a pseudo-element.
content: attr(data-content);
The final demo uses data-author to display an author name. Keep essential authorship in accessible HTML on a real site.