<length>
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The<length>
CSSdata type represents a distance value. Lengths can be used in numerous CSS properties, such aswidth
,height
,margin
,padding
,border-width
,font-size
, andtext-shadow
.
Note:Although<percentage>
values are usable in some of the same properties that accept<length>
values, they are not themselves<length>
values. See<length-percentage>
.
In this article
Syntax
The<length>
data type consists of a<number>
followed by one of the units listed below. As with all CSS dimensions, there is no space between the number and the unit literal. Specifying the length unit is optional if the number is0
.
Note:Some properties allow negative<length>
values, while others do not.
Thespecified value of a length (specified length) is represented by its quantity and unit. Thecomputed value of a length (computed length) is the specified length resolved to an absolute length, and its unit is not distinguished.
The<length>
units can be relative or absolute. Relative lengths represent a measurement in terms of some other distance. Depending on the unit, this distance can be the size of a specific character, theline height, or the size of theviewport. Style sheets that use relative length units can more easily scale from one output environment to another.
Note:Child elements do not inherit the relative values as specified for their parent; they inherit the computed values.
Relative length units
CSS relative length units are based on font, container, or viewport sizes.
Relative length units based on font
Font lengths define the<length>
value in terms of the size of a particular character or font attribute in the font currently in effect in an element or its parent.
Note:These units, especiallyem
and the root relativerem
, are often used to create scalable layouts, which maintain the vertical rhythm of the page even when the user changes the font size.
cap
Represents the "cap height" (nominal height of capital letters) of the element's
font
.ch
Represents the width or, more precisely, theadvance measure of the glyph
0
(zero, the Unicode character U+0030) in the element'sfont
.In cases where determining the measure of the0
glyph is impossible or impractical, it must be assumed to be0.5em
wide by1em
tall.em
Represents the calculated
font-size
of the element. If used on thefont-size
property itself, it represents theinherited font-size of the element.ex
Represents thex-height of the element's
font
. In fonts with thex
letter, this is generally the height of lowercase letters in the font;1ex ≈ 0.5em
in many fonts.ic
Equal to the usedadvance measure of the "水" glyph (CJK water ideograph, U+6C34), found in the font used to render it.
lh
Equal to the computed value of the
line-height
property of the element on which it is used, converted to an absolute length. This unit enables length calculations based on the theoretical size of an ideal empty line. However, the size of actual line boxes may differ based on their content.
Relative length units based on root element's font
Root element font relative length units define the<length>
value in terms of the size of a particular character or font attribute of theroot element:
rcap
Equal to the "cap height" (nominal height of capital letters) of the root element's
font
.rch
Equal to the width or theadvance measure of the glyph
0
(zero, the Unicode character U+0030) in the root element'sfont
.rem
Represents the
font-size
of the root element (typically<html>
). When used within the root elementfont-size
, it represents its initial value. A common browser default is16px
, but user-defined preferences may modify this.rex
Represents the x-height of the root element's
font
.ric
Equal to the value of
ic
unit on the root element's font.rlh
Equal to the value of
lh
unit on the root element's font. This unit enables length calculations based on the theoretical size of an ideal empty line. However, the size of actual line boxes may differ based on their content.
Relative length units based on viewport
Theviewport-percentage length units are based on four different viewport sizes: small, large, dynamic, and default. The allowance for the different viewport sizes is in response to browser interfaces expanding and retracting dynamically and hiding and showing the content underneath.
- Small viewport units
When you want the smallest possible viewport in response to browser interfaces expanding dynamically, you should use the small viewport size. The small viewport size allows the content you design to fill the entire viewport when browser interfaces are expanded. Choosing this size might also possibly leave empty spaces when browser interfaces retract.
For example, an element that is sized using viewport-percentage units based on the small viewport size, the element will fill the screen perfectly without any of its content being obscured when all the dynamic browser interfaces are shown. When those browser interfaces are hidden, however, there might be extra space visible around the element. Therefore, the small viewport-percentage units are "safer" to use in general, but might not produce the most attractive layout after a user starts interacting with the page.
The small viewport size is represented by the
sv
prefix and results in thesv*
viewport-percentage length units. The sizes of the small viewport-percentage units are fixed, and therefore stable, unless the viewport itself is resized.- Large viewport units
When you want the largest possible viewport in response to browser interfaces retracting dynamically, you should use the large viewport size. The large viewport size allows the content you design to fill the entire viewport when browser interfaces are retracting. You need to be aware that the content might get hidden when browser interfaces expand.
For example, on mobile phones where screen real-estate is at a premium, browsers often hide part or all of the title and address bar after a user starts scrolling the page. When an element is sized using a viewport-percentage unit based on the large viewport size, the content of the element will fill the entire visible page when these browser interfaces are hidden. However, when these retractable browser interfaces are shown, they can hide the content that is sized or positioned using thelarge viewport-percentage units.
The large viewport unit is represented by the
lv
prefix and results in thelv*
viewport-percentage units. The sizes of the large viewport-percentage units are fixed and therefore stable, unless the viewport itself is resized.- Dynamic viewport units
When you want the viewport to be automatically sized in response to browser interfaces dynamically expanding or retracting, you can use the dynamic viewport size. The dynamic viewport size allows the content you design to fit exactly within the viewport, irrespective of the presence of dynamic browser interfaces.
The dynamic viewport unit is represented by the
dv
prefix and results in thedv*
viewport-percentage units. The sizes of the dynamic viewport-percentage units are not stable, even when the viewport itself is unchanged.Note:While the dynamic viewport size can give you more control and flexibility, using viewport-percentage units based on the dynamic viewport size can cause the content to resize while a user is scrolling a page. This can lead to degradation of the user interface and cause a performance hit.
- Default viewport units
The default viewport size is defined by the browser. The behavior of the resulting viewport-percentage unit could be equivalent to the viewport-percentage unit based on the small viewport size, the large viewport size, an intermediate size between the two, or the dynamic viewport size.
Note:For example, a browser might implement the default viewport-percentage unit for height (
vh
) that is equivalent to the large viewport-percentage height unit (lvh
). If so, this could obscure content on a full-page display while the browser interface is expanded. Currently, all default viewport units (vh
,vw
, etc.) are equivalent to their large viewport counterparts (lvh
,lvw
, etc.).
Viewport-percentage lengths define<length>
values in percentage relative to the size of the initialcontaining block, which in turn is based on either the size of theviewport or the page area, i.e., the visible portion of the document. When the height or width of the initial containing block is changed, the elements that are sized based on them are scaled accordingly. There is a viewport-percentage length unit variant corresponding to each of the viewport sizes, as described below.
Note:Viewport lengths are invalid in@page
declaration blocks.
vh
Represents a percentage of the height of the viewport's initialcontaining block.
1vh
is 1% of the viewport height. For example, if the viewport height is300px
, then a value of70vh
on a property will be210px
.The respective viewport-percentage units for small, large, and dynamic viewport sizes are
svh
,lvh
, anddvh
.vh
is equivalent tolvh
, representing the viewport-percentage length unit based on the large viewport size.vw
Represents a percentage of the width of the viewport's initialcontaining block.
1vw
is 1% of the viewport width. For example, if the viewport width is800px
, then a value of50vw
on a property will be400px
.For small, large, and dynamic viewport sizes, the respective viewport-percentage units are
svw
,lvw
, anddvw
.vw
is equivalent tolvw
, representing the viewport-percentage length unit based on the large viewport size.vmax
Represents in percentage the largest of
vw
andvh
.For small, large, and dynamic viewport sizes, the respective viewport-percentage units are
svmax
,lvmax
, anddvmax
.vmax
is equivalent tolvmax
, representing the viewport-percentage length unit based on the large viewport size.vmin
Represents in percentage the smallest of
vw
andvh
.For small, large, and dynamic viewport sizes, the respective viewport-percentage units are
svmin
,lvmin
, anddvmin
.vmin
is equivalent tolvmin
, representing the viewport-percentage length unit based on the large viewport size.vb
Represents the percentage of the size of the initialcontaining block, in the direction of the root element'sblock axis.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are
svb
,lvb
, anddvb
, respectively.vb
is equivalent tolvb
, representing the viewport-percentage length unit based on the large viewport size.vi
Represents a percentage of the size of the initialcontaining block, in the direction of the root element'sinline axis.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are
svi
,lvi
, anddvi
.vi
is equivalent tolvi
, representing the viewport-percentage length unit based on the large viewport size.
Container query length units
When applying styles to a container using container queries, you can use container query length units.These units specify a length relative to the dimensions of a query container.Components that use units of length relative to their container are more flexible to use in different containers without having to recalculate concrete length values.
If no eligible container is available for the query, the container query length unit defaults to thesmall viewport unit for that axis (sv*
).
For more information, seeContainer queries.
cqw
Represents a percentage of the width of the query container.
1cqw
is 1% of the query container's width. For example, if the query container's width is800px
, then a value of50cqw
on a property will be400px
.cqh
Represents a percentage of the height of the query container.
1cqh
is 1% of the query container's height. For example, if the query container's height is300px
, then a value of10cqh
on a property will be30px
.cqi
Represents a percentage of the inline size of the query container.
1cqi
is 1% of the query container's inline size. For example, if the query container's inline size is800px
, then a value of50cqi
on a property will be400px
.cqb
Represents a percentage of the block size of the query container.
1cqb
is 1% of the query container's block size. For example, if the query container's block size is300px
, then a value of10cqb
on a property will be30px
.cqmin
Represents a percentage of the smaller value of either the query container's inline size or block size.
1cqmin
is 1% of the smaller value of either the query container's inline size or block size. For example, if the query container's inline size is800px
and its block size is300px
, then a value of50cqmin
on a property will be150px
.cqmax
Represents a percentage of the larger value of either the query container's inline size or block size.
1cqmax
is 1% of the larger value of either the query container's inline size or block size. For example, if the query container's inline size is800px
and its block size is300px
, then a value of50cqmax
on a property will be400px
.
Absolute length units
Absolute length units represent a physical measurement when the physical properties of the output medium are known, such as for print layout. This is done by anchoring one of the units to aphysical unit or thevisual angle unit and then defining the others relative to it. Physical units includecm
,in
,mm
,pc
,pt
,px
, andQ
.The anchoring is done differently for low-resolution devices, such as screens, versus high-resolution devices, such as printers.
For low-dpi devices, the unitpx
represents the physicalreference pixel; other units are defined relative to it. Thus,1in
is defined as96px
, which equals72pt
. The consequence of this definition is that on such devices, dimensions described in inches (in
), centimeters (cm
), or millimeters (mm
) don't necessarily match the size of the physical unit with the same name.
For high-dpi devices, inches (in
), centimeters (cm
), and millimeters (mm
) are the same as their physical counterparts. Therefore, thepx
unit is defined relative to them (1/96 of1in
).
Note:Many users increase theiruser agent's default font size to make text more legible. Absolute lengths can cause accessibility problems because they are fixed and do not scale according to user settings. For this reason, prefer relative lengths (such asem
orrem
) when settingfont-size
.
px
One pixel. For screen displays, it traditionally represents onedevice pixel (dot). However, forprinters andhigh-resolution screens, one CSS pixel implies multiple device pixels.
1px
=1in / 96
.cm
One centimeter.
1cm
=96px / 2.54
.mm
One millimeter.
1mm
=1cm / 10
.Q
One quarter of a millimeter.
1Q
=1cm / 40
.in
One inch.
1in
=2.54cm
=96px
.pc
One pica.
1pc
=12pt
=1in / 6
.pt
One point.
1pt
=1in / 72
.
Interpolation
When animated, values of the<length>
data type are interpolated as real, floating-point numbers. Theinterpolation happens on the calculated value. The speed of the interpolation is determined by theeasing function associated with the animation.
Examples
>Comparing different length units
The following example provides you with an input field in which you can enter a<length>
value (e.g.,300px
,50%
,30vw
) to set the width of a result bar that will appear below it once you've pressed theEnter or theReturn key.
This allows you to compare and contrast the effects of different length units.
HTML
<div> <div> <label for="length">Enter width:</label> <input type="text" /> </div> <div></div></div><div></div>
CSS
html { font-family: sans-serif; font-weight: bold; box-sizing: border-box;}.outer { width: 100%; height: 50px; background-color: #eeeeee; position: relative;}.inner { height: 50px; background-color: #999999; box-shadow: inset 3px 3px 5px rgb(255 255 255 / 50%), inset -3px -3px 5px rgb(0 0 0 / 50%);}.result { height: 20px; box-shadow: inset 3px 3px 5px rgb(255 255 255 / 50%), inset -3px -3px 5px rgb(0 0 0 / 50%); background-color: orange; display: flex; align-items: center; margin-top: 10px;}.result code { position: absolute; margin-left: 20px;}.results { margin-top: 10px;}.input-container { position: absolute; display: flex; justify-content: flex-start; align-items: center; height: 50px;}label { margin: 0 10px 0 20px;}
JavaScript
const inputDiv = document.querySelector(".inner");const inputElem = document.querySelector("input");const resultsDiv = document.querySelector(".results");inputElem.addEventListener("change", () => { inputDiv.style.width = inputElem.value; const result = document.createElement("div"); result.className = "result"; result.style.width = inputElem.value; const code = document.createElement("code"); code.textContent = `width: ${inputElem.value}`; result.appendChild(code); resultsDiv.appendChild(result); inputElem.value = ""; inputElem.focus();});
Result
Specifications
Specification |
---|
CSS Values and Units Module Level 4> # lengths> |
Browser compatibility
Loading…