天天看點

CSS3的content屬性詳解

CSS中主要的僞元素有四個:before/after/first-letter/first-line,在before/after僞元素選擇器中,有一個content屬性,能夠實作頁面中的内容插入。

插入純文字

content:”插入的文章”,或者content:none不插入内容

html:

<h1>這是h1</h1>
<h2>這是h2</h2>
           

css

h1::after{
    content:"h1後插入内容"
}
h2::after{
    content:none
}
           

運作結果:https://jsfiddle.net/dwqs/Lmm1r08x/

嵌入文字元号

可以使用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;
}
           

運作結果:https://jsfiddle.net/dwqs/p8e3qvv4/

插入圖檔

content屬性也可以直接在元素前/後插入圖檔

html:

css:

h3::after{
    content:url(http://ido321.qiniudn.com/wp-content/themes/yusi1./img/new.gif)
}
           

運作結果:https://jsfiddle.net/dwqs/c6qk6pkv/

插入元素的屬性值

content屬性可以直接利用attr擷取元素的屬性,将其插入到對應位置。

html:

css:

a:after{
    content:attr(href);
}
           

運作結果:https://jsfiddle.net/dwqs/m220nzan/

插入項目編号

利用content的counter屬性針對多個項目追加連續編号.

html:

<h1>大标題</h1>
<p>文字</p>
<h1>大标題</h1>
<p>文字</p>
<h1>大标題</h1>
<p>文字</p>
<h1>大标題</h1>
<p>文字</p>
           

css:

h1:before{
    content:counter(my)'.';
}
h1{
    counter-increment:my;
}
           

運作結果:https://jsfiddle.net/dwqs/2ueLg3uj/

項目編号修飾

預設插入的項目編号是數字型的,1,2,3.。。。自動遞增,也能給項目編号追加文字和樣式,依舊利用上面的html,css修改如下:

h1:before{
    content:'第'counter(my)'章';
    color:red;
    font-size:px;
}
h1{
    counter-increment:my;
}
           

運作結果:https://jsfiddle.net/dwqs/17hqznca/

指定編号種類

利用content(計數器名,編号種類)格式的文法指定編号種類,編号種類的參考可以依據ul的list-style-type屬性值。利用上述的html,css修改如下:

h1:before{
    content:counter(my,upper-alpha);
    color:red;
    font-size:px;
}
h1{
    counter-increment:my;
}
           

運作結果:https://jsfiddle.net/dwqs/4nsrtxup/

編号嵌套

大編号中嵌套中編号,中編号中嵌套小編号。

html:

<h1>大标題</h1>
<p>文字1</p>
<p>文字2</p>
<p>文字3</p>
<h1>大标題</h1>
<p>文字1</p>
<p>文字2</p>
<p>文字3</p>
<h1>大标題</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:px;
}
p{
    counter-increment:p;
}
           

運作結果:https://jsfiddle.net/dwqs/2k5qbz51/

在示例的輸出中可以發現,p的編号是連續的。如果對于每一個h1後的三個p重新編号的話,可以使用counter-reset屬性重置,修改上述h1的css:

h1{
    counter-increment:h;
    counter-reset:p;
}
           

這樣,編号就重置了,看看結果:https://jsfiddle.net/dwqs/hfutu4Lq/

還可以實作更複雜的嵌套,例如三層嵌套。

html:

<h1>大标題</h1>
<h2>中标題</h2>
<h3>小标題</h3>
<h3>小标題</h3>
<h2>中标題</h2>
<h3>小标題</h3>
<h3>小标題</h3>
<h1>大标題</h1>
<h2>中标題</h2>
<h3>小标題</h3>
<h3>小标題</h3>
<h2>中标題</h2>
<h3>小标題</h3>
<h3>小标題</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:px;
}
h3::before{
    content:counter(h1) '-' counter(h2) '-' counter(h3);
}
h3{
    counter-increment:h3;
    margin-left:px;
}
           

運作結果:https://jsfiddle.net/dwqs/wuuckquy/

張大大有一篇利用counter實作計數的文章:小tip:CSS計數器+僞類實作數值動态計算與呈現

原文:http://www.ido321.com/1555.html

上一篇: 搜尋題庫