使用before、after给文字前后添加横线

本文深入探讨了CSS中.endSession类的使用技巧,包括如何通过:before和:after伪元素实现精细的布局控制,如线条的位置调整等,对于前端开发者来说,这些细节能够提升网页的视觉效果。
<style scoped lang="scss">
.endSession{
  text-align: center;
  position: relative;
  &:after{
    content: '';
    width: 52px;
    height: 1px;
    border-bottom: 1px solid #ccc;
    display: inline-block;
    position: absolute;
    top: 10px;
    right: 29px;
  }
  &:before{
    content: '';
    width: 52px;
    height: 1px;
    border-bottom: 1px solid #ccc;
    display: inline-block;
    position: absolute;
    top: 11px;
    left: 29px;
  }
}

</style>

实现中间文字两边横线效果有以下几种方法: ### 方法一:使用 HTML 元素和 CSS 布局 通过创建 HTML 元素,利用 CSS 的 `flex` 布局实现。示例代码如下: ```html <div class="line-box"> <span class="line"></span> <span class="text">我是一条分割线</span> <span class="line"></span> </div> <style> .line-box{ display: flex; justify-content: space-between; align-items: center; margin: 40px auto; } .text{ min-width: 120px; font-size: 14px; text-align: center; } .line{ width: 40%; height: 1px; background-color: #ccc; } </style> ``` 这种方法中,`line-box` 类使用 `flex` 布局,将子元素水平排列并均匀分布,通过设置 `line` 类的宽度和背景色来显示横线,`text` 类用于显示中间的文字 [^1]。 ### 方法二:使用 CSS 伪类 通过 CSS 伪类 `:before` 和 `:after` 为包含文字的元素添加横线。示例代码如下: ```html <div class="header"> <div>中间文字,两边横线</div> </div> <style> .header { width:400px; height:36px; line-height: 36px; text-align:center; border:1px solid green; position:relative; } .header div:before,.header div:after { position:absolute; background:#ccc; content:""; height:1px; top:50%; width:100px; } .header div:before{left:10px;} .header div:after{right:10px;} </style> ``` 在这个例子中,`.header` 元素设置了相对定位,`.header div:before` 和 `.header div:after` 伪类设置了绝对定位,通过 `top`、`left` 和 `right` 属性来确定横线的位置 [^2]。 ### 方法三:另一种 CSS 伪类实现 代码示例如下: ```html <div class="footer">中间文字</div> <style> .footer { width: 100%; height: 143px; font-size: 24px; font-weight: 400; color: rgba(153, 153, 153, 0.6); text-align: center; line-height: 156px; position: relative; } .footer:before, .footer:after { content: ''; position: absolute; top: 53%; background: #999999; width: 6px; height: 2px; } .footer:before { left: 32%; } .footer:after { right: 32%; } </style> ``` 此方法同样利用伪类 `:before` 和 `:after`,通过设置不同的 `left` 和 `right` 值来调整横线的位置 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值