<length>
CSS
data type
represents a distance value. Lengths can be used in numerous CSS properties, such as
width
,
height
,
margin
,
padding
,
border-width
,
font-size
,和
text-shadow
.
注意:
Although
<percentage>
values are also CSS dimensions, and are usable in some of the same properties that accept
<length>
values, they are not themselves
<length>
values. See
<length-percentage>
.
<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 unit literal and the number. The length unit is optional after the number
0
.
注意:
Some properties allow negative
<length>
s, while others do not.
Relative lengths represent a measurement in terms of some other distance. Depending on the unit, this can be the size of a specific character, the line height , or the size of the viewport .
Font-relative 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.
注意:
These units, especially
em
and
rem
, are often used to create scalable layouts, which maintain the
vertical rhythm of the page
even when the user changes the font size.
cap
font
.
ch
font
.
em
font-size
of the element. If used on the
font-size
property itself, it represents the
inherited
font-size of the element.
ex
font
. On fonts with the "x" letter, this is generally the height of lowercase letters in the font;
1ex ≈ 0.5em
in many fonts.
ic
Equal to the used advance measure of the "水" glyph (CJK water ideograph, U+6C34), found in the font used to render it.
lh
line-height
property of the element on which it is used, converted to an absolute length.
rem
font-size
of the root element (typically
<html>
). When used within the root element
font-size
, it represents its initial value (a common browser default is
16px
, but user-defined preferences may modify this).
rlh
line-height
property on the root element (typically
<html>
), converted to an absolute length. When used on the
font-size
or
line-height
properties of the root element, it refers to the properties' initial value.
Viewport-percentage lengths define the
<length>
value relative to the size of the
viewport
, i.e., the visible portion of the document. Viewport lengths are invalid in
@page
declaration blocks.
vh
vw
vi
vb
vmin
vw
and
vh
.
vmax
vw
and
vh
.
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 anchor 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 necessary 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 1 inch).
注意:
Many users increase their
用户代理
's default font size to make text more legible. Absolute lengths can cause accessibility problems, since 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
1px
= 1/96th of
1in
.
cm
1cm
=
96px/2.54
.
mm
1mm
= 1/10th of
1cm
.
Q
1Q
= 1/40th of
1cm
.
in
1in
=
2.54cm
=
96px
.
pc
1pc
=
12pt
= 1/6th of
1in
.
pt
1pt
= 1/72nd of
1in
.
mozmm
, removed in Firefox 59
An experimental unit that attempts to render at exactly one millimeter regardless of the size or resolution of the display. This is rarely actually what you want, but may be useful in particular for mobile devices.
When animated, values of the
<length>
data type are interpolated as real, floating-point numbers. The interpolation happens on the calculated value. The speed of the interpolation is determined by the
timing function
associated with the animation.
The following demo 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
返回
.
This allows you to compare and contrast the effect of different length units.
<div class="outer">
<div class="input-container">
<label>Enter width:</label>
<input type="text" id="length">
</div>
<div class="inner">
</div>
</div>
<div class="results">
</div>
html {
font-family: sans-serif;
font-weight: bold;
box-sizing: border-box;
}
.outer {
width: 100%;
height: 50px;
background-color: #eee;
position: relative;
}
.inner {
height: 50px;
background-color: #999;
box-shadow: inset 3px 3px 5px rgba(255,255,255,0.5),
inset -3px -3px 5px rgba(0,0,0,0.5);
}
.result {
height: 20px;
background-color: #999;
box-shadow: inset 3px 3px 5px rgba(255,255,255,0.5),
inset -3px -3px 5px rgba(0,0,0,0.5);
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;
}
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;
result.innerHTML = `<code>width: ${inputElem.value}</code>`;
resultsDiv.appendChild(result);
inputElem.value = '';
inputElem.focus();
})
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Values and Units Module Level 4
The definition of '<length>' in that specification. |
编者草案 |
添加
vi
,
vb
,
ic
,
lh
,和
rlh
units.
|
|
CSS Values and Units Module Level 3
The definition of '<length>' in that specification. |
候选推荐 |
添加
ch
,
rem
,
vw
,
vh
,
vmin
,
vmax
,和
Q
units.
|
|
CSS Level 2 (Revision 1)
The definition of '<length>' in that specification. |
推荐 |
Explicit definition of the
em
,
pt
,
pc
,和
px
units.
|
|
CSS Level 1
The definition of '<length>' in that specification. |
推荐 |
Initial definition. Implicit definition of the
em
,
pt
,
pc
,和
px
units.
|
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
<length>
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 3 | Opera 完整支持 3.5 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
Q
unit
|
Chrome 完整支持 63 | Edge 完整支持 79 | Firefox 完整支持 49 | IE 不支持 No | Opera 完整支持 50 | Safari 不支持 No | WebView Android 完整支持 63 | Chrome Android 完整支持 63 | Firefox Android 完整支持 49 | Opera Android 完整支持 46 | Safari iOS 不支持 No | Samsung Internet Android 完整支持 8.0 |
cap
unit
|
Chrome 不支持 No | Edge 不支持 No | Firefox 不支持 No | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
ch
unit
|
Chrome 完整支持 27 | Edge 完整支持 12 |
Firefox
完整支持
1
注意事项
|
IE 完整支持 9 | Opera 完整支持 20 | Safari 完整支持 7 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 27 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 7 | Samsung Internet Android 完整支持 1.5 |
ex
unit
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 3.5 | Safari 完整支持 1 | WebView Android 完整支持 1 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
ic
unit
|
Chrome 不支持 No | Edge 不支持 No | Firefox 不支持 No | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
lh
unit
|
Chrome 不支持 No | Edge 不支持 No | Firefox 不支持 No | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
mozmm
unit
非标
|
Chrome 不支持 No | Edge 不支持 No | Firefox 不支持 4 — 59 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 4 — 59 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
rem
unit
|
Chrome 完整支持 4 | Edge 完整支持 12 | Firefox 完整支持 3.6 | IE 完整支持 9 | Opera 完整支持 11.6 | Safari 完整支持 5 | WebView Android 完整支持 2 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 12 | Safari iOS 完整支持 4 | Samsung Internet Android 完整支持 1.0 |
rlh
unit
|
Chrome 不支持 No | Edge 不支持 No | Firefox 不支持 No | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
vb
unit
|
Chrome 不支持 No | Edge 不支持 No | Firefox 不支持 No | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
vh
unit
|
Chrome 完整支持 20 | Edge 完整支持 12 |
Firefox
完整支持
19
注意事项
|
IE 完整支持 9 | Opera 完整支持 20 | Safari 完整支持 6 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 25 |
Firefox Android
完整支持
19
注意事项
|
Opera Android 完整支持 14 | Safari iOS 完整支持 6 | Samsung Internet Android 完整支持 1.5 |
vi
unit
|
Chrome 不支持 No | Edge 不支持 No | Firefox 不支持 No | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
vmax
unit
|
Chrome 完整支持 26 | Edge 完整支持 16 |
Firefox
完整支持
19
注意事项
|
IE 不支持 No | Opera 完整支持 15 | Safari 完整支持 6.1 | WebView Android 完整支持 1.5 | Chrome Android 完整支持 26 |
Firefox Android
完整支持
19
注意事项
|
Opera Android 完整支持 14 | Safari iOS 完整支持 6.1 | Samsung Internet Android 完整支持 1.5 |
vmin
unit
|
Chrome 完整支持 26 |
Edge
完整支持
12
|
Firefox
完整支持
19
注意事项
|
IE
完整支持
10
|
Opera 完整支持 15 | Safari 完整支持 6.1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 26 |
Firefox Android
完整支持
19
注意事项
|
Opera Android 完整支持 14 | Safari iOS 完整支持 6.1 | Samsung Internet Android 完整支持 1.5 |
vw
unit
|
Chrome 完整支持 20 | Edge 完整支持 12 |
Firefox
完整支持
19
注意事项
|
IE 完整支持 9 | Opera 完整支持 20 | Safari 完整支持 6 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 25 |
Firefox Android
完整支持
19
注意事项
|
Opera Android 完整支持 14 | Safari iOS 完整支持 6 | Samsung Internet Android 完整支持 1.5 |
完整支持
不支持
非标。预期跨浏览器支持较差。
见实现注意事项。
使用非标名称。
<angle-percentage>
<angle>
<basic-shape>
<blend-mode>
<custom-ident>
<dimension>
<frequency-percentage>
<frequency>
<gradient>
<image>
<integer>
<length-percentage>
<length>
<number>
<percentage>
<ratio>
<resolution>
<shape>
<string>
<time-percentage>
<time>
<transform-function>
<url>
color
flex
ident
position