CKTIS / CSS
CSS values, functions, and units
A CSS value must match the syntax of its property. Learn the difference between numbers, dimensions, and percentages, the basics of relative and absolute units, and calculations with calc(), min(), max(), and clamp().
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
property: value;
width: calc(50% - 16px);
font-size: clamp(16px, 2vw, 24px);In a property: value; declaration, the property determines the accepted values: keywords such as auto, numbers, lengths, colors, or functions. Documentation notation such as <length> names a type; it is not text to enter in a stylesheet. Not every property accepts every value type.
A <number> is unitless, for example 1.5. A dimension combines a number and a unit without whitespace: 16px, not 16 px. Lengths use units such as px, angles use deg, and time uses ms or s. A zero length can usually be written as 0; this does not mean units can be omitted from other numbers.
px is an absolute CSS unit, not a promise of one physical screen pixel. em depends on the element’s font size, but in font-size it refers to the inherited size. rem refers to the root element’s font size, except in the root element’s own font-size declaration, where it refers to the initial value of font-size, not the resulting font size. vw equals 1% of the large viewport width. A percentage is not a universal length: its reference quantity is property-specific, so it is not always a parent dimension.
calc() calculates with compatible types; calc(50% - 16px) can specify a width, but adding a length to a time is invalid. Surround + and - operators with whitespace. min() selects the smallest value, max() the largest, and clamp(minimum, preferred, maximum) bounds the preferred value; if the bounds conflict, the minimum wins. A function’s presence in a CSSWG draft does not prove browser support—the example needs separate verification.
Examples
<!doctype html>
<meta charset="utf-8">
<title>CSS values and units</title>
<style>
.container { width: 400px; }
.box {
width: calc(50% - 16px);
height: 32px;
background: teal;
}
</style>
<div class="container"><div class="box"></div></div>
<script>
console.log(getComputedStyle(document.querySelector('.box')).width);
</script>Result
184px
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.