前言
單冒号寫法相容性比較強[可支援解析的浏覽器較多],雙冒号的寫法隻支援較新的主流浏覽器;
W3C的解釋用一句話概括:單冒号(
:
)用于CSS3僞類,雙冒号(
::
)用于CSS3僞元素。
基礎理論
-
和::before
是用來給元素添加額外内容的,因為隻存在于作用元素内容的前後::after
-
和::before
内部的content支援以下三種特性!::after
- 字元串[“可以是符号什麼的也可以是單純的字元” ,支援unicode編碼!!]
- 屬性[
, 可以擷取标簽上的元素屬性,比如attr()
的自定義屬性,title,alt這些]data-*
- 引用媒體檔案[url ,可以連結圖檔作為背景圖什麼的]
- 計數器[
]counter()
靈活運用這兩個可以做好多東東,比如清除浮動,各種額外的視覺效果[陰影,跳轉等]
效果
代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>before 和 after</title>
<style>
.wrapper * {
box-sizing: border-box;
}
.wrapper>div {
height: 250px;
width: 250px;
border: 1px solid #ccc;
background: #000;
margin: 10px;
float: left;
color: #D9D9D9;
padding: 30px;
text-align: center;
}
/*基礎用法1*/
.base1:before {
content: "我是::before";
color: #FB0D0D;
}
.base1:after {
content: "我是::after";
color: #f70;
}
/*基礎用法2*/
.base2:before {
content: '\ABCD\ABCD\ABCD\ABCD';
white-space: pre;
color: #FB0D0D;
}
.base2:after {
content: '\609\609\609\609\609';
white-space: pre;
color: #f70;
}
/*::before , ::after添加背景圖*/
.base3:before {
content: url(icon-plus.png);
}
.base3:after {
content: url(icon-plus.png);
}
/*取自定義屬性*/
.base4:before {
content: attr(title);
color: #E8E3AA;
}
.base4:after {
content: attr(data-test);
color: #D8CF23;
}
/*小試身手合集*/
.base5,
.base6,
.base7,
.base8 {
position: relative;
}
/*小試身手1*/
.base5:before {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
height: 50px;
width: 50px;
border-top: 5px solid #f70;
border-left: 5px solid #f70;
}
.base5:after {
content: "";
display: block;
position: absolute;
right: 0;
bottom: 0;
height: 50px;
width: 50px;
border-right: 5px solid #f70;
border-bottom: 5px solid #f70;
}
/*小試身手2*/
.base6:before {
content: "";
display: block;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: -webkit-linear-gradient(45deg, rgba(75, 65, 45, .3), rgba(123, 456, 789, .3), rgba(854, 183, 729, .3));
background: linear-gradient(45deg, rgba(75, 65, 45, .3), rgba(123, 456, 789, .3), rgba(854, 183, 729, .3));
z-index: 1;
}
.base6:after {
content: "";
display: block;
position: absolute;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
background: -webkit-linear-gradient(-45deg, rgba(50, 125, 55, .7), rgba(55, 3, 45, .5), rgba(99, 12, 3, .8));
background: linear-gradient(-45deg, rgba(50, 125, 55, .7), rgba(55, 3, 45, .5), rgba(99, 12, 3, .8));
z-index: 1;
}
/*小試身手3*/
.base7:before {
content: "";
display: block;
position: absolute;
width: 50px;
height: 50px;
-webkit-animation: circle 2s ease-in-out infinite;
-moz-animation: circle 2s ease-in-out infinite;
-ms-animation: circle 2s ease-in-out infinite;
-o-animation: circle 2s ease-in-out infinite;
animation: circle 2s ease-in-out infinite;
background: #C3172C;
}
.base7:after {
content: "";
display: block;
position: absolute;
content: "";
background: #14965E;
display: block;
position: absolute;
width: 50px;
height: 50px;
-webkit-animation: circle 2s ease-in-out infinite;
-moz-animation: circle 2s ease-in-out infinite;
-ms-animation: circle 2s ease-in-out infinite;
-o-animation: circle 2s ease-in-out infinite;
animation: circle 2s ease-in-out infinite;
}
/*小試身手4*/
.base8:before {
content: "1";
display: block;
position: absolute;
height: 100%;
width: 10px;
background: #6F0ECF;
left: 0;
top: 0;
margin-left: -10px;
}
.base8:hover:before {
background: #9F81DE;
-webkit-transform: rotate(-90deg) translate(-30%, 30%);
transform: rotate(-90deg) translate(-30%, 30%);
-webkit-transition: all 2s ease-in;
transition: all 2s ease-in;
}
.base8:after {
content: ".";
display: block;
position: absolute;
height: 100%;
width: 10px;
background: #6F0ECF;
right: 0;
bottom: 0;
margin-right: -10px;
}
.base8:hover:after {
background: #9F81DE;
-webkit-transform: rotate(-90deg) translate(-30%, 30%);
transform: rotate(-90deg) translate(-30%, 30%);
-webkit-transition: all 2s ease-in;
transition: all 2s ease-in;
}
@-webkit-keyframes circle {
from {
border-radius: 0%;
top: 0;
}
35% {
background: #2B2FDC;
left: 30%;
top: 50%;
}
75% {
background: #AB9E9E;
right: 0;
bottom: 20%;
}
to {
border-radius: 100%;
top: 250px;
left: 15%;
bottom: 50%;
}
}
@-moz-keyframes circle {
from {
border-radius: 0%;
top: 0;
}
35% {
background: #2B2FDC;
left: 30%;
top: 50%;
}
75% {
background: #AB9E9E;
right: 0;
bottom: 20%;
}
to {
border-radius: 100%;
top: 250px;
left: 15%;
bottom: 50%;
}
}
@keyframes circle {
from {
border-radius: 0%;
top: 0;
}
35% {
background: #2B2FDC;
left: 30%;
top: 50%;
}
75% {
background: #AB9E9E;
right: 0;
bottom: 20%;
}
to {
border-radius: 100%;
top: 250px;
left: 15%;
bottom: 50%;
}
}
</style>
</head>
<body>
<div class="wrapper">
<div class="base1">添加文字</div>
</div>
<div class="wrapper">
<div class="base2">添加unicode文字,圖示的需要特殊字型庫[font-face引入webfont,内部的unicode編碼]</div>
</div>
<div class="wrapper">
<div class="base3">添加背景圖,但是不如用background好控制</div>
</div>
<div class="wrapper">
<div class="base4" title="呵呵哒,萌萌哒" data-test="我是HTML5自定義屬性">取自定義屬性,取值不帶雙引号!!</div>
</div>
<div class="wrapper">
<div class="base5">小試身手1:框框</div>
</div>
<div class="wrapper">
<div class="base6">小試身手2:漸變</div>
</div>
<div class="wrapper">
<div class="base7">小試身手3:變形</div>
</div>
<div class="wrapper">
<div class="base8">小試身手4:追加</div>
</div>
</body>
</html>
注意點
-
和::before
的與僞類的結合使用是有先後順序的;::after
.class:hover::before{} /*是有效的*/
.class::before:hover{} /*是無效的*/