非常教程

CSS参考手册

值和单位 | Values & Units

attr()

attr() CSS函数用于检索所选择的元素的属性值,并且在样式表中使用它。它也可以用于伪元素,在这种情况下,它返回的是该伪元素的源元素上的属性值。

/* Simple usage */
attr(data-count);
attr(title);

/* With type */
attr(src url);
attr(data-count number);
attr(data-width px);

/* With fallback */
attr(data-count number, 0);
attr(src url, '');
attr(data-width px, inherit);
attr(data-something, 'default');

注意:attr()函数可以用于任何CSS属性,但仅支持除了实验性content以外的属性。

语法

可能值

attribute-name——是CSS中引用的HTML元素的属性名称。

<type-or-unit>——是一个关键字,表示属性值的类型或其单位,因为在HTML中,某些属性具有隐式单位。如果<type-or-unit>所作用的属性的值是无效的,则attr()表达式也将是无效的。如果省略,则默认为string。有效值的列表是:

Keyword

Associated type

Comment

Default value

string

<string>

The attribute value is treated as a CSS <string>. It is NOT reparsed, and in particular the characters are used as-is instead of CSS escapes being turned into different characters.

An empty string.

color

<color>

The attribute value is parsed as a hash (3- or 6-value hash) or a keyword. It must be a valid CSS <string> value. Leading and trailing spaces are stripped.

currentColor

url

<url>

The attribute value is parsed as a string that is used inside a CSS url()function. Relative URL are resolved relatively to the original document, not relatively to the style sheet. Leading and trailing spaces are stripped.

The url about:invalid that points to a non-existent document with a generic error condition.

integer

<integer>

The attribute value is parsed as a CSS <integer>. If it is not valid, that is not an integer or out of the range accepted by the CSS property, the default value is used. Leading and trailing spaces are stripped.

0, or, if 0 is not a valid value for the property, the property's minimum value.

number

<number>

The attribute value is parsed as a CSS <number>. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used. Leading and trailing spaces are stripped.

0, or, if 0 is not a valid value for the property, the property's minimum value.

length

<length>

The attribute value is parsed as a CSS <length> dimension, that is including the unit (e.g. 12.5em). If it is not valid, that is not a length or out of the range accepted by the CSS property, the default value is used. If the given unit is a relative length, attr()computes it to an absolute length. Leading and trailing spaces are stripped.

0, or, if 0 is not a valid value for the property, the property's minimum value.

em, ex, px, rem, vw, vh, vmin, vmax, mm, cm, in, pt, or pc

<length>

The attribute value is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as a <length> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used. If the given unit is a relative length, attr()computes it to an absolute length. Leading and trailing spaces are stripped.

0, or, if 0 is not a valid value for the property, the property's minimum value.

angle

<angle>

The attribute value is parsed as a CSS <angle> dimension, that is including the unit (e.g. 30.5deg). If it is not valid, that is not an angle or out of the range accepted by the CSS property, the default value is used. Leading and trailing spaces are stripped.

0deg, or, if 0deg is not a valid value for the property, the property's minimum value.

deg, grad, rad

<angle>

The attribute value is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as an <angle> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used. Leading and trailing spaces are stripped.

0deg, or, if 0deg is not a valid value for the property, the property's minimum value.

time

<time>

The attribute value is parsed as a CSS <time> dimension, that is including the unit (e.g. 30.5ms). If it is not valid, that is not a time or out of the range accepted by the CSS property, the default value is used. Leading and trailing spaces are stripped.

0s, or, if 0s is not a valid value for the property, the property's minimum value.

s, ms

<time>

The attribute value is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as an<time> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used. Leading and trailing spaces are stripped.

0s, or, if 0s is not a valid value for the property, the property's minimum value.

frequency

<frequency>

The attribute value is parsed as a CSS <frequency> dimension, that is including the unit (e.g. 30.5kHz). If it is not valid, that is not a frequency or out of the range accepted by the CSS property, the default value is used.

0Hz, or, if 0Hz is not a valid value for the property, the property's minimum value.

Hz, kHz

<frequency>

The attribute value is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as a <frequency> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used. Leading and trailing spaces are stripped.

0Hz, or, if 0Hz is not a valid value for the property, the property's minimum value.

%

<percentage>

The attribute value is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as a <percentage>. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used. If the given value is used as a length, attr()computes it to an absolute length. Leading and trailing spaces are stripped.

0%, or, if 0% is not a valid value for the property, the property's minimum value.

