CSS <percentage> Data Type
The CSS <percentage>
data type denotes an integer number, positive or negative.
When you see <percentage>
(including the <
and >
) anywhere in the CSS specifications, this refers to the fact that the value can be a valid percentage.
A percentage consists of a <number>
followed by a %
(percent sign).
Here are examples of valid <percentage>
values:
A <percentage>
Example
The width
property allows the following values.
That definition includes the <percentage>
type (among others). So if you decide to use a percentage value, you could write something like this:
Here's a working example:
Percentage of What?
Percentage values are always relative to another quantity, for example a length.
In the CSS specifications, each property that allows percentages also defines the quantity to which the percentage refers. This quantity can be a value of another property for the same element, the value of a property for an ancestor element, a measurement of the formatting context (e.g. the width of a containing block), or something else.
Here are some examples of percentage values along with an explanation of what each percentage value is relative to:
Combining Percentages with Dimensions or Numbers
There are cases where a percentage value will represent the same quantity as a dimension or number. In these cases, the percentage can be used in a calc()
function where it can be combined with another dimension or number.
Here's an example:
In this case, both values are converted to absolute lengths and added together.
When a CSS property allows percentage values to be used in this way, the CSS specification may use the following notations:
- <length-percentage>
- Equivalent to [
<length>
|<percentage>
], where the<percentage>
will resolve to a<length>
. - <frequency-percentage>
- Equivalent to [
<frequency>
|<percentage>
], where the<percentage>
will resolve to a<frequency>
. - <angle-percentage>
- Equivalent to [
<angle>
|<percentage>
], where the<percentage>
will resolve to a<angle>
. - <time-percentage>
- Equivalent to [
<time>
|<percentage>
], where the<percentage>
will resolve to a<time>
. - <number-percentage>
- Equivalent to [
<number>
|<percentage>
], where the<percentage>
will resolve to a<number>
.
CSS Specifications
- The
<percentage>
data type is defined in CSS Values and Units Module Level 3 (W3C Candidate Recommendation, 29 September 2016) - It is also defined in Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification (W3C Recommendation 07 June 2011)
- And it's also defined in Cascading Style Sheets, level 1 (W3C Recommendation 17 Dec 1996)