css的content属性使用

岁月这东西,总是要按时带走它要带走的部分。

p 标签内容空时

1
2
3
4
p:empty:before {
color: #CCC;
content: "Don't make me empty!";
}

p 标签起始增加内部内容

1
2
3
4
p:before { 
display: block;
content: 'Some';
}

p 标签结尾增加内部内容

1
2
3
4
p:after { 
display: block;
content: 'Some';
}

将a标签的title属性添加到a的text

1
2
3
4
5
a:before {
content: attr(title) ": ";
}

<a title="A web design community." href="https://css-tricks.com">CSS-Tricks</a>

增加 unicode 字符

https://unicode-table.com/cn/2665/

1
2
3
4
5
p::after {
content: "\2665";
color: red;
font-size: 23px;
}

tooltips实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
a {
color: #900;
text-decoration: none;
}

a:hover {
color: red;
position: relative;
}

a[title]:hover::after {
content: attr(title);
padding: 4px 8px;
color: #333;
position: absolute;
left: 0;
top: 100%;
white-space: nowrap;
z-index: 20;
border-radius: 5px;
box-shadow: 0px 0px 4px #222;
background-image: linear-gradient(#eeeeee, #cccccc);
}

标题前后加对称图标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
h2 {
text-align: center;
}
h2:before, h2:after {
font-family: "Some cool font with glyphs", serif;
content: "\00d7"; /* Some fancy character */
color: #c83f3f;
}
h2:before {
margin-right: 10px;
}
h2:after {
margin-left: 10px;
}

实现面包屑效果

1
2
3
4
5
6
7
8
9
10
11
12
li {
float: left;
margin-left: 12px;
list-style-type: none;
}

li:after {
content: "// ";
position: relative;
left: 3px;
}

https://css-tricks.com/pseudo-element-roundup/#top-of-site