css ::before学习笔记
::before如果直接加在id或者class上面,则只加在对应的id/class上:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><div class="c"><div class="a"></div><div class="b"></div></div><Style>div{width: 300px;height: 300px;background-color: aquamarine;}.a{height: 50px;margin-top: 50px;background-color: blueviolet;}.b{height: 50px;background-color: azure;}.c::before{content: '';border: solid 2px chartreuse;}</Style>
</body>
</html>
渲染结果:
如果::before嵌套在选择器里面,就会加在所有后代元素上:
.c{::before{content: '';border: solid 2px chartreuse;}}
渲染结果:
如果希望是c::before的效果,但又由于管理代码方便,写在选择器内部,可以使用&语法,&::before相当于c::before:
.c{&::before{content: '';border: solid 2px chartreuse;}}
渲染结果: