文转载自:https://blog.csdn.net/hongyu799/article/details/109114843
问题背景:
在进行页面开发时,经常会使用:before, :after伪元素创建一些小tips,但是在:before或:after的content属性使用中文的话,会导致某些浏览器上出现乱码。
解决方案:
避免在CSS的:before, :after中使用中文,如果一定要使用,可以使用中文对应的Unicode。可以使用使用站长工具,或者是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小时内与您取得联系。