既然說到僞類,這裏就用足夠的代碼給表現一下他們的神奇用法。從簡單到複雜,能夠清晰的看清到僞類的諸多使用方法,對於有些功能近似的就取其一舉例了:css
爲第一個字添加樣式,這裏用一個首字下沉的例子來演示一下:html
<!--html部分--> <p>中國是以華夏文明爲源泉<!--內容省略--></p> <!--css部分--> p:first-letter{ display: block; float: left; margin-right: .2em; font-size: 1.7em; }
爲段落的第一行添加樣式:ui
<!--html部分--> <p style="text-align: center;"> 錦瑟<br/> 錦瑟無故五十弦,一弦一柱思華年。<br/> 莊生曉夢迷蝴蝶,望帝春心託杜鵑。<br/> 滄海月明珠有淚,藍田日暖玉生煙。<br/> 此情可待成追憶?只是當時已惘然。<br/> </p> <!--css部分--> p:first-line{ font-weight: bold; font-size: 1.7em; }
設置文字被選中是的狀態,仍是上面那段文字:url
<!--css部分--> .select::selection{ background-color: yellowgreen; }
內容爲空的元素樣式spa
<!--html部分--> <div></div> <div>我有內容</div> <!--css部分--> div{ width: 60px; height: 40px; background-color: lightgray; margin: 5px; } div:empty{ background-color: darkgray; }
若是<a></a>
中間沒有內容,把href的值做爲內容:firefox
<!--html部分--> <a href="www.baidu.com"></a> <!--css部分--> a:empty:before{ content: attr(href); }
當元素得到焦點時的樣式3d
<!--html部分--> <input tyle="text" /> <!--css部分--> input[type="text"]:focus{ border: 1px purple solid; box-shadow: 0 0 15px black; }
被禁用元素的樣式code
<!--html部分--> <input type="text" disabled /> <input type="text" /> <!--css部分--> input:disabled{ background-color: red; } input:enabled{ background-color: yellow; }
僞類 | 描述 |
---|---|
:checked | input中選中的radio、checkbox和select |
:read-only | 有readonly屬性的input、select和textarea元素(firefox不支持) |
:read-write | 沒有readonly屬性的input、select和textarea可寫元素(firefox不支持) |
:indeterminate | 沒有任一項被選中的radio組(firefox不支持) |
:invalid | input表單驗證不經過的元素 |
:valid | input表單驗證經過的元素 |
<!-- html部分 --> <input name="multi" type="checkbox" checked /> <input name="multi" type="checkbox" /> <input type="text" readonly value="read-only" /> <input type="text" value="read-write" /> <hr> <input type="radio" name="sel" /> <input type="radio" name="sel" /> <input type="radio" name="sel" /> <hr> <input type="email" /> <input type="email" /> <!-- css部分 --> input[type="checkbox"]:checked{ outline: 2px solid red; } input:read-only{ background-color: yellow; } input:read-write{ background-color: lightgreen; } input:indeterminate{ outline: 1px solid blue; } input[type="email"]:invalid{ background-color: red; color: #fff; } input[type="email"]:valid{ background-color: #lightgreen; }
僞類 | 描述 |
---|---|
:required | 具備required屬性的input、select和textarea元素 |
:optional | 沒有required屬性的可編輯的input、select和textarea元素 |
:in-range | 在規定範圍內的合法輸入 |
:out-of-range | 不在規定範圍內的合法輸入 |
:default | 默認樣式(僅firefox支持) |
<!-- html部分 --> <input type="text" required /> <input type="text" /> <hr> <input type="number" min="20" max="80" /> <input type="number" min="20" max="80" /> <hr> <input type="range" name="range" id="range"> <!-- css部分 --> :default{ background-color: green; } input:required{ background-color: yellow; } input:optional{ background-color: orange; } input:in-range{ background-color: lightgreen; } input:out-of-range{ background-color: red; color: white; } input:indeterminate{ outline: 1px solid blue; }
:link 錨點的樣式
:hover 鼠標浮動在元素上方時的樣式(任何元素)
active 鼠標點擊下去的樣式(任何元素)
:visited 鼠標點擊事後的顏色(任何元素)orm
<!--html部分--> <a href="www.baidu.com">百度</a> <!--css部分--> a:link{ text-decoration: none; font-weight: bold; color: black; } a:hover{ text-decoration: underline; color: blue; } a:active{ text-decoration: none; color: purple; } a:visited{ text-decoration: none; color: darkgray; }
:first-child 第一個元素樣式
:last-child 最後一個元素樣式
:nth-child(n) 第n個元素樣式(這個還能玩出花樣的)
:not(selector) 不符合selector選擇器的樣式htm
<!--html部分省略,一個10元素的ul,其中第四個li的name屬性爲except--> <!--css部分--> ul li{ list-style: none; background-color: skyblue; color: white; text-align: center; width: 100px; height: 20px; margin: 5px auto; float: left; } ul li:first-child{ color: blue; } ul li:last-child{ color: red; } ul li:nth-child(3){ color: darkgreen; } ul li:not([name="except"]){ font-style: italic; }
<!--css部分--> /*下面實現偶數部分樣式變化*/ ul li:nth-child(2n){ /*2n+1能夠表示奇數的*/ background-color: blue; }
<!--css部分--> /*下面實現連續部分樣式變化*/ ul li:nth-child(n+3):nth-child(-n+8){ background-color: blue; } /* :nth-child(n+3)表示第三個之後的元素 :nth-child(-n+8)表示第8個之前的元素 所以這裏選擇了選擇第三到第八的元素 */
:only-child 放在下一節和:only-of-type比較講解
此外CSS3中:first-of-type :last-of-type :nth-of-type(n) :nth-last-of-type(n)用法與上述類似,做用也一致,其中:nth-last-of-type(n)表示倒數第n個元素。type和child的兩種具備如下差異:
//僞代碼 <!-- html部分 --> <div> <div> <h3>div1中的惟一子元素h3</h3> <!-- 這個是h3:only-child --> </div> <div> <h3>div2中的第1個h3</h3> <h4>div2中的第1個h4</h4> <!-- 這個是h4:only-of-type --> <h3>div2中的第2個h3</h3> </div> <div> <h3>div3中的第1個h3</h3> <!-- 這個是h3:nth-of-type(1) 是 h3:nth-child(1) --> <h4>div3中的第1個h4</h4> <!-- 這個是h4:nth-of-type(1) 是 h4:nth-child(2) --> <h3>div3中的第2個h3</h3> <!-- 這個是h3:nth-of-type(2) 是 h3:nth-child(3) --> <h4>div3中的第2個h4</h4> <!-- 這個是h3:nth-of-type(2) 是 h4:nth-child(4) --> <h3>div3中的第3個h3</h3> <!-- 這個是h3:nth-of-type(3) 是 h3:nth-child(5) --> <h4>div3中的第3個h4</h4> <!-- 這個是h3:nth-of-type(4) 是 h4:nth-child(6) --> </div> <!-- css部分 --> h3:nth-of-type(2){ color: #00f; } h4:nth-child(4){ color: #ff0; } h4:only-of-type{ background-color: #ff0; } h3:only-child{ background-color: #f0f; }
上面例子中還有 :only-child和CSS3中的:only-of-type兩個僞類,表示單獨的元素,也就是先後沒有與之相同的元素。具體效果見下圖:
:target 選擇器可用於選取當前活動的目標元素(即url中的錨元素)。
下面用target作一個選項卡的樣式(點擊切換)
<!--html部分--> <div id="tab"> <nav class="title"> <a href="#a">標籤一</a> <a href="#b">標籤二</a> <a href="#c">標籤三</a> </nav> <ul class="content"> <li id="a">內容一</li> <li id="b">內容二</li> <li id="c">內容三</li> </ul> </div> <!--css部分--> #tab .title a{ float: left; text-decoration: none; width: 100px; height: 35px; line-height: 35px; text-align: center; border:1px solid black; } #tab .title a:nth-child(n+2){ border-left:none; } #tab .content{ clear:both; position:relative; } #tab .content li{ width:302px; height:300px; border:1px solid black; border-top: none; background-color: white; position:absolute; left:0; top:0; } #tab .content li:target{ z-index:1; }
這個是最值得一提的,在元素的先後添加內容,固然也能夠添加一個塊元素,這個塊變化就無窮了,下面舉幾個例子:
<!--html部分--> <div class="clr"> <div class="float">1</div> <div class="float">2</div> </div> <div class="float">3</div> <div class="float">4</div> <!--css部分--> *{ margin:0; padding:0; } .float{ width: 40px; height: 40px; background-color: blue; margin: 5px; float: left; color: yellow; } .clr:after{ content: ""; clear: both; overflow: hidden; height: 0; display: block; }
<!-- html部分 --> <h3>hello</h3> <h4>world!</h4> <h4>world!</h4> <h4>world!</h4> <h4>world!</h4> <h4>world!</h4> <h3>hello</h3> <h4>world!</h4> <h4>world!</h4> <h4>world!</h4> <h4>world!</h4> <h4>world!</h4> <!-- css部分 --> h3{ counter-increment: myNumH3; counter-reset: myNumH4; } h3:before{ content: '第'counter(myNumH3)'章 '; color: #08f; } h4{ counter-increment: myNumH4; margin-left: 20px; } h4:before{ content: '第'counter(myNumH3)'-'counter(myNumH4)'節 '; color: #00f; }
<!--html部分--> <div class="new"><img src="pic/test.jpg" /><span>new</span></div> <!--css部分--> .new,img{ width: 300px; height: 200px; } .new span{ position: relative; display: block; letter-spacing: 2px; font-size:20px; width:30px; height:20px; color: white; top: -190px; left: 262px; z-index:1; transform: rotate(45deg); } .new:after{ content:""; display:block; font-size:20px; width: 0; height: 0; border:solid 35px transparent; border-top-color: red; border-right-color: red; position:relative; top: -224px; left: 230px; }
<!--html部分--> <div class="enlarge button">按鈕</div> <!--css部分--> .button{ width:80px; height: 40px; border:2px solid dimgray; background-color: dodgerblue; color: #202020; text-align: center; line-height: 40px; font-size: 20px; margin:20px; } .enlarge:after{ content:""; display: block; height: 60px; width: 100px; position: relative; top: -50px; left: -10px; background-color: rgba(100, 100, 100, 0.4);/*用顏色表示一下區域,應該透明*/ }
<!--html部分--> <div class="cor_num button" data-num="8">按鈕</div> <!--css部分--> .cor_num:after{ content: attr(data-num); padding:0; line-height: 22px; position: relative; display: block; background-color: red; top: -50px; left: 68px; width: 24px; height: 24px; border-radius: 12px; color: white; font-size:14px; }
<!--html部分--> <div class="dialog">這是一個對話框</div> <!--css部分--> .dialog{ background-color: pink; border: 2px solid gray; text-align: center; line-height: 80px; width: 150px; height: 80px; margin-bottom: 40px; } .dialog:after{ content:""; display: block; background: inherit; border: inherit; border-top: 0; border-left: 0; position: relative; width:30px; height: 30px; top: -15px; left: 20px; transform: rotate(45deg); }
<!--html部分--> <div class="luck"><span>福</span></div> <!--css部分--> .luck{ float: left; width: 100px; height: 100px; margin:30px; margin-bottom: 45px; } .luck span{ color: gold; position: relative; font-size: 4em; width:70px; height: 70px; transform: rotate(180deg); display: block; top: -80px; left: 16px; } .luck:before{ content:""; display:block; width: 100px; height: 100px; background-color: red; transform: rotate(45deg); }
不足之處請多指點。