CSS中主要的偽元素有四個:before/after/first-letter/first-line,在before/after偽元素選擇器中,有一個content屬性,能夠實現頁面中的內容插入。
插入純文字
content : ”插入的文章”,或者content:none 不插入內容
#html <h1 id="这是h">这是h1</h1> <h2 id="这是h">这是h2</h2> #css h1::after{ content:"h1后插入内容" } h2::after{ content:none }
嵌入文字符號
可以使用content屬性的open-quote 屬性值和close-quote 屬性值在兩邊加入諸如括號、單引號、雙引號之類的嵌套文字符號。 open-quote 用於新增開始的文字符號,close-quote 用於新增結束的文字符號。修改上述的css:
h1{ quotes:"(" ")"; /*利用元素的quotes属性指定文字符号*/ } h1::before{ content:open-quote; } h1::after{ content:close-quote; } h2{ quotes:"\"" "\""; /*添加双引号要转义*/ } h2::before{ content:open-quote; } h2::after{ content:close-quote; }
插入圖片
content屬性也可以直接在元素前/後插入圖片
#html <h3 id="这是h">这是h3</h3> #css h3::after{ content:url(图片路径) }
插入元素的屬性值
content屬性可以直接利用attr獲取元素的屬性,將其插入到對應值
content屬性可以直接利用attr獲取元素的屬性,將其插入到對應物位置。
#html <a href="http://www.php.cn">这是链接</a> #css a:after{ content:attr(href); }
插入項目編號
利用content的counter屬性針對多個項目追加連續編號.
#html <h1 id="大标题">大标题</h1> <p>文字</p> <h1 id="大标题">大标题</h1> <p>文字</p> <h1 id="大标题">大标题</h1> <p>文字</p> <h1 id="大标题">大标题</h1> <p>文字</p> #css h1:before{ content:counter(my)'.'; } h1{ counter-increment:my; }
項目編號修飾
預設插入的項目編號是數字型的,1,2,3.。 。 。自動遞增,也能為項目編號追加文字和樣式,依舊利用上面的html,css修改如下:
h1:before{ content:'第'counter(my)'章'; color:red; font-size:42px; } h1{ counter-increment:my; }
指定編號種類
利用content(計數器名,編號種類)格式的語法指定編號種類,編號種類的參考可以依據ul的list-style-type屬性值。利用上述的html,css修改如下:
h1:before{ content:counter(my,upper-alpha); color:red; font-size:42px; } h1{ counter-increment:my; }
編號嵌套
大編號中嵌套中編號,中編號中嵌套小編號。
#html <h1 id="大标题">大标题</h1> <p>文字1</p> <p>文字2</p> <p>文字3</p> <h1 id="大标题">大标题</h1> <p>文字1</p> <p>文字2</p> <p>文字3</p> <h1 id="大标题">大标题</h1> <p>文字1</p> <p>文字2</p> <p>文字3</p> #css h1::before{ content:counter(h)'.'; } h1{ counter-increment:h; } p::before{ content:counter(p)'.'; margin-left:40px; } p{ counter-increment:p; }
在範例的輸出中可以發現,p的編號是連續的。如果每一個h1後的三個p重新編號的話,可以使用counter-reset屬性重置,修改上述h1的css:
h1{ counter-increment:h; counter-reset:p; }
還可以實現更複雜的嵌套,例如三層嵌套。
#html <h1 id="大标题">大标题</h1> <h2 id="中标题">中标题</h2> <h3 id="小标题">小标题</h3> <h3 id="小标题">小标题</h3> <h2 id="中标题">中标题</h2> <h3 id="小标题">小标题</h3> <h3 id="小标题">小标题</h3> <h1 id="大标题">大标题</h1> <h2 id="中标题">中标题</h2> <h3 id="小标题">小标题</h3> <h3 id="小标题">小标题</h3> <h2 id="中标题">中标题</h2> <h3 id="小标题">小标题</h3> <h3 id="小标题">小标题</h3> #css h1::before{ content:counter(h1)'.'; } h1{ counter-increment:h1; counter-reset:h2; } h2::before{ content:counter(h1) '-' counter(h2); } h2{ counter-increment:h2; counter-reset:h3; margin-left:40px; } h3::before{ content:counter(h1) '-' counter(h2) '-' counter(h3); } h3{ counter-increment:h3; margin-left:80px; }
下面給出完整DEMO
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS content</title> <style> .string p:after { margin-left: -16px; background: #fff; content: "支持"; color: #f00; } .attr p:after { content: attr(title); } .url p:before { content: url(https://img.php.cn/upload/article/000/000/009/587d87ecca52d563.jpg); display: block; } .test ol { margin: 16px 0; padding: 0; list-style: none; } .counter1 li { counter-increment: testname; } .counter1 li:before { content: counter(testname)":"; color: #f00; font-family: georgia,serif,sans-serif; } .counter2 li { counter-increment: testname2; } .counter2 li:before { content: counter(testname2,lower-roman)":"; color: #f00; font-family: georgia,serif,sans-serif; } .counter3 ol ol { margin: 0 0 0 28px; } .counter3 li { padding: 2px 0; counter-increment: testname3; } .counter3 li:before { content: counter(testname3,float)":"; color: #f00; font-family: georgia,serif,sans-serif; } .counter3 li li { counter-increment: testname4; } .counter3 li li:before { content: counter(testname3,decimal)"."counter(testname4,decimal)":"; } .counter3 li li li { counter-increment: testname5; } .counter3 li li li:before { content: counter(testname3,decimal)"."counter(testname4,decimal)"."counter(testname5,decimal)":"; } </style> </head> <body> <ul> <li> <strong>string:</strong> <p>你的浏览器是否支持content属性:否</p> </li> <li> <strong>attr:</strong> <p title="如果你看到我则说明你目前使用的浏览器支持content属性"></p> </li> <li> <strong>url():</strong> <p>如果你看到图片则说明你目前使用的浏览器支持content属性</p> </li> <li> <strong>counter(name):</strong> <ol> <li>列表项</li> <li>列表项</li> <li>列表项</li> </ol> </li> <li> <strong>counter(name,list-style-type):</strong> <ol> <li>列表项</li> <li>列表项</li> <li>列表项</li> </ol> </li> <li> <strong>counter(name)拓展应用:</strong> <ol> <li>列表项 <ol> <li>列表项 <ol> <li>列表项</li> <li>列表项</li> </ol> </li> <li>列表项</li> </ol> </li> <li>列表项 <ol> <li>列表项</li> <li>列表项</li> </ol> </li> <li>列表项 <ol> <li>列表项</li> <li>列表项</li> </ol> </li> </ol> </li> </ul> </body> </html>🎜🎜

Wufoo一直在集成方面非常出色。他們與特定應用程序(例如廣告系列顯示器,MailChimp和Typekit)進行集成,但他們也


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。