CSS Value Types: Keywords, Strings, URLs, Numbers and Percentages
The building blocks of CSS values: keywords and custom identifiers, strings and escapes, url(), images, integers, numbers, percentages and fr, reading MDN's value syntax, out-of-range values, and the stages from declared to computed and used values.
kvi5hnuCode Sprout team member10 min readOn this page
Every property accepts values of certain types: color takes a <color>, width a <length> or <percentage>, z-index an <integer>. Knowing the types, and how the specification and MDN describe them, makes it much easier to read documentation, understand why a value is rejected, and predict what the browser computes. This article covers the basic types. Lengths, viewport units, math functions, colors, angles and times have their own articles in this part and the next.
Keywords
A keyword is a predefined word: none, auto, block, bold, center. Keywords aren't quoted, and they're case-insensitive: BLOCK works, though everyone writes lowercase.
Each property defines its own keywords, and the same word can mean different things in different properties:
normalinletter-spacingmeans no extra spacing, infont-weightit means 400, and inwhite-spacea specific wrapping behavior.autoinwidthmeans "fill the available space", while inmarginit can absorb free space (which centers a block).noneis a keyword, distinct from0.border: noneandborder: 0both remove a border, but for different reasons: one sets the style, the other the width.
Every property also accepts the global keywords inherit, initial, unset, revert, revert-layer and revert-rule (article 18).
Custom identifiers
Some values are names you choose: animation names, counter names, grid line and area names, container names, layer names. The specification calls these <custom-ident>:
@keyframes slide-in { ... }
.panel { animation-name: slide-in; container-name: sidebar; }
- They're case-sensitive:
slide-inandSlide-Inare different names. - They can't be a keyword the property already uses: you can't name an animation
noneorinitial, because the browser would read it as the keyword. - They can't start with a digit, or with a hyphen followed by a digit, unless escaped.
- Names that start with two hyphens are
<dashed-ident>s: custom properties such as--brand, anchor names such as--menu, and custom function names. They can never clash with a keyword.
Strings
A string is text in straight single or double quotes. Strings appear in content, font-family names, quotes, @import, grid template areas and attribute selectors:
.price::before { content: "€"; }
.quote::before { content: '“'; }
.note::before { content: "Don't miss this"; }
.esc::before { content: 'Don\'t miss this'; } /* escaped quote */
- The quotes must match, and only straight quotes delimit a string. Curly quotes are ordinary characters inside it.
- A backslash escapes the next character, as in
\'or\\. \followed by hex digits is a Unicode character:"\201C"is a left curly quote and"\A"is a line break. After a hex escape, a space ends the escape and is removed, so write"\A Second line"and that space disappears.- A line break inside a string must be escaped with
\at the end of the line, and the browser removes it, joining the lines. To display a real line break, use\Atogether withwhite-space: pre-lineorpre:
.stamp::before {
content: "\201C Signed by the author\201D \A Limited edition";
white-space: pre-line;
display: block;
font: italic 1rem Georgia, serif;
color: #8a3b12;
}
Font family names may be quoted strings or unquoted identifiers: "Times New Roman" and Times New Roman are both valid, but generic names such as serif must not be quoted, since "serif" would mean a font actually named "serif".
URLs
url() refers to a file: an image, font, stylesheet or SVG resource:
.hero { background-image: url("images/harbor.jpg"); }
@import url(base.css);
@font-face { font-family: Quay; src: url("/fonts/quay.woff2") format("woff2"); }
- Quotes are optional, but needed for URLs that contain spaces, parentheses or quotes. Quoting always is simplest.
- No space is allowed between
urland(:url (x.png)is invalid, and the whole declaration is dropped. - Relative URLs resolve against the stylesheet, not the HTML page. In
/css/site.css,url(images/logo.svg)means/css/images/logo.svg. In a<style>element orstyleattribute, they resolve against the page. - A
url()can't be built from a custom property:url(var(--file))doesn't work, because an unquoted URL is read as one literal token. You can store the wholeurl("...")in the variable instead:--hero: url("harbor.jpg"). The specification adds asrc()function that acceptsvar(), but no browser ships it yet. data:URLs embed small files directly:url("data:image/svg+xml,...").
Images
Properties such as background-image, border-image-source, list-style-image, mask-image and content accept an <image>, which is more than a URL:
| Image value | What it is | Support |
|---|---|---|
url(...) | an image file | everywhere |
gradients (linear-gradient(), radial-gradient(), conic-gradient() and repeating versions) | generated images | everywhere (article 50) |
image-set() | a choice of files by resolution or type, like srcset | all browsers since 2023 |
cross-fade() | blends images | -webkit- prefix in Chromium; unprefixed in Safari (old syntax only); not in Firefox |
element() | a live image of another element | Firefox only, prefixed |
paint() | an image drawn by a script worklet | Chromium only |
.hero {
background-image: image-set(
url("harbor.avif") type("image/avif"),
url("harbor.jpg") type("image/jpeg")
);
}
Integers and numbers
- An
<integer>is a whole number, optionally signed:3,-1,+10.z-index,order,grid-rowline numbers,column-countand counters take integers.z-index: 1.5is invalid. - A
<number>is any real number:0.5,.5,-3.25, and even scientific notation such as1e3.opacity,flex-grow,line-height(unitless) andscaletake numbers. - A dimension is a number with a unit and no space:
16px,1.5rem,300ms,45deg.10 pxis invalid. - Zero lengths can be written without a unit:
margin: 0. Other zero dimensions still need theirs in most places:transition-duration: 0is invalid, so write0s.
Percentages
A <percentage> is a number followed by %. It's always relative to something, and each property defines what:
| Property | Percentage of |
|---|---|
width, padding, margin (all sides!) | the containing block's width (inline size) |
height | the containing block's height, if it's definite |
font-size | the parent's font size |
line-height | the element's own font size |
transform: translate() | the element's own size |
background-position | the difference between the box and the image size |
The surprise in that table is padding and margin: even vertical padding percentages refer to the width. That's what the old "padding-top: 56.25%" trick for 16 boxes relied on, now replaced by aspect-ratio (article 38).

Halving the container's width halves the top padding too: the vertical percentage follows the width.
Flexible lengths: fr
The fr unit, as in 1fr, is a fraction of the free space in a grid container (article 67). It isn't a length, so it can't be mixed with lengths in calc(): calc(1fr - 10px) is invalid.
Ranges and invalid values
Properties restrict their values: padding can't be negative, opacity is meant to be between 0 and 1, flex-grow can't be negative. What happens outside the range depends on where the value comes from:
- A literal value outside the allowed range makes the declaration invalid at parse time, so it's dropped:
padding: -1pxis ignored, and the previous value stays. - Some properties clamp instead:
opacity: 2is accepted and computes to1. - A math function whose result falls outside the range is clamped:
padding: calc(10px - 20px)is valid and computes to0. In an integer context, the result is rounded:z-index: calc(1.6)computes to2.

The first and last panels show a dropped declaration. The middle two show a value that was accepted and then clamped.
Reading value syntax
The Formal syntax sections on MDN, and the specifications, describe values with a compact notation:
| Notation | Meaning | Example |
|---|---|---|
<length> | a value of that type | 16px |
auto | that exact keyword | auto |
a | b | exactly one of them | auto | <length> |
a || b | one or both, in any order | <line-style> || <color> |
a && b | both, in any order | |
[ ... ] | grouping | |
? | optional | <length>? |
*, + | zero or more, one or more | |
{2,4} | between 2 and 4 times | <length>{1,4} for margin |
# | one or more, comma-separated | <shadow># |
[0,∞] | an allowed range | <number [0,∞]> |
So border: <line-width> || <line-style> || <color> says that each part is optional and the order is free, and margin: [ <length-percentage> | auto ]{1,4} says one to four values, each a length, a percentage or auto.
From declared to used values
A value goes through several stages between your stylesheet and the screen:

- Declared values: every declaration that applies to the element.
- Cascaded value: the winner of the cascade (article 17).
- Specified value: the cascaded value, or, if there is none, the inherited or initial value (article 16).
- Computed value: resolved as far as possible without layout:
emandrembecome pixels, keywords likebolderbecome numbers, relative URLs become absolute. Percentages of the container's width stay percentages. This is what children inherit. - Used value: after layout, everything is absolute:
width: 50%becomes200px. - Actual value: the used value adjusted to what the device can do, for example rounded to whole device pixels.
getComputedStyle() returns the resolved value, which is the used value for a few layout properties such as width, height, margin and top, and the computed value for the rest. On an element with display: none, there's no layout, so width: 50% is reported as 50%.
What's current
image-set()without a prefix is in all browsers since 2023.- Typed range checks in math functions,
round()and friends are covered in article 26. attr()with types, which reads attribute values as numbers, lengths or colors, works in Chrome (since 133) and Firefox (since 155), with Safari 27.3 in beta (article 30).
Common questions
Why is transition: opacity 0 invalid when margin: 0 is fine?
Only lengths may drop the unit when the value is zero. Times, angles and other dimensions need their unit, even for zero: 0s, 0deg, apart from a few legacy angle contexts such as transform: rotate(0) (article 29).
Why is my relative url() broken when I move CSS into a separate file?
Relative URLs resolve against the file that contains them. Moved from a <style> element in the page to /css/site.css, the same url(images/x.png) now points into /css/images/. Use root-relative paths such as /images/x.png if the CSS may move.
Why does padding-top: 50% depend on the width?
Percentages for all margins and paddings refer to the containing block's inline size, so that they're the same on all four sides and don't depend on content height.
Can I name an animation none?
No. none is a keyword of animation-name, so it means "no animation". Custom identifiers can't use the property's keywords or the global keywords.
Summary
- Keywords are unquoted, case-insensitive and defined per property. Custom identifiers are your own names, case-sensitive, and can't clash with keywords.
- Strings use straight quotes, backslash escapes and hex escapes such as
\Afor a line break. url()paths resolve against the stylesheet. No space before the parenthesis, and novar()inside an unquoted URL.<image>covers URLs, gradients andimage-set().- Integers, numbers, dimensions (number plus unit, no space) and percentages each have their own rules. Only zero lengths can drop the unit.
- Out-of-range literals are invalid, while math results are clamped.
- Values go from declared, cascaded and specified to computed (inherited by children), used (after layout) and actual.
Practice questions
Which of these declarations are invalid: z-index: 2.5, opacity: 0.5, padding: -4px, margin: -4px, width: 10 px, transition-duration: 0?
Answer
z-index: 2.5 (needs an integer), padding: -4px (padding can't be negative), width: 10 px (no space allowed between number and unit) and transition-duration: 0 (times need a unit). Negative margins are allowed.
A stylesheet at /assets/css/main.css contains background: url(../img/wave.svg). Which file does the browser load for the page /shop/books/index.html?
Answer
/assets/img/wave.svg. Relative URLs in a stylesheet resolve against the stylesheet's location, not the page's.
What does this syntax allow: box-shadow: none | <shadow>#?
Answer
Either the keyword none, or one or more shadows separated by commas.
An element has width: 50% in a 600px-wide container and font-size: 1.5em with a 16px parent. What are the computed and used values of each?
Answer
font-size: computed 24px, used 24px. width: computed 50% (percentages of the container stay unresolved until layout), used 300px.
Previous: @scope: Limiting Where Styles Apply, Scope Limits and Proximity
Next: Length Units: px, em, rem, ch, lh and the Other Font-Relative Units

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.