今天我们来回顾一下后端CSS伪元素的知识,以及如何利用CSS伪元素来减轻JavaScript的压力,创建一些富有想象力的图形。
前知识伪元素
伪元素是附加到选择器末尾的关键字,允许您设置所选元素的特定部分的样式。 伪元素主要包括:
我个人认为伪元素可以理解为元素的装饰,可以给元素带来额外的样式,属于额外的文档结构。
伪类
用于表示 CSS 中难以轻松或可靠测量的元素的状态或属性。 例如a标签的hover表示键盘的样式,visited表示访问链接的样式,常用于描述元素的状态。 变化的风格和伪类别主要包括:
借助CSS伪类和伪元素,我们可以实现许多强大的视觉效果。 这里我主要介绍一下伪元素。 如果你对伪类或者其他CSS相关的知识感兴趣,可以看看我之前的CSS文章,内容很全面。
文本
我们首先看一下我们使用纯CSS实现的图标库:
当然,如果我们知道了制作上述图标的css原理,那么我们就可以实现日益丰富、复杂的图形,甚至可以构建自己的图标库。 接下来我将介绍一下上述图标的实现形式和技巧,并给出一些源代码,以方便大家阅读。
原则
我们上面实现的CSS图标是基于伪元素的。 我们可以使用伪元素的 ::before、::after 和 content 属性为元素添加额外的视觉效果。 上面我们也介绍了伪元素的概念和类型。 接下来,让我们一起实现吧~
实现箭头
虽然思路是利用元素的::before伪元素画一个三角形,但是用css将span样式设置为一条线,并进行布局定位:
less
{
position: relative;
display: inline-block;
0; :
#ccc; :
{
width: 16px;
height: 1px;
}
:before { :
content: '';
position: absolute;
top: -4px;
right: -8px;
border: 4px solid transparent;
4px solid #ccc; :
}
}
html
"arrow arrow-hor right"> =
这实现了一个指向右侧的箭头。 我们还可以用类似的方式实现向左箭头、向上和向下箭头。 要实现单向箭头,我们只需要添加一个 ::after 伪类并将其放置在适当的位置即可。
实现搜索图标
其实只需要一个圆和一条线就可以实现搜索图标,然后我们就用transform:ratate来实现角度倾斜。 具体实现如下:
less
{
position: relative;
display: inline-block;
width: 12px;
height: 12px;
50%; :
border: 1px solid #ccc;
center; :
transform: rotate(-45deg);
:after { :
content: '';
display: inline-block;
width: 1px;
height: 4px;
#ccc; :
}
}
html
"search"> =
实现头像
为了实现头像css微元素,我们需要使用before和after伪元素分别制作角色的脸部和身体。 对于主体,我们将用 css 绘制一个椭圆:
less
{
position: relative;
display: inline-flex;
width: 60px;
height: 60px;
0; :
center; :
50%; :
#06c; :
transform: rotate(-90deg);
:before { :
content: '';
display: inline-block;
width: 28px;
height: 40px;
-3px; :
50% 50%; :
0; :
#fff; :
}
:after { :
content: '';
display: inline-block;
width: 20px;
height: 20px;
0; :
50%; :
#fff; :
}
}
html
"search"> =
实现地点图标
位置图标由一个圆形和一个三角形组成。 如果我们想让它更豪华css微元素,我们可以使用伪元素来制作固定点:
less
{
position: relative;
display: inline-block;
width: 50px;
height: 50px;
50%; :
#06c; :
:before { :
content: '';
position: absolute;
display: inline-block;
width: 12px;
height: 12px;
50%; :
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
#fff; :
}
:after { :
content: '';
45px; :
display: inline-block;
border: 15px solid transparent;
#06c; :
}
}
html
"locate-icon mr-20"> =
实现Momo图标
图中的两只耳朵主要是利用伪元素和box-shadow来实现的。 这样可以省一个伪元素来做个小尾巴。 至于如何实现不同形状的三角形,如果有疑问可以和我交流。 具体实现如下:
less
{
display: inline-block;
width: 50px;
height: 50px;
50%; :
rgb(68, 170, 59); :
:before { :
content: '';
14px; :
position: absolute;
width: 4px;
height: 4px;
50%; :
#fff; :
16px 0 0 #fff; :
}
:after { :
content: '';
42px; :
display: inline-block;
6px 10px 6px 10px; :
solid; :
transparent; :
rgb(68, 170, 59); :
transform: rotate(-147deg);
}
}
html
"wechat-icon mr-20"> =
实施勾选图标
这里也很简单,我们在伪元素的内容上打勾,然后设置颜色大小:
// less
.yes-icon {
display: inline-flex;
justify-content: center;
align-items: center;
width: 48px;
height: 48px;
background-color: green;
border-radius: 50%;
&::after {
content: '✓';
color:
font-size: 30px;
font-weight: bold;
}
}
// html
<span class="yes-icon mr-20">
实现耳朵(一般用于网站流量图标)
主要是制作一个椭圆加一个正方形伪元素:
{
display: inline-flex;
center; :
center; :
width: 58px;
height: 36px;
#06c; :
50%; :
:after { :
content: '';
display: inline-block;
width: 20px;
height: 20px;
50%; :
#fff; :
}
}
导航图标
原理类似,主要思路是画两个三角形,用伪元素的三角形挡住主元素的顶部:
{
position: relative;
display: inline-flex;
center; :
center; :
30px 15px 30px 15px; :
transparent; :
solid; :
#06c; :
transform: translate(10px, -16px) rotate(32deg);
:after { :
position: absolute;
bottom: -48px;
content: '';
display: inline-block;
10px 38px 30px 38px; :
transparent; :
solid; :
#fff; :
}
}
实现心形图标
原理是用两个伪元素实现两个椭圆,只需将它们旋转到重合即可:
{
position: relative;
display: inline-flex;
width: 50px;
height: 50px;
50%; :
rgb(212, 73, 17); :
:after { :
position: absolute;
content: '';
left: 10px;
top: 12px;
display: inline-block;
width: 20px;
height: 30px;
50%; :
#fff; :
transform: rotate(135deg);
}
:before { :
position: absolute;
content: '';
right: 10px;
top: 12px;
display: inline-block;
width: 20px;
height: 30px;
50%; :
#fff; :
transform: rotate(-135deg);
}
}
ps 图标
这个也使用伪元素。 一个伪元素用于文字图形,另一个用于笔画,营造出伪立体的感觉:
{
position: relative;
display: inline-flex;
center; :
center; :
width: 50px;
height: 50px;
8px; :
rgb(15, 102, 184); :
:before { :
position: absolute;
content: '';
display: inline-block;
width: 50px;
height: 40px;
rgba(255, 255, 255, .1); :
}
:after { :
position: absolute;
content: 'PS';
24px; :
display: inline-block;
color: #fff;
}
}
实现气泡对话框
类似于 Momo 图标:
{
display: inline-block;
width: 150px;
height: 50px;
8px; :
rgb(68, 170, 59); :
:before { :
content: '等你下课哦!';
14px; :
-33px; :
position: absolute;
color: #fff;
}
:after { :
content: '';
42px; :
display: inline-block;
6px 10px 6px 10px; :
solid; :
transparent; :
rgb(68, 170, 59); :
transform: rotate(-147deg) translate(-12px, 6px);
}
}
由于篇幅原因,其他图标就不一一实现了。 原理是相似的。 笔者之前曾使用该方案制作过一套100个图标库,并已在各个项目中成功使用。 因为体积小不会引起额外的要求,所以越来越实用,但对于比较复杂的图形,为了方便,建议使用字体图标或者svg、base64等。
终于
如果你想了解更多css、javascript、nodeJS等后端知识和实践,欢迎加入我们公众号“趣聊前端”,一起学习讨论,探索前端的边界后端。
发表评论