CSS 数据类型定义可用于各种 CSS 属性的值的类型。 每个 CSS 属性都需要特定类型的值,了解这些数据类型对于正确设置 Web 内容的样式和格式至关重要。
下面列出的类型是最常见的,但它并不是任何 CSS 规范中定义的所有类型的完整列表。

语法

selector {
   property: <unit-data-type>;
} 

CSS 语法使用不等号“<”和“">”之间的关键字来指示数据类型。

文本数据类型

这些类型包括关键字、标识符、字符串和 URL。

预定义关键字

表示特定于与其一起使用的属性的预定义关键字(例如 auto、inherit、none)。

CSS-wide 关键字

下表列出了所有 CSS 范围内的关键字:

属性描述
<custom-ident>A user-defined identifier, such as a name given with the grid-area property.
<dashed-ident>Using CSS Custom Properties, a <custom-ident> begin with two dashes.
<string>A string that has been quoted, such as the value for the content property.
url()A reference to a resource, such the background-image value.

Numeric 数据类型

这些数据类型表示数量、索引和位置。 下表列出了所有数值数据类型:

属性描述
<integer>一个或多个小数单位 (0–9)。
<number>实数可能包含小数部分,例如 1 或 1.54。
<dimension>包含单位的数值,例如 23px 或 15em。
<percentage>包含百分比符号的数值,例如 15%。
<ratio>比率表示为<数字> / <数字>。
<flex>CSS 网格布局引入了灵活的长度,由 <number> 表示,并附加 fr 单位以调整网格轨道大小。

Quantities

距离和其他数量是使用这些类型定义的。 下表列出了所有数量:

属性描述
<length>Lengths are a dimension that refers to distances.
<angle>Angles are represented as a <dimension> with deg, grad, rad, or turn units, and used in properties such as linear-gradient().
<time>Duration units are represented as a <dimension> with a s or ms unit.
<resolution>This is a <dimension> with the unit identifier dpi, dpcm, dppx, or x.

类型的组合

同时接受尺寸和百分比值的 CSS 属性属于此类。 百分比值将转换为相对于允许尺寸的数量。 同时采用百分比和尺寸的属性将使用以下类型之一:

属性描述
<length-percentage>A type that can take a a length or a percentage value.
<angle-percentage>A type that can take a a angle or a percentage value.
<time-percentage>A type that can take a a time or a percentage value.

Color

Color related properties define the <color> data type and additional types related to colors.

The following table lists all the color related data types:

属性描述
<color>A color represented as a keyword or a numerical value.
<color-interpolation-method>Determines the color space used when creating a transition between different <color> values.
<hex-color>Describes the hexadecimal color syntax of an sRGB color using the primary color components (red, green, blue) denoted as hexadecimal numbers, along with its transparency.
<system-color>Commonly linked to the default color selections for different components on a webpage.
<alpha-value>Represents the transparency of a color. The value can be either a <number> (0 is fully transparent, 1 is fully opaque) or a <percentage> (0 is fully transparent, 100% is fully opaque).
<hue>Define the <angle> of the color wheel for the <absolute-color-functions> component using units such as, deg, grad, rad, turn, or a unitless number (interpreted as deg).
<hue-interpolation-method>Determines the algorithm that is used for interpolation between hue values. This method specifies how to find a midpoint between two hue values based on a color wheel. It is a component of the <color-interpolation-method> data type.
<named-color>Specified as a <ident> in syntax, depicts colors by names such as red, blue, black, or light green.

图像

CSS 图像规范定义了与图像相关的数据类型,例如渐变。 下表列出了所有与图像相关的数据类型:

属性描述
<image>指向图像或颜色渐变的 URL。

2D定位

下表列出了所有与 2D 定位相关的数据类型:

属性描述
<position>Determines the position of an object region by providing a keyword value, such as top or left, or a <length-percentage>.

计算数据类型

CSS math functions use these data types in calculations. The following table lists all the calculation data types:

属性描述
<calc-sum>A calculation is a series of calculated values separated by addition (+) and subtraction (-) operators. This data type requires that both values contain units.
<calc-constant>Defines CSS keywords for numeric constants such as e and π, which can be used in CSS math functions.

Display

下表列出了所有与显示相关的数据类型:

属性描述
<display-box>Determines if an element creates display boxes or not.
<display-inside>Determines the inner display of an element, which specifies the formatting context type, for a non-replaced element.
<display-internal>Determines the internal display values, which have relevance only to that particular layout model.
<display-legacy>A single-keyword syntax has been used for display property in CSS 2, which required separate keywords for block-level and inline-level variants of the same layout model.
<display-listitem>The keyword list-item makes the element to generate a ::marker pseudo-element with the content that is specified by the list-style properties, along with a main box of the specified type for its own contents.
<display-outside>An element's outer display type, which is essential in flow layout, is determined by the <display-outside> keywords.

其他数据类型

下表列出了更多数据类型:

属性描述
<absolute-size>Defines absolute font size in the font shorthand and font-size properties.
<basic-shape>Defines different shapes used for properties such as clip-path, shape-outside, and offset-path.
<blend-mode>Specifies the color scheme that should be used when elements overlap.
<box-edge>Defines different box edges, such as content-box and border-box.
<easing-function>Control the speed of a transition or animation between two states of an element.
<filter-function>Signifies a graphical effect, which changes the appearance of an input image.
<generic-family>Signifies the keyword values for generic font families.
<gradient>Demonstrates a progressive transition between two or more colors.
<ident>Signifies an arbitrary string that is used as an identifier.
<line-style>Specifying how a line appears or doesn't appear in a certain context are included in the <line-style> enumerated value type.
<overflow>The keyword values used in relation to the shorthand overflow property and the longhand overflow-block, overflow-inline, overflow-x, and overflow-y properties are indicated by the enumerated value type <overflow>.
<relative-size>Define a relative size to the calculated size of the parent element.
<transform-function>The transformation functions are responsible for rotating, scaling (resizing), skewing (distorting), or moving an element in two-dimensional (2D) and three-dimensional (3D) space.