- 利用content屬性,為元素添加內容修飾:
1) 添加字符串:
使用引號包括一段字符串,將會向元素內容中添加字符串。栗子:
a:after { content: "after content"; }
2) 使用attr()方法,調用當前元素的屬性的值:
栗子:
a:after { content: attr(href); }
a:after { content: attr(data-attr); }
3)使用url()方法,引用多媒體文件:
栗子:
a::before { content: url(logo.png); }
- 使用counter()方法,調用計時器:
栗子:
h:before { counter-increment: chapter; cotent: "Chapter " counter(chapter) ". " }
- 清除浮動:
.clear-fix { *overflow: hidden; *zoom: 1; }
.clear-fix:after { display: table; content: ""; width: 0; clear: both; }
- 特效妙用:
// CSS代碼
a {
position: relative;
display: inline-block;
text-decoration: none;
color: #000;
font-size: 32px;
padding: 5px 10px;
}
a::before, a::after {
content: "";
transition: all 0.2s;
}
a::before {
left: 0;
}
a::after {
right: 0;
}
a:hover::before, a:hover::after {
position: absolute;
}
a:hover::before { content: "\5B"; left: -20px; }
a:hover::after { content: "\5D"; right: -20px; }
// HTML代碼
<a href="#">我是個超鏈接</a>
- 特殊形狀的實現:
舉個栗子:(譬如對話氣泡)
// CSS代碼
.tooltip {
position: relative;
display: inline-block;
padding: 5px 10px;
background: #80D4C8;
}
.tooltip:before {
content: "";
display: block;
position: absolute;
left: 50%;
margin-left: -5px;
bottom: -5px;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 5px solid #80D4C8;
}
// HTML代碼
<div class="tooltip">I'm a tooltip.</div>