Skip to main content

CSS values and units

Every CSS declaration includes a property / value pair. Depending on the property, the value can include a single integer or keyword, to a series of keywords and values with or without units. There are a common set of data types — values and units — that CSS properties accept. Below is an overview of most of these data types. Refer to the page for each value type for more detailed information.

Textual data types

  • {{cssxref("<custom-ident>")}}
  • Pre-defined keywords as an <ident>
  • {{cssxref("<string>")}}
  • {{cssxref("url_value", "<url>")}}

Text data types are either <string>, a quoted series of characters, or an <ident>, a "CSS Identifier" which is an unquoted string. A <string> must be quoted with either single or double quotes. CSS Identifiers, listed in the specifications as <ident> or <custom-ident>, must be unquoted.

In the CSS specifications, values that can be defined by the web developer, like keyframe animations, font-family names, or grid areas are listed as a {{cssxref("<custom-ident>")}}, {{cssxref("<string>")}}, or both.

When both quoted and unquoted user defined text values are permitted, the specification will list <custom-ident> | <string>, meaning quotes are optional, such as is the case with animation names:

@keyframe validIdent {
/* keyframes go here */
}
@keyframe 'validString' {
/* keyframes go here */
}

Some text values are not valid if encompassed in quotes. For example, the value of {{cssxref("grid-area")}} can be a <custom-ident>, so if we had a grid area named content we would use it without quotes:

.item {
grid-area: content;
}

In comparison, a data type that is a {{cssxref("<string>")}}, such as a string value of the {{cssxref("content")}} property, must be quoted:

.item::after {
content: "This is my content.";
}

While you can generally create any name you want, including using emojis, the identifier can't be none, unset, initial, or inherit, start with a digit or two dashes, and generally you don't want it to be any other pre-defined CSS keyword. See the {{cssxref("<custom-ident>")}} and {{cssxref("<string>")}} reference pages for more details.

Pre-defined keyword values

Pre-defined keywords are text values defined by the specification for that property. These keywords are also CSS Identifiers and are therefore used without quotes.

When viewing CSS property value syntax in a CSS specification or the MDN property page, allowable keywords will be listed in the following form. The following values are the pre-defined keyword values allowed for {{cssxref("float")}}.

left | right | none | inline-start | inline-end

Such values are used without quotes:

.box {
float: left;
}

CSS-wide values

In addition to the pre-defined keywords that are part of the specification for a property, all CSS properties accept the CSS-wide property values {{cssxref("initial")}}, {{cssxref("inherit")}}, {{cssxref("unset")}}, {{cssxref("revert")}}, and {{cssxref("revert-layer")}}, which explicitly specify defaulting behaviors.

  • {{cssxref("initial")}}
    • : Represents the value specified as the property's initial value.
  • {{cssxref("inherit")}}
    • : Represents the computed value of the property on the element's parent, provided it is inherited.
  • {{cssxref("unset")}}
    • : Acts as either inherit or initial, depending on whether the property is inherited or not.
  • {{cssxref("revert")}}
    • : Resets the property to its inherited value if it inherits from its parent or to the default value established by the user agent's stylesheet (or by user styles, if any exist).
  • {{cssxref("revert-layer")}}
    • : Rolls back the value of a property in a cascade layer to the value of the property in a CSS rule matching the element in a previous cascade layer. The value of the property with this keyword is recalculated as if no rules were specified on the target element in the current cascade layer.

URLs

A {{cssxref("url_value", "<url>")}} type uses functional notation, which accepts a <string> that is a URL. This may be an absolute URL or a relative URL. For example, if you wanted to include a background image, you might use either of the following.

.box {
background-image: url("images/my-background.png");
}

.box {
background-image: url("https://www.exammple.com/images/my-background.png");
}

The parameter for url() can be either quoted or unquoted. If unquoted, it is parsed as a <url-token>, which has extra requirements including the escaping of certain characters. See {{cssxref("url_value", "<url>")}} for more information.

Numeric data types

  • {{cssxref("<integer>")}}
  • {{cssxref("<number>")}}
  • {{cssxref("<dimension>")}}
  • {{cssxref("<percentage>")}}

Integers

An integer is one or more decimal digits, 0 through 9, such as 1024 or -55. An integer may be preceded by a + or - symbol, with no space between the symbol and the integer.

