<percentage>
CSS
data type
represents a percentage value. It is often used to define a size as relative to an element's parent object. Numerous properties can use percentages, such as
width
,
height
,
margin
,
padding
,和
font-size
.
<length>
value) will be accessible on the inherited property, not the percentage value.
<percentage>
data type consists of a
<number>
followed by the percentage sign (
%
). Optionally, it may be preceded by a single
+
or
-
sign, although negative values are not valid for all properties. As with all CSS dimensions, there is no space between the symbol and the number.
When animated, values of the
<percentage>
data type are interpolated as real, floating-point numbers. The speed of the interpolation is determined by the
timing function
associated with the animation.
<div style="background-color:navy;">
<div style="width:50%; margin-left:20%; background-color:chartreuse;">
Width: 50%, Left margin: 20%
</div>
<div style="width:30%; margin-left:60%; background-color:pink;">
Width: 30%, Left margin: 60%
</div>
</div>
The above HTML will output:
<div style="font-size:18px;"> <p>Full-size text (18px)</p> <p><span style="font-size:50%;">50% (9px)</span></p> <p><span style="font-size:200%;">200% (36px)</span></p> </div>
The above HTML will output:
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Values and Units Module Level 4
The definition of '<percentage>' in that specification. |
编者草案 | No significant change. |
|
CSS Values and Units Module Level 3
The definition of '<percentage>' in that specification. |
候选推荐 | No significant change from CSS Level 2 (Revision 1). |
|
CSS Level 2 (Revision 1)
The definition of '<percentage>' in that specification. |
推荐 | No change from CSS Level 1. |
|
CSS Level 1
The definition of '<percentage>' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
<percentage>
|
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 |
完整支持
<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