今天做项目的时候,要实现效果:中间是文字,两边是横线围着,其中有间距。了解了几种做法。伪元素这个刚开始没想到,回来又自己敲了一遍,以做记录…
效果:
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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>CSS伪元素实现中间文字, 两边横线效果</title> <style> .container { position: relative; width: 640px; margin: 0 auto; } .wrapper { text-align: center; width: 100%; } .wrapper .word:before, .wrapper .word:after { position: absolute; top: 50%; width: 40%; height: 1px; background: blue; content: ''; }
.wrapper .word::before { left: 0; } .wrapper .word::after { right: 0; } </style> </head> <body> <div class="container"> <div class="wrapper"> <div class="word">中间的字</div> </div> </div> </body> </html>
|