伪元素实现文字左右横线

效果如下
image

html
1
<p className={styles['title']}>Sign in</p>
css
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
.title {
position: relative;
color: $gray-color-1;
text-align: center;
margin-bottom: 10px;

font-family: $decoration-text;
font-size: 28px;

&:before {
content: '';
position: absolute;
top: 50%;
/*background: $gray-color-8;*/
border-bottom: 1px solid $gray-color-8;
width: 35%;
height: 1px;
left: -1%;
}

&:after {
content: '';
position: absolute;
top: 50%;
/*background: $gray-color-8;*/
border-bottom: 1px solid $gray-color-8;
width: 35%;
height: 1px;
right: 1%;
}
}

主要是利用:before与:after伪元素,为横向预留位置

利用position将伪元素与主元素定位,利用布局将其居中显示,然后划线,background与border均可。