CSS Reference ยท Units and Values
How to read CSS value syntax
Learn to read CSS value definition syntax: angle-bracket types, juxtaposition, |, ||, &&, brackets and the multipliers *, +, ?, {A,B}, # and !.
Every CSS property has a formal syntax: a compact line that describes exactly which values it accepts. You see it in the Syntax section of each reference page and in the specs. At first it looks cryptic, but it uses only a handful of symbols, and once you know them you can predict which declarations are valid without trial and error.
This is a concept page. It teaches the notation using real properties.
margin: [ <length-percentage> | auto ]{1,4} border: <line-width> || <line-style> || <color> transition: <single-transition>#
Try it
The notation
| Symbol | Name | Meaning | Example |
|---|---|---|---|
keyword |
Literal | Written exactly as shown. | auto, none |
<type> |
Data type | Any value of that type. | <length>, <color>, <number> |
<'property'> |
Property reference | Whatever that property accepts. | <'border-width'> |
A B |
Juxtaposition | All components, in this order. | <length> <length> |
A && B |
And | Both, in any order. | <line-width> && <line-style> |
A || B |
Or | One or more, in any order. | <line-width> || <line-style> || <color> |
A | B |
Exclusive or | Exactly one. | auto | none |
[ ... ] |
Grouping | Treats the contents as one component. | [ <length> | auto ] |
* |
Zero or more | The component may repeat. | <length>* |
+ |
One or more | At least once. | <length>+ |
? |
Optional | Zero or one time. | <color>? |
{A,B} |
Count | At least A, at most B times. | <length>{1,4} |
# |
Comma list | One or more, separated by commas. | <time># |
! |
Required group | After a group, the group must produce at least one value. | [ <a>? <b>? ]! |
Precedence from tightest to loosest is: juxtaposition, then &&, then ||, then |. Brackets override it.
Reading a real example
margin: [ <length-percentage> | auto ]{1,4} reads: "one to four values, each of which is a length, a percentage or auto". That is why margin: 0 auto is valid, while margin: 1px 2px 3px 4px 5px is not.
A type in angle brackets can carry a range: <length [0,โ]> means a non-negative length, <integer [1,โ]> a positive integer.
Usage notes
- Commas in the syntax are literal. If a comma appears in a definition, you must write it.
- Slash is literal too. In
<bg-position> / <bg-size>the/separates two parts. - Reference pages show simplified syntax. The spec may hide large pieces behind names such as
<bg-layer>; follow those names to see the full pattern. - Order matters only with juxtaposition. If two values can be mixed up (as in
font), the grammar may fix the order. - Invalid values are dropped. A declaration that does not match the grammar is ignored in full, and earlier valid declarations apply.
Browser support
This is a notation for describing CSS, not a feature of browsers. The Houdini @property rule uses a related syntax string for typed custom properties.
Related
marginโ a{1,4}exampleborderโ a||exampletransitionโ a#example@property- Lesson: Syntax