如何在css中使用content屬性?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),桂平企業(yè)網(wǎng)站建設(shè),桂平品牌網(wǎng)站建設(shè),網(wǎng)站定制,桂平網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,桂平網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
content屬性一般用于::before、::after偽元素中,用于呈現(xiàn)偽元素的內(nèi)容。平時(shí)content屬性值我們用的最多的就是給個(gè)純字符,其實(shí)它還有很多值可供選擇。
1、插入純字符
<style> *{margin: 0;padding: 0;box-sizing: border-box;} li{list-style: none;} .content{ position: relative;padding: 10px; border: 1px solid #666;margin: 10px; } .content.only-text::before{ content: '插入純字符'; } </style> <body> <h2>1、插入純字符</h2> <div class="content only-text"></div> </body>
2、插入圖片
<style> *{margin: 0;padding: 0;box-sizing: border-box;} li{list-style: none;} .content{ position: relative;padding: 10px; border: 1px solid #666;margin: 10px; } .content.fill-image::before{ content: url('https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_86d58ae1.png'); } </style> <body> <h2>2、插入圖片</h2> <div class="content fill-image"></div> </body>
3、插入元素屬性
<style> *{margin: 0;padding: 0;box-sizing: border-box;} li{list-style: none;} .content{ position: relative;padding: 10px; border: 1px solid #666;margin: 10px; } .content.fill-dom-attr::before{ content: attr(data-title); } </style> <body> <h2>3、插入元素屬性</h2> <div class="content fill-dom-attr" data-title="我是.fill-dom-attr元素的 data-title 屬性值"></div> </body>
4、插入當(dāng)前元素編號(即當(dāng)前元素索引)
這個(gè)特性可用于活動(dòng)頁面的規(guī)則介紹。
<style> *{margin: 0;padding: 0;box-sizing: border-box;} li{list-style: none;} .content{ position: relative;padding: 10px; border: 1px solid #666;margin: 10px; } .fill-dom-index li{ position: relative; /* 給計(jì)數(shù)器加器起個(gè)名字,它只會累加 li 標(biāo)簽的索引,li元素中間的div并不會理會 */ counter-increment: my; } .fill-dom-index li div::before{ /* 使用指定名字的計(jì)算器 */ content: counter(my)'- '; color: #f00; font-weight: 600; } </style> <body> <h2>4、插入當(dāng)前元素編號(即當(dāng)前元素索引)</h2> <div class="content fill-dom-index"> <ul> <li><div>我是第1個(gè)li標(biāo)簽</div></li> <div>我是li標(biāo)簽中的第1個(gè)div標(biāo)簽</div> <li><div>我是第2個(gè)li標(biāo)簽</div></li> <li><div>我是第3個(gè)li標(biāo)簽</div></li> <div>我是li標(biāo)簽中的第2個(gè)div標(biāo)簽</div> <li><div>我是第4個(gè)li標(biāo)簽</div></li> <li><div>我是第5個(gè)li標(biāo)簽</div></li> </ul> </div> </body>
5、插入當(dāng)前元素編號(指定種類)
<style> *{margin: 0;padding: 0;box-sizing: border-box;} li{list-style: none;} .content{ position: relative;padding: 10px; border: 1px solid #666;margin: 10px; } .fill-dom-index2 li{ position: relative; counter-increment: my2; } .fill-dom-index2 li div::before{ /* 第二個(gè)參數(shù)為list-style-type,可用值見: http://www.w3school.com.cn/cssref/pr_list-style-type.asp*/ content: counter(my2,lower-latin)'- '; color: #f00; font-weight: 600; } </style> <body> <h2>5、插入當(dāng)前元素編號(指定種類)</h2> <div class="content fill-dom-index2"> <ul> <li><div>我是第1個(gè)li標(biāo)簽</div></li> <div>我是li標(biāo)簽中的第1個(gè)div標(biāo)簽</div> <li><div>我是第2個(gè)li標(biāo)簽</div></li> <li><div>我是第3個(gè)li標(biāo)簽</div></li> <div>我是li標(biāo)簽中的第2個(gè)div標(biāo)簽</div> <li><div>我是第4個(gè)li標(biāo)簽</div></li> <li><div>我是第5個(gè)li標(biāo)簽</div></li> </ul> </div> </body>
css的選擇器可以分為三大類,即id選擇器、class選擇器、標(biāo)簽選擇器。它們之間可以有多種組合,有后代選擇器、子選擇器、偽類選擇器、通用選擇器、群組選擇器等等
看完上述內(nèi)容,你們掌握如何在css中使用content屬性的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站欄目:如何在css中使用content屬性
當(dāng)前URL:http://chinadenli.net/article38/iegssp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、靜態(tài)網(wǎng)站、網(wǎng)站導(dǎo)航、企業(yè)建站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)