在前端開發(fā)中,偽類是一種讓你可以選擇元素的某個(gè)狀態(tài)或位置的 CSS 選擇器。其中,:before
和 :after
偽類允許你在一個(gè)元素之前或之后插入內(nèi)容。
:before
和 :after
偽類創(chuàng)建的元素是不在 HTML 文檔中的,它們是通過 CSS 生成的??梢杂盟鼈儊?lái)在一個(gè)元素的前面或后面插入一些內(nèi)容,這些內(nèi)容可以是文本、圖像或者其他任何可以用 CSS 生成的內(nèi)容。
before/after
偽類相當(dāng)于在元素內(nèi)部插入兩個(gè)額外的標(biāo)簽,其最適合也是最推薦的應(yīng)用就是圖形生成。在一些精致的UI實(shí)現(xiàn)上,可以簡(jiǎn)化HTML代碼,提高可讀性和可維護(hù)性。
總之,before
和 after
偽類允許你在一個(gè)元素前后插入內(nèi)容,這些內(nèi)容可以是文本、圖像或其他任何可以用 CSS 生成的內(nèi)容。
效果使用:
?
像這種小圖標(biāo)大多使用before,after來(lái)實(shí)現(xiàn),不僅簡(jiǎn)單還方便。
1.基本用法
:before和:after的作用就是在指定的元素內(nèi)容(而不是元素本身)之前或者之后插入一個(gè)包含content屬性指定內(nèi)容的行內(nèi)元素,最基本的用法如下:
#example:before {
content: "#";
color: red;
}
#example:after {
content: "$";
color: red;
}
這兩個(gè)偽類都屬于內(nèi)聯(lián)元素,但是用display:block;屬性可以將其轉(zhuǎn)換成塊狀元素,比較常見的用法就是樣式的一些實(shí)現(xiàn),還有就是清除浮動(dòng)的效果。。
2.樣式修改
代碼如下所示:
<div class="quote">
<span>打老虎</span>
</div>
.quote:before,.quote:after{//用這兩個(gè)偽類實(shí)現(xiàn)樣式渲染
content:"";
display:inline-block;
width:5%;
margin:5px 1%;
border-bottom:1px solid blue;
}
3.清除浮動(dòng)
代碼如下所示:
<div class="parent">
<div class="son1"></div>
<div class="son2"></div>
</div>
<div class="parent2">parent2</div>
//css代碼
.son1{
width:300px;
height:200px;
background-color: lightgray;
float:left;
}
.son2{
width:300px;
height:100px;
background-color: lightblue;
float:left;
}
.parent2{
width:400px;
height: 400px;
background-color:blue;
color:#fff;
text-align:center;
line-height:400px;
font-size:30px;
}
如果在上面代碼中加上這段代碼用來(lái)清除浮動(dòng)則會(huì)達(dá)到不一樣的效果:
.parent:after{
content:"";
display:block;//設(shè)為塊狀元素
clear:both; //用這個(gè)屬性來(lái)清除浮動(dòng)
}
::before和::after下特有的content,用于在css渲染中向元素邏輯上的頭部或尾部添加內(nèi)容。
這些添加不會(huì)出現(xiàn)在DOM中,不會(huì)改變文檔內(nèi)容,不可復(fù)制,僅僅是在css渲染層加入。
所以不要用:before或:after展示有實(shí)際意義的內(nèi)容,盡量使用它們顯示修飾性內(nèi)容,例如圖標(biāo)。
注意:在使用before和after時(shí)content必不可少。
注意:在使用before和after時(shí)content必不可少。
注意:在使用before和after時(shí)content必不可少。
4.content屬性
::before和::after必須配合content屬性來(lái)使用,content用來(lái)定義插入的內(nèi)容,content必須有值,至少是空。默認(rèn)情況下,偽類元素的display是默認(rèn)值inline,可以通過設(shè)置display:block來(lái)改變其顯示。
content可取以下值。
1、string
使用引號(hào)包一段字符串,將會(huì)向元素內(nèi)容中添加字符串。如:a:after{content:""}
<!DOCTYPE html>
<meta charset="utf-8" />
<style type="text/css">
p::before{
content: "《";
color: blue;
}
p::after{
content: "》";
color: blue;
}
</style>
<p>平凡的世界</p>
2、attr()
通過attr()調(diào)用當(dāng)前元素的屬性,比如將圖片alt提示文字或者鏈接的href地址顯示出來(lái)。
<style type="text/css">
a::after{
content: "(" attr(href) ")";
}
</style>
<a rel="external nofollow" >starof</a>
3、url()/uri()
用于引用媒體文件。
舉例:“百度”前面給出一張圖片,后面給出href屬性。
<style>
a::before{
content: url("https://www.baidu.com/img/baidu_jgylogo3.gif");
}
a::after{
content:"("attr(href)")";
}
a{
text-decoration: none;
}
</style>
---------------------------
<body>
<a rel="external nofollow" >百度</a>
</body>
4、counter()
調(diào)用計(jì)數(shù)器,可以不使用列表元素實(shí)現(xiàn)序號(hào)功能。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-627193.html
配合counter-increment和counter-reset屬性使用:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-627193.html
h2:before { counter-increment: chapter; content: "Chapter " counter(chapter) ". " }
<style>
body{
counter-reset: section;
}
h1{
counter-reset: subsection;
}
h1:before{
counter-increment:section;
content:counter(section) "、";
}
h2:before{
counter-increment:subsection;
content: counter(section) "." counter(subsection) "、";
}
</style>
------------------------------------------------
<body>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>
<h1>XML tutorials</h1>
<h2>XML</h2>
<h2>XSL</h2>
</body>
到了這里,關(guān)于JavaScript前端中的偽類元素before和after使用詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!