Numbers

A {{cssxref("<number>")}} represents a real number, which may or may not have a decimal point with a fractional component, for example 0.255, 128 or -1.2. Numbers may also be preceded by a + or - symbol.

Dimensions

A {{cssxref("<dimension>")}} is a <number> with a unit attached to it, for example 45deg, 100ms, or 10px. The attached unit identifier is case insensitive. There is never a space or any other characters between the number and the unit identifier: i.e. 1 cm is not valid.

CSS uses dimensions to specify:

  • {{cssxref("<length>")}} (Distance units)
  • {{cssxref("<angle>")}}
  • {{cssxref("<time>")}}
  • {{cssxref("<frequency>")}}
  • {{cssxref("<flex>")}}
  • {{cssxref("<resolution>")}}

These are all covered in subsections below.

Distance units

Where a distance unit, also known as a length, is allowed as a value for a property, this is described as the {{cssxref("<length>")}} type. There are two types of lengths in CSS: relative and absolute. Relative length units specify a length in relation to something else.

There are two types of relative lengths: font-relative lengths and viewport-percentage lengths. These both come in two types. Font-relative length units are either local font-relative or root font-relative. Viewport percentage lengths are either relative to the viewport height or width size or, as defined in the CSS Containment module, relative to a container.

Local font-relative lengths

Local font-relative lengths are relative to the "local" font size or line height, specifying a length in relation to a computed size of a feature of the element itself, or relative to the element's inherited value in the case of a circular reference, such as the em value for a {{cssxref("font-size")}} property or a lh value for a {{cssxref("line-height")}} property. For example, em is relative to the font size on the element and ex is relative to the x-height of the element's font.

UnitRelative to
capCap height (the nominal height of capital letters) of the element's font.
chAverage character advance of a narrow glyph in the element's font, as represented by the "0" (ZERO, U+0030) glyph.
emFont size of the element's font.
exx-height of the element's font.
icAverage character advance of a full-width glyph in the element's font, as represented by the "水" (CJK water ideograph, U+6C34) glyph.
lhLine height of the element.
Root font-relative lengths

Root font-relative lengths specify a length in relation to the element's root element ancestor, such as {{HTMLElement("HTML")}} or {{SVGElement("SVG")}}. For example, rem is relative to the font size on the root element and rex is the x-height of the root element's font.

UnitRelative to
rcapCap height (the nominal height of capital letters) of the root element's font.
rchAverage character advance of a narrow glyph in the root element's font, as represented by the "0" (ZERO, U+0030) glyph.
remFont size of the root element's font.
rexx-height of the root element's font.
ricAverage character advance of a full-width glyph in the root element's font, as represented by the "水" (CJK water ideograph, U+6C34) glyph.
rlhLine height of the root element.
Viewport units

Viewport unit lengths specify a length relative to the dimensions of the viewport. For example, vw is relative to the width of the viewport and vh is relative to the height of the viewport.

UnitRelative to
dvh1% of the dynamic viewport's height.
dvw1% of the dynamic viewport's width.
lvh1% of the large viewport's height.
lvw1% of the large viewport's width.
svh1% of the small viewport's height.
svw1% of the small viewport's width.
vb1% of viewport's size in the root element's block axis.
vh1% of viewport's height.
vi1% of viewport's size in the root element's inline axis.
vmax1% of viewport's larger dimension.
vmin1% of viewport's smaller dimension.
vw1% of viewport's width.
Container units

Container query length units specify a length relative to the dimensions of a query container. For example, cqw is relative to the width of the query container and cqh is relative to the height of the query container.

UnitRelative to
cqb1% of a query container's block size
cqh1% of a query container's height
cqi1% of a query container's inline size
cqmaxThe larger value of cqi or cqb
cqminThe smaller value of cqi or cqb
cqw1% of a query container's width

Absolute length units

Absolute length units are fixed to a physical length: either an inch or a centimeter. Many of these units are therefore more useful when the output is a fixed size media, such as print. For example, mm is a physical millimeter, 1/10th of a centimeter.

UnitNameEquivalent to
cmCentimeters1cm = 96px/2.54
inInches1in = 2.54cm = 96px
mmMillimeters1mm = 1/10th of 1cm
pcPicas1pc = 1/6th of 1in
ptPoints1pt = 1/72th of 1in
pxPixels1px = 1/96th of 1in
QQuarter-millimeters1Q = 1/40th of 1cm