<fallback>——如果关联属性丢失或包含无效值,则使用该值。回退值(fallback)必须在attr()使用的地方有效,即使它未被使用,也不得包含其他attr()表达式。如果attr()不是属性的唯一组件值,则其<fallback>值必须是由<type-or-unit>所定义的类型。如果没有设置,CSS将使用为每个<type-or-unit>定义的默认值。

形式语法

attr( <attr-name> <type-or-unit>? [, <attr-fallback> ]? )where 
<type-or-unit> = string | integer | color | url | integer | number | length | angle | time | frequency | em | ex | px | rem | vw | vh | vmin | vmax | mm | q | cm | in | pt | pc | deg | grad | rad | ms | s | Hz | kHz | %

实例

HTML

<p data-foo="hello">world</p>

CSS

p::before {
  content: attr(data-foo) " ";
}

结果

规范

Specification

Status

Comment

CSS Values and Units Module Level 3The definition of 'attr()' in that specification.

Candidate Recommendation

Added two optional parameters; can be used on all properties; may return other values than <string>. These changes are experimental and may be dropped during the CR phase if browser support is too small.

CSS Level 2 (Revision 1)The definition of 'attr()' in that specification.

Recommendation

Limited to the content property; always return a <string>.

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

2.0

(Yes)

1.0 (1.7 or earlier)

8

9.0

3.1

Usage in other properties than content and with non-string values

No support2

No support

No support 1

No support

No support

?

Feature

Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

2.1

(Yes)

1.0 (1.0)

8

10.0

3.1

Usage in other properties than contentand with non-string values

?

No support

No support 1

No support

?

?

CSS

层叠样式表( Cascading Style Sheets )是一种用来表现 HTML 或 XML 等文件样式的计算机语言。CSS 不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。

CSS目录

1.动画和转换 | Animations & Transitions
2.背景和边框 | Backgrounds & Borders
3.基本框模型 | Basic Box Model
4.基本用户界面 | Basic User Interface
5.框对齐 | Box Alignment
6. 级联和继承 | Cascading & Inheritance
7.颜色 | Color
8. 合成与混合 | Compositing & Blending
9.条件和规则 | Conditional Rules
10.计数器样式 | Counter Styles
11.设备适配 | Device Adaptation
12.扩展 | Extensions
13.滤镜效果 | Filter Effects
14.灵活的框布局 | Flexible Box Layout
15.字体 | Fonts
16.片段模块 | Fragmentation
17.全屏 API | Fullscreen API
18. 生成内容 | Generated Content
19.网格布局 | Grid Layout
20.图像值 | Image Values
21.初始线格局 | Inline Layout
22.列表和计数器 | Lists & Counters
23.逻辑属性 | Logical Properties
24.面具 | Masking
25.媒体查询 | Media Queries
26.杂项 | Miscellaneous
27.Miscellaneous Level 1
28.Miscellaneous Level 2
29.运动路径 | Motion Path
30. 多列布局 | Multi-column Layout
31.命名空间 | Namespaces
32.项目模型 | Object Model View
33.网页媒体 | Paged Media
34.定位布局 | Positioned Layout
35.伪元素 | Pseudo-
36.节奏大小 | Rhythmic Sizing
37. Ruby布局 | Ruby Layout
38.可缩放矢量图形 | Scalable Vector Graphics
39.滚动快照 | Scroll Snap
40.选择 | Selectors
41.形状 | Shapes
42.文本 | Text
43.文字装饰 | Text Decoration
44.变换 | Transforms
45.值和单位 | Values & Units
46.变量 | Variables
47.写入模型 | Writing Modes
48.CSS 教程
49.CSS 创建
50.CSS Id 和 Class选择器
51.CSS 简介
52.CSS 盒子模型
53.CSS Table(表格)
54.CSS 列表样式(ul)
55.CSS 链接(link)
56.CSS Fonts(字体)
57.CSS Text(文本)
58.CSS Backgrounds(背景)
59.CSS Display(显示) 与 Visibility(可见性)
60.CSS 尺寸 (Dimension)
61.CSS 分组和嵌套
62.CSS 轮廓(outline)属性
63.CSS Border(边框)
64.CSS 图像透明/不透明
65.CSS 导航栏
66.CSS 伪元素
67.CSS 伪类
68.CSS Float(浮动)
69.CSS Position(定位)
70.CSS 总结
71.CSS 属性选择器
72.CSS 媒体类型
73.CSS 图像拼合技术
74.CSS 实例
75.CSS 组合选择符
76.响应式 Web 设计 – 框架
77.响应式 Web 设计 – 视频(Video)
78.CSS 提示工具(Tooltip)
79.CSS 布局 Overflow
80.CSS 计数器
81.CSS 表单