這篇文章帶給大家的內容是關於CSS中偽類選擇器以及偽元素選擇器的介紹(附代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
一、連結偽類別
1、連結偽類別
/*链接伪类*/ 注意:link,:visited,:target是作用于链接元素的! :link 表示作为超链接,并指向一个未访问的地址的所有锚 :visited 表示作为超链接,并指向一个已访问的地址的所有锚 :target 代表一个特殊的元素,它的id是URI的片段标识符
2、程式碼實例:
01 _錨點偽類.html
<head> <meta charset="UTF-8"> <title></title> <!--:link:表示作为超链接,并指向一个未访问的地址的所有锚--> <style type="text/css"> a{ text-decoration: none; } a:link{ color: deeppink; } #test:link{ background: pink; } </style> </head> <body> <a href="#">点我点我点我</a> <p id="test">我是p啦</p> </body>
02_錨點偽類.html
<head> <meta charset="UTF-8"> <title></title> <!--:visited:表示作为超链接,并指向一个已访问的地址的所有锚--> <style type="text/css"> a{ text-decoration: none; } a:link{ color: black; } a:visited{ color: pink; } </style> </head> <body> <a href="#">点我点我点我</a> </body>
03_target.html
<head> <meta charset="UTF-8"> <title></title> <!--:target 代表一个特殊的元素,这个元素的id是URI的片段标识符。--> <style type="text/css"> *{ margin: 0; padding: 0; } p{ width: 300px; height: 200px; line-height: 200px; background: black; color: pink; text-align: center; display: none; } :target{ display: block; } </style> </head> <body> <a href="#p1">p1</a> <a href="#p2">p2</a> <a href="#p3">p3</a> <p id="p1"> p1 </p> <p id="p2"> p2 </p> <p id="p3"> p3 </p> </body>
二、動態偽類
1、動態偽類
/*动态伪类*/ 注意:hover,:active基本可以作用于所有的元素! :hover 表示悬浮到元素上 :active 表示匹配被用户激活的元素(点击按住时) 注意: 由于a标签的:link和:visited可以覆盖了所有a标签的状态,所以当:link,:visited,:hover,:active同时出现在a标签身上时 :link和:visited不能放在最后!!!
2、程式碼實例:
<head> <meta charset="UTF-8"> <title></title> <style type="text/css"> #test:hover{ color: pink; } #test:active{ color: red; } </style> </head> <body> <p id="test"> 我是test </p> </body>
三、隱私與:visited選擇器
# 1.隱私與:visited選擇器
/*隐私与:visited选择器*/只有下列的属性才能被应用到已访问链接 : color background-color border-color
四、表單相關偽類
1、表單相關偽類
/*表单相关伪类*/ :enabled 匹配可编辑的表单 :disable 匹配被禁用的表单 :checked 匹配被选中的表单 :focus 匹配获焦的表单
#2 、程式碼實例:
01_表單狀態.html
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> input { width: 100px; height: 30px; color: #000; } input:enabled { color: red; } input:disabled { color: blue; } </style> </head> <body> <input type="text" value="晓飞张" /> <input type="text" value="晓飞张" disabled="disabled" /> </body>
02_表單狀態.html
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> input:checked { width: 100px; height: 100px; } </style> </head> <body> <input type="checkbox" /> </body>
03_取得焦點.html
<head> <meta charset="UTF-8"> <title></title> <style type="text/css"> input:focus{ background: pink; } p:focus{ background: pink; } </style> </head> <body> <input type="text" value="" /> <p style="width: 200px;height: 200px;background: deeppink;" contenteditable="true" ></p> </body>
04 _模擬單選框.html
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> label { float: left; margin: 0 5px; overflow: hidden; position: relative; } label input { position: absolute; left: -50px; top: -50px; } span { float: left; width: 50px; height: 50px; border: 3px solid #000; } input:checked~span { background: red; } </style> </head> <body> <label> <input type="radio" name="tab" /> <span></span> </label> <label> <input type="radio" name="tab" /> <span></span> </label> <label> <input type="radio" name="tab" /> <span></span> </label> </body>
四、結構性偽類
#1、結構性偽類
/*结构性伪类*/index的值从1开始计数!!!! index可以为变量n(只能是n) index可以为even odd #wrap ele:nth-child(index) 表示匹配#wrap中第index的子元素 这个子元素必须是ele #wrap ele:nth-of-type(index) 表示匹配#wrap中第index的ele子元素 除此之外:nth-child和:nth-of-type有一个很重要的区别!! nth-of-type以元素为中心!!! :nth-child(index)系列 :first-child :last-child :nth-last-child(index) :only-child (相当于:first-child:last-child 或者 :nth-child(1):nth-last-child(1)) :nth-of-type(index)系列 :first-of-type :last-of-type :nth-last-type(index) :only-of-type (相当于:first-of-type:last-of-type 或者 :nth-of-type(1):nth-last-of-type(1)) :not :empty(内容必须是空的,有空格都不行,有attr没关系)
##2、程式碼實例:
<head> <meta charset="UTF-8"> <title></title> <style type="text/css"> /*子元素的标签应该要统一*/ /*ul .item:nth-child(3){ border: 1px solid; }*/ ul .item:nth-of-type(3){ border: 1px solid; } /*ul p:nth-of-type(3){ border: 1px solid; } ul p:nth-of-type(3){ border: 1px solid; } ul li:nth-of-type(3){ border: 1px solid; }*/ </style> </head> <body> <ul> <p class="item">p1</p> <p class="item">p2</p> <p class="item">p3</p> <li class="item">1</li> <li class="item">2</li> <li class="item">3</li> <li class="item">4</li> <li class="item">5</li> <p class="item">p1</p> <p class="item">p2</p> <p class="item">p3</p> <li class="item">6</li> <li class="item">7</li> <li class="item">8</li> <li class="item">9</li> </ul> </body>04_not.html
<head> <meta charset="UTF-8"> <title>not</title> <style type="text/css"> * { margin: 0; padding: 0; border: none; } a { text-decoration: none; color: #333; font-size: 14px; display: block; float: left; width: 100px; height: 30px; } p { width: 800px; margin: 0 auto; } p>a:not(:last-of-type) { border-right: 1px solid red; } </style> </head> <body> <p> <a href="#">first</a> <a href="#">second</a> <a href="#">third</a> <a href="#">fourth</a> <a href="#">fifth</a> </p> </body>#05_empty.html
<head> <meta charset="UTF-8"> <title>empty</title> <style type="text/css"> p { height: 200px; background: #abcdef; } p:empty { background: #f00; } </style> </head> <body> <p></p> <p>Second</p> <p></p> <p>Third</p> </body>五、偽元素
#1、偽元素
/*伪元素*/ ::after ::before ::firstLetter ::firstLine ::selection
2、程式碼實例: after.html
<head> <meta charset="UTF-8"> <title>after</title> <style type="text/css"> p { width: 300px; height: 100px; border: 1px solid #000; } p::after { content: "我在内容的后面"; } </style> </head> <body> <p>伪元素</p> </body>before.html
<head> <meta charset="UTF-8"> <title>before</title> <style type="text/css"> p { width: 300px; height: 100px; border: 1px solid #000; } p::before { content: "我在内容的前面"; } </style> </head> <body> <p>伪元素</p> </body>firstLetter.html
<head> <meta charset="UTF-8"> <title>First-Letter</title> <style type="text/css"> p { width: 500px; margin: 0 auto; font-size: 12px; } p::first-letter { color: #f00; font-size: 24px; font-weight: bold; } </style> </head> <body> <p>sssss</p> </body>firstLine.html
<head> <meta charset="UTF-8"> <title>First-Line</title> <style type="text/css"> p { width: 500px; margin: 0 auto; } p::first-line { color: #f00; font-weight: bold; } </style> </head> <body> <p> sssss<br> sssss <br> sssss <br> </p> </body>selection.html
<head> <meta charset="UTF-8"> <title>Selection</title> <style type="text/css"> p::selection { background: red; color: pink; } </style> </head> <body> <p>SelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelection</p> </body>相關推薦:##CSS中一些利用偽類別、偽元素和相鄰元素選擇器的技巧CSS中的選擇器種類總結及效率比較範例
以上是CSS中的偽類選擇器和偽元素選擇器的程式碼分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

具有CSS的自定義光標很棒,但是我們可以將JavaScript提升到一個新的水平。使用JavaScript,我們可以在光標狀態之間過渡,將動態文本放置在光標中,應用複雜的動畫並應用過濾器。

互動CSS動畫和元素相互啟動的元素在2025年似乎更合理。雖然不需要在CSS中實施乒乓球,但CSS的靈活性和力量的增加,可以懷疑Lee&Aver Lee有一天會成為一種

有關利用CSS背景濾波器屬性來樣式用戶界面的提示和技巧。您將學習如何在多個元素之間進行背景過濾器,並將它們與其他CSS圖形效果集成在一起以創建精心設計的設計。

好吧,事實證明,SVG的內置動畫功能從未按計劃進行棄用。當然,CSS和JavaScript具有承載負載的能力,但是很高興知道Smil並沒有像以前那樣死在水中

是的,讓#039;跳上文字包裝:Safari Technology Preview In Pretty Landing!但是請注意,它與在鉻瀏覽器中的工作方式不同。

此CSS-tricks更新了,重點介紹了年鑑,最近的播客出現,新的CSS計數器指南以及增加了幾位新作者,這些新作者貢獻了有價值的內容。

在大多數情況下,人們展示了@Apply的@Apply功能,其中包括Tailwind的單個property實用程序之一(會改變單個CSS聲明)。當以這種方式展示時,@Apply聽起來似乎很有希望。如此明顯


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),