When including a length value, if the length is 0, the unit identifier is not required. Otherwise, the unit identifier is required, is case insensitive, and must come immediately after the numeric part of the value, with no space in-between.

Angle units

Angle values are represented by the type {{cssxref("<angle>")}} and accept the following values:

UnitNameDescription
degDegreesThere are 360 degrees in a full circle.
gradGradiansThere are 400 gradians in a full circle.
radRadiansThere are 2π radians in a full circle.
turnTurnsThere is 1 turn in a full circle.
Time units

Time values are represented by the type {{cssxref("<time>")}}. When including a time value, the unit identifier — the s or ms — is required. It accepts the following values.

UnitNameDescription
msMillisecondsThere are 1,000 milliseconds in a second.
sSeconds
Frequency units

Frequency values are represented by the type {{cssxref("<frequency>")}}. It accepts the following values.

UnitNameDescription
HzHertzRepresents the number of occurrences per second.
kHzKiloHertzA kiloHertz is 1000 Hertz.

1Hz, which can also be written as 1hz or 1HZ, is one cycle per second.

Flex units

Flex units are represented by the type {{cssxref("<flex>")}}. It accepts the following value.

UnitNameDescription
frFlexRepresents a flexible length within a grid container
Resolution units

Resolution units are represented by the type {{cssxref("<resolution>")}}. They represent the size of a single dot in a graphical representation, such as a screen, by indicating how many of these dots fit in a CSS inch, centimeter, or pixel. It accepts the following values:

UnitDescription
dpcmDots per centimeter.
dpiDots per inch.
dppx, xDots per px unit.

Percentages

A {{cssxref("<percentage>")}} is a type that represents a fraction of some other value.

Percentage values are always relative to another quantity, for example a length. Each property that allows percentages also defines the quantity to which the percentage refers. This quantity can be a value of another property of the same element, the value of a property of an ancestor element, a measurement of a containing block, or something else.

As an example, if you specify the {{cssxref("width")}} of a box as a percentage, it refers to the percentage of the box's parent's computed width:

.box {
width: 50%;
}

Mixing percentages and dimensions

Some properties accept a dimension that could be either one of two types, for example a <length> or a <percentage>. In this case the allowed value is detailed in the specification as a combination unit, e.g. {{cssxref("<length-percentage>")}}. Other possible combinations are as follows:

  • {{cssxref("<frequency-percentage>")}}
  • {{cssxref("<angle-percentage>")}}
  • {{cssxref("<time-percentage>")}}

Special data types (defined in other specs)

  • {{cssxref("<image>")}}
  • {{cssxref("<position>")}}

Color

The value specifies the color of an element feature (e.g. it's background color), and is defined in the CSS Color Module.

Image

The {{cssxref("<image>")}} value specifies all the different types of image that can be used in CSS, and is defined in the CSS Image Values and Replaced Content Module.

Position

The {{cssxref("<position>")}} type defines 2D positioning of an object inside a positioning area, for example a background image inside a container. This type is interpreted as a {{cssxref("background-position")}} and therefore specified in the CSS Backgrounds and Borders specification.

Functional notation

  • {{cssxref("calc", "calc()")}}
  • {{cssxref("min", "min()")}}
  • {{cssxref("max", "max()")}}
  • {{cssxref("minmax", "minmax()")}}
  • {{cssxref("clamp", "clamp()")}}
  • {{cssxref("toggle", "toggle()")}}
  • {{cssxref("attr", "attr()")}}

Functional notation is a type of value that can represent more complex types or invoke special processing by CSS. The syntax starts with the name of the function immediately followed by a left parenthesis ( followed by the argument(s) to the notation followed by a right parenthesis ). Functions can take multiple arguments, which are formatted similarly to a CSS property value.

White space is allowed, but optional inside the parentheses. (But see notes regarding whitespace within pages for min(), max(), minmax(), and clamp() functions.)

Some legacy functional notations, such as legacy syntax for rgb(), rgba(), hsl(), and hsla(), used commas, but commas are generally only used to separate items in a list. If a comma is used to separate arguments, white space is optional before and after the comma.

Specifications

See also