首页 panel盒子设置上下左右四个边框
文章
取消

panel盒子设置上下左右四个边框

定位、less语法和伪元素选择器的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>panel盒子上下左右边框</title>
    <link rel="stylesheet" href="./panel.css">
</head>

<body>
    <div class="panel">
        <div class="panel-footer">
        </div>
    </div>
</body>

</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
.panel{
    width: 8.125rem;
    height: 8.125rem;
    border: 1px solid #1e1e1e;
    position: relative;
    &::before{
        position: absolute;
        content: '';
        top: 0;
        left: 0;
        height: 2.1042rem;        
        width: 2.1042rem;
        border-top: 2px solid green;
        border-left: 2px solid green;
    }
    &::after{
        position: absolute;
        content: '';
        top: 0;
        right: 0;
        /*这里after和before本来应该是行内元素的,但因为加了absolute定位,变成了块元素,因此可以设置宽高*/
        height: 2.1042rem;        
        width: 2.1042rem;
        border-top: 2px solid green;
        border-right: 2px solid green;
    }
    .panel-footer{
        /*这里panel-footer无定位,伪元素before和after相对于元素.panel*/
        &::before{
            position: absolute;
            content: '';
            bottom: 0;
            left: 0;
            height: 2.1042rem;        
            width: 2.1042rem;
            border-bottom: 2px solid green;
            border-left: 2px solid green;
        }
        &::after{
            position: absolute;
            content: '';
            bottom: 0;
            right: 0;
            height: 2.1042rem;        
            width: 2.1042rem;
            border-bottom: 2px solid green;
            border-right: 2px solid green;
        }
    }
}

提醒:
1.这里after和before本来应该是行内元素的,但因为加了absolute定位,变成了块元素,因此可以设置宽高
2.这里panel-footer无定位,伪元素before和after相对于元素.panel进行移动
3.&符号,这是less语法,指向上一级,也就是说&::after会被编译成.panel-footer::after或者panel::after,如果不使用&,就会编译成.panel-footer ::afterpanel ::after,这样不符号伪元素语法

本文由作者按照 CC BY 4.0 进行授权