文轉載自:https://blog.csdn.net/hongyu799/article/details/109114843
問題背景:
在進行頁面開發時,經常會使用:before, :after偽元素創建一些小tips,但是在:before或:after的content屬性使用中文的話,會導致某些瀏覽器上出現亂碼。
解決方案:
避免在CSS的:before, :after中使用中文,如果一定要使用,可以使用中文對應的Unicode??梢允褂檬褂谜鹃L工具,或者是JavaScript的原生方法escape將中文轉為Unicode。
需要注意的是Unicode在CSS中的書寫方式,例如“小時”對應的Unicode是'\u5c0f\u65f6',而在CSS中要寫成(去掉里面的u,切記切記)
ss3中出現了 ":before",":after"偽類,
你可以這樣寫:
h1:after{
content:'h1后插入的文本';
...}
這兩個選擇器的作用以及效果,這里就不在介紹了;主要說一下上面提到的一個css屬性【content】用來和:after及:before偽元素一起使用,在對象前或后顯示內容。
content的取值:
normal:默認值。表現與none值相同
none:不生成任何值。<attr>:插入標簽屬性值<url>:使用指定的絕對或相對地址插入一個外部資源(圖像,聲頻,視頻或瀏覽器支持的其他任何資源)<string>:插入字符串
counter(name):使用已命名的計數器
counter(name,list-style-type):使用已命名的計數器并遵從指定的list-style-type屬性
counters(name,string):使用所有已命名的計數器
counters(name,string,list-style-type):使用所有已命名的計數器并遵從指定的list-style-type屬性
no-close-quote:并不插入quotes屬性的后標記。但增加其嵌套級別
no-open-quote:并不插入quotes屬性的前標記。但減少其嵌套級別
close-quote:插入quotes屬性的后標記
open-quote:插入quotes屬性的前標記
這里比較不好理解的取值就是:counter(name)這些;
下面主要總結一下這塊,最后會給出各個取值的demo,
比如我有如下html結構:
<ul>
<li>這個是有序列表</li>
<li>這個是有序列表</li>
<li>這個是有序列表</li>
<li>這個是有序列表</li>
<li>這個是有序列表</li></ul>
我要在每個li的后面加上當前li【index】值:
ul li{
counter-increment:index;
}
ul li:after{
content:'統計:'counter(index);
display:block;
line-height:35px;
}
解釋:
count(name)這里的name,必須要提前指定好,否則所有的值都將是0;
count(name,list-style-type)這里的list-style-type就是css中list-style-type屬性的取值;
下面給出完整DEMO
<!DOCTYPE html><html><head><meta charset="utf-8"><title>CSS content</title><meta name="author" content="phpstudy.net"><meta name="copyright" content="www.phpstudy.net"><style>
.string p:after {
margin-left: -16px;
background: #fff;
content: "支持";
color: #f00;}
.attr p:after {
content: attr(title);}
.url p:before {
content: url(https://pic.cnblogs.com/avatar/779447/20160817152433.png);
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>
SS中的偽類真是個神奇的東西,有時要實現某些布局的時候菜鳥都是div套div,實在不行上flex,而高手一般都是用最少的元素來實現效果,比如巧妙的運用偽類來達到目的的同時減少元素節點,代碼清爽了很多不用再寫一坨div。
這不,最近學習了幾招偽類的使用,特此記錄下。
flex布局中,最后一行個數不滿的情況:
.container {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.list {
width: 24%; height: 100px;
background-color: skyblue;
margin-top: 15px;
}
最后一行不足4個:
<div class="container">
<div class="list"></div>
<div class="list"></div>
<div class="list"></div>
<div class="list"></div>
<div class="list"></div>
<div class="list"></div>
<div class="list"></div>
</div>
解決思路有很多,這里我只說一種方式:使用偽類動態計算最后一個元素的margin。比如每行有4個元素,最后一行只有3個,就計算最后一個的margin-right為“元素寬度+間隔大小”,若只有2個,則margin-right為”2個元素寬度+2個間隔大小”,以此類推,代碼如下:
.container {
display: flex;
/* 兩端對齊 */
justify-content: space-between;
flex-wrap: wrap;
}
.list {
width: 24%; height: 100px;
background-color: skyblue;
margin-top: 15px;
}
/* 如果最后一行是3個元素 */
.list:last-child:nth-child(4n - 1) {
margin-right: calc(24% + 4% / 3);
}
/* 如果最后一行是2個元素 */
.list:last-child:nth-child(4n - 2) {
margin-right: calc(48% + 8% / 3);
}
效果:
黃色部分為動態計算的margin,上面代碼最重要的就是.list:last-child:nth-child(4n - 1),騷操作,頭一次見,稍微解釋下:
元素的展開和收起也是會經常遇到的,默認收起狀態,點擊后就會展開,類似于點擊彈出下拉菜單,只涉及到兩種狀態,也可以結合偽類實現:
.btn::after {
content: '展開'
}
#exp {
display: none;
}
#exp:checked+.btn::after {
content: '收起'
}
html如下:
<div class="wrap">
<input type="checkbox" id="exp">
<label class="btn" for="exp"></label>
</div>
效果:
這里巧妙地借助:checked偽類實現狀態的切換,換做javascript就要寫好幾行代碼。
.arrow-button {
position: relative;
width: 180px;
height: 64px;
background: #f49714;
}
.arrow-button::after {
content: "";
position: absolute;
width: 32px;
height: 64px;
top: 0;
right: -32px;
background:
linear-gradient(-45deg, transparent 0, transparent 22px, #f49714 22px, #f49714 100%),
linear-gradient(-135deg, transparent 0, transparent 22px, #f49714 22px, #f49714 100%);
background-size: 32px 32px;
background-repeat: no-repeat;
background-position: 0 bottom, 0 top;
}
效果:
這個就很極客了,先看看效果:
這個是先效果的背后和第一個例子一樣使用了偽類級聯,同一個選擇器上使用多個偽類,核心代碼如下:
.box span {
font-size: 40px;
}
.box span:first-child:nth-last-child(n+13),
.box span:first-child:nth-last-child(n+13) ~ span {
font-size: 30px;
}
.box span:first-child:nth-last-child(n+17),
.box span:first-child:nth-last-child(n+17) ~ span {
font-size: 20px;
}
.box span:first-child:nth-last-child(n+25),
.box span:first-child:nth-last-child(n+25) ~ span {
font-size: 14px;
}
通過使用偽類級聯能控制不同字符個數的樣式。
能實現的效果還有很多,很多我也不會。。。
就我自己在開發時,很容易就把偽類這個東西拋之腦后了,下意識地習慣直接div一把梭,實現不了就用javascript,因為這種下意思的習慣成本很低,不要多加思考。其實有時候仔細想想,很多情況真的沒必要“殺雞用牛刀”,習慣也是有代價的,下意識的使用方式看起來似乎成本很低,但代碼因此變得復雜,增加了后期的維護成本和理解成本。不光是我們在編碼的時候,生活中也是如此,遇到問題不妨多思考,不要急于作出反饋。
http://www.zhangxinxu.com/wordpress/?p=1514
https://www.zhangxinxu.com/wordpress/?p=8540
https://segmentfault.com/a/1190000040030723
*請認真填寫需求信息,我們會在24小時內與您取得聯系。