微信小程序文字两边添加横线

本文介绍两种使用WXSS实现文字两边带线效果的方法。第一种利用伪元素简化布局,通过调整border-spacing属性控制线条长度。第二种采用更复杂的Flex布局,分别设置前后空白文本视图作为线条。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

 

实现代码

 方法1 简单点的

 wxml 

<view >
     <text class='reason_txt'>入网的理由</text>
</view>

wxss


 
 
.reason_txt{
  font-size: 32rpx;
  color: #333333;
  display: table;
  width: auto;
  white-space: nowrap;
  border-spacing: 0.5rem 0;
  margin-left: 28rpx;
  margin-right: 28rpx;
}

.reason_txt::before,.reason_txt::after{
  display: table-cell;
  content: "";
  width: 50%;
  background: linear-gradient(#D8D8D8, #D8D8D8) repeat-x center;
  background-size: 0.1rem 0.1rem;
}



若果想修改线的长度修改border-spacing: 0.5rem 0; 的值即可 

 

方法二:麻烦点的

wxml代码如下

<view class='main_view'>
      <text class='befor'></text>
     <text class='reason_txt'>入网的理由</text>
      <text class='after'></text>
</view>

wxss 代码如下

 .main_view{
   display: flex;
   flex-direction: row;
   justify-content: space-around;
   width: 100%;
   height: 60rpx;
   margin-top: 100rpx;
   
 }

 .befor{
   width: 200rpx;
   height: 1rpx;
   background: #333333;
   line-height: 60rpx;
 }
 .after{
   width: 200rpx;
   height: 1rpx;
   background: #333333;
   line-height: 60rpx;
 }

 .reason_txt{
   position: relative;
   margin-top: -28rpx;
 }



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值