The viewport-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
-
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 the sv*
viewport-percentage length units. The sizes of the small viewport-percentage units are fixed, and therefore stable, unless the viewport itself is resized.
- Large
-
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 though that the content might get hidden when browser interfaces expand.
For example, on mobile phones where the 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 the large viewport-percentage units.
The large viewport unit is represented by the lv
prefix and results in the lv*
viewport-percentage units. The sizes of the large viewport-percentage units are fixed, and therefore stable, unless the viewport itself is resized.
- Dynamic
-
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 the dv*
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
-
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.
Viewport-percentage lengths define <length>
values in percentage relative to the size of the initial containing block, which in turn is based on either the size of the viewport 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 initial containing block. 1vh
is 1% of the viewport height. For example, if the viewport height is 300px
, then a value of 70vh
on a property will be 210px
.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are svh
, lvh
, and dvh
. vh
represents the viewport-percentage length unit based on the browser default viewport size.
vw
-
Represents a percentage of the width of the viewport's initial containing block. 1vw
is 1% of the viewport width. For example, if the viewport width is 800px
, then a value of 50vw
on a property will be 400px
.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are svw
, lvw
, and dvw
. vw
represents the viewport-percentage length unit based on the browser default viewport size.
vmax
-
Represents in percentage the larger of vw
and vh
.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are svmax
, lvmax
, and dvmax
. vmax
represents the viewport-percentage length unit based on the browser default viewport size.
vmin
-
Represents in percentage the smaller of vw
and vh
.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are svmin
, lvmin
, and dvmin
. vmin
represents the viewport-percentage length unit based on the browser default viewport size.
vb
-
Represents percentage of the size of the initial containing block, in the direction of the root element's block axis.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are svb
, lvb
, and dvb
, respectively. vb
represents the viewport-percentage length unit based on the browser default viewport size.
vi
-
Represents a percentage of the size of the initial containing block, in the direction of the root element's inline axis.
For small, large, and dynamic viewport sizes, the respective viewport-percentage units are svi
, lvi
, and dvi
. vi
represents the viewport-percentage length unit based on the browser default viewport size.
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 a physical unit and then defining the others relative to it. The anchoring is done differently for low-resolution devices, such as screens, versus high-resolution devices, such as printers.
For low-dpi devices, the unit px
represents the physical reference pixel; other units are defined relative to it. Thus, 1in
is defined as 96px
, which equals 72pt
. 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, the px
unit is defined relative to them (1/96 of 1in
).
Note: Many users increase their user 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 as em
or rem
) when setting font-size
.
px
-
One pixel. For screen displays, it traditionally represents one device pixel (dot). However, for printers and high-resolution screens, one CSS pixel implies multiple device pixels. 1px
= 1/96th of 1in
.
cm
-
One centimeter. 1cm
= 96px/2.54
.
mm
-
One millimeter. 1mm
= 1/10th of 1cm
.
Q
-
One quarter of a millimeter. 1Q
= 1/40th of 1cm
.
in
-
One inch. 1in
= 2.54cm
= 96px
.
pc
-
One pica. 1pc
= 12pt
= 1/6th of 1in
.
pt
-
One point. 1pt
= 1/72nd of 1in
.