效果:css
代碼:html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>僞元素選擇器</title> <style type="text/css"> .head{ margin: 0 0 0 100px; height: 80px; } .container{ margin: 0 0 0 100px; } p{ width: 400px; height: 60px; border: 1px solid red; position: relative; top: 0; left: 0; } div{ width: 400px; height: 200px; margin: 20px 0 0 0; } p::before{ position: relative; display: block; top: 20px; left: 20px; width: 0; height: 0; float: left; content: ""; border: 10px solid red; border-color: red transparent transparent transparent; } p::after{ position: relative; display: block; top: 20px; right: 20px; width: 0; float: right; height: 0; content: ""; border: 10px solid red; border-color: red transparent transparent transparent; } .div1:first-letter{ font-weight: bold; color: red; font-size: 30px; } .div2:first-line{ font-weight: bold; color: red; font-size: 30px; } .div3::selection{ color: white; background-color: black; } .div3::-moz-selection{ color: white; background-color: black; } .div3::-webkit-selection{ color: white; background-color: black; } </style> </head> <body> <div class="head"> <h1>僞元素選擇器</h1> <h2><a href="inde.html">首頁</a></h2> </div> <div class="container"> <p>::before,::after</p> <div class = 'div1'> Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 </div> <div class = 'div2'> Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺
Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺
Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 </div> <div class = 'div3'> Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺
Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺
Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺Sunlike阿理旺旺 </div> </div> </body> </html>
總結:web
一、first-letter: 獲取第一個字母或中文字;spa
二、first-line:給block,inline-block,table-capttion,table-cell元素提供選取第一行元素的功能;3d
三、::before,::after:給元素添加額外的塊;htm
四、selection:被選中的文本內容。blog