1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>before 和 after</title>
  6. <style>
  7. .wrapper * {
  8. box-sizing: border-box;
  9. }
  10. .wrapper>div {
  11. height: 250px;
  12. width: 250px;
  13. border: 1px solid #ccc;
  14. background: #000;
  15. margin: 10px;
  16. float: left;
  17. color: #D9D9D9;
  18. padding: 30px;
  19. text-align: center;
  20. }
  21. /*基础用法1*/
  22. .base1:before {
  23. content: "我是::before";
  24. color: #FB0D0D;
  25. }
  26. .base1:after {
  27. content: "我是::after";
  28. color: #f70;
  29. }
  30. /*基础用法2*/
  31. .base2:before {
  32. content: '\ABCD\ABCD\ABCD\ABCD';
  33. white-space: pre;
  34. color: #FB0D0D;
  35. }
  36. .base2:after {
  37. content: '\609\609\609\609\609';
  38. white-space: pre;
  39. color: #f70;
  40. }
  41. /*::before , ::after添加背景图*/
  42. .base3:before {
  43. content: url(icon-plus.png);
  44. }
  45. .base3:after {
  46. content: url(icon-plus.png);
  47. }
  48. /*取自定义属性*/
  49. .base4:before {
  50. content: attr(title);
  51. color: #E8E3AA;
  52. }
  53. .base4:after {
  54. content: attr(data-test);
  55. color: #D8CF23;
  56. }
  57. /*小试身手合集*/
  58. .base5,
  59. .base6,
  60. .base7,
  61. .base8 {
  62. position: relative;
  63. }
  64. /*小试身手1*/
  65. .base5:before {
  66. content: "";
  67. display: block;
  68. position: absolute;
  69. top: 0;
  70. left: 0;
  71. height: 50px;
  72. width: 50px;
  73. border-top: 5px solid #f70;
  74. border-left: 5px solid #f70;
  75. }
  76. .base5:after {
  77. content: "";
  78. display: block;
  79. position: absolute;
  80. right: 0;
  81. bottom: 0;
  82. height: 50px;
  83. width: 50px;
  84. border-right: 5px solid #f70;
  85. border-bottom: 5px solid #f70;
  86. }
  87. /*小试身手2*/
  88. .base6:before {
  89. content: "";
  90. display: block;
  91. position: absolute;
  92. left: 0;
  93. top: 0;
  94. width: 100%;
  95. height: 100%;
  96. background: -webkit-linear-gradient(45deg, rgba(75, 65, 45, .3), rgba(123, 456, 789, .3), rgba(854, 183, 729, .3));
  97. background: linear-gradient(45deg, rgba(75, 65, 45, .3), rgba(123, 456, 789, .3), rgba(854, 183, 729, .3));
  98. z-index: 1;
  99. }
  100. .base6:after {
  101. content: "";
  102. display: block;
  103. position: absolute;
  104. bottom: 0;
  105. right: 0;
  106. width: 100%;
  107. height: 100%;
  108. background: -webkit-linear-gradient(-45deg, rgba(50, 125, 55, .7), rgba(55, 3, 45, .5), rgba(99, 12, 3, .8));
  109. background: linear-gradient(-45deg, rgba(50, 125, 55, .7), rgba(55, 3, 45, .5), rgba(99, 12, 3, .8));
  110. z-index: 1;
  111. }
  112. /*小试身手3*/
  113. .base7:before {
  114. content: "";
  115. display: block;
  116. position: absolute;
  117. width: 50px;
  118. height: 50px;
  119. -webkit-animation: circle 2s ease-in-out infinite;
  120. -moz-animation: circle 2s ease-in-out infinite;
  121. -ms-animation: circle 2s ease-in-out infinite;
  122. -o-animation: circle 2s ease-in-out infinite;
  123. animation: circle 2s ease-in-out infinite;
  124. background: #C3172C;
  125. }
  126. .base7:after {
  127. content: "";
  128. display: block;
  129. position: absolute;
  130. content: "";
  131. background: #14965E;
  132. display: block;
  133. position: absolute;
  134. width: 50px;
  135. height: 50px;
  136. -webkit-animation: circle 2s ease-in-out infinite;
  137. -moz-animation: circle 2s ease-in-out infinite;
  138. -ms-animation: circle 2s ease-in-out infinite;
  139. -o-animation: circle 2s ease-in-out infinite;
  140. animation: circle 2s ease-in-out infinite;
  141. }
  142. /*小试身手4*/
  143. .base8:before {
  144. content: "1";
  145. display: block;
  146. position: absolute;
  147. height: 100%;
  148. width: 10px;
  149. background: #6F0ECF;
  150. left: 0;
  151. top: 0;
  152. margin-left: -10px;
  153. }
  154. .base8:hover:before {
  155. background: #9F81DE;
  156. -webkit-transform: rotate(-90deg) translate(-30%, 30%);
  157. transform: rotate(-90deg) translate(-30%, 30%);
  158. -webkit-transition: all 2s ease-in;
  159. transition: all 2s ease-in;
  160. }
  161. .base8:after {
  162. content: ".";
  163. display: block;
  164. position: absolute;
  165. height: 100%;
  166. width: 10px;
  167. background: #6F0ECF;
  168. right: 0;
  169. bottom: 0;
  170. margin-right: -10px;
  171. }
  172. .base8:hover:after {
  173. background: #9F81DE;
  174. -webkit-transform: rotate(-90deg) translate(-30%, 30%);
  175. transform: rotate(-90deg) translate(-30%, 30%);
  176. -webkit-transition: all 2s ease-in;
  177. transition: all 2s ease-in;
  178. }
  179. @-webkit-keyframes circle {
  180. from {
  181. border-radius: 0%;
  182. top: 0;
  183. }
  184. 35% {
  185. background: #2B2FDC;
  186. left: 30%;
  187. top: 50%;
  188. }
  189. 75% {
  190. background: #AB9E9E;
  191. right: 0;
  192. bottom: 20%;
  193. }
  194. to {
  195. border-radius: 100%;
  196. top: 250px;
  197. left: 15%;
  198. bottom: 50%;
  199. }
  200. }
  201. @-moz-keyframes circle {
  202. from {
  203. border-radius: 0%;
  204. top: 0;
  205. }
  206. 35% {
  207. background: #2B2FDC;
  208. left: 30%;
  209. top: 50%;
  210. }
  211. 75% {
  212. background: #AB9E9E;
  213. right: 0;
  214. bottom: 20%;
  215. }
  216. to {
  217. border-radius: 100%;
  218. top: 250px;
  219. left: 15%;
  220. bottom: 50%;
  221. }
  222. }
  223. @keyframes circle {
  224. from {
  225. border-radius: 0%;
  226. top: 0;
  227. }
  228. 35% {
  229. background: #2B2FDC;
  230. left: 30%;
  231. top: 50%;
  232. }
  233. 75% {
  234. background: #AB9E9E;
  235. right: 0;
  236. bottom: 20%;
  237. }
  238. to {
  239. border-radius: 100%;
  240. top: 250px;
  241. left: 15%;
  242. bottom: 50%;
  243. }
  244. }
  245. </style>
  246. </head>
  247. <body>
  248. <div class="wrapper">
  249. <div class="base1">添加文字</div>
  250. </div>
  251. <div class="wrapper">
  252. <div class="base2">添加unicode文字,图标的需要特殊字体库[font-face引入webfont,内部的unicode编码]</div>
  253. </div>
  254. <div class="wrapper">
  255. <div class="base3">添加背景图,但是不如用background好控制</div>
  256. </div>
  257. <div class="wrapper">
  258. <div class="base4" title="呵呵哒,萌萌哒" data-test="我是HTML5自定义属性">取自定义属性,取值不带双引号!!</div>
  259. </div>
  260. <div class="wrapper">
  261. <div class="base5">小试身手1:框框</div>
  262. </div>
  263. <div class="wrapper">
  264. <div class="base6">小试身手2:渐变</div>
  265. </div>
  266. <div class="wrapper">
  267. <div class="base7">小试身手3:变形</div>
  268. </div>
  269. <div class="wrapper">
  270. <div class="base8">小试身手4:追加</div>
  271. </div>
  272. </body>
  273. </html>

W3C的解释用一句话概括:单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素。

基础理论

::before和::after 是用来给元素添加额外内容的,因为只存在于作用元素内容的前后
::before和::after 内部的content支持以下三种特性!

  • 字符串[“可以是符号什么的也可以是单纯的字符” ,支持unicode编码!!]
  • 属性[attr() , 可以获取标签上的元素属性,比如data-*的自定义属性,title,alt这些]
  • 引用媒体文件[url ,可以链接图片作为背景图什么的]
  • 计数器[counter()]

image.png