:after伪元素标签写分割线

本文分享了两种在手机商城网站中实现分割线的方法。第一种利用伪元素和层级定位来制作带有文字的分割线;第二种则展示了如何为列表项添加分割线。

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

最近在写手机商城网站,用到了分割线,下面跟大家分享一下这种写法。

第一种写法:

html:

<div class="hor">
        <span>产品热卖</span>
    </div>


style:

.hor{
            width:100%;
            height:20px;
            background-color:#f2f2f2;
            text-align:center;
            color:#000;
            font-size:12px;
            line-height:20px;
            position:relative;
        }
        .hor:after{
            content:"";
            width:100%;
            height:1px;
           background-color:red;
           position:absolute;
           bottom:50%;
           z-index:1;
           left:0;
        }
      .hor span{
          z-index:2;
          position: relative;
          background-color:#f2f2f2;
          padding:0 10px;
      }

效果如下:


注意:

   z-index属性要配合相对定位和绝对定位才会起作用的。


第二种写法:

html:

    <div class="list">
                <ul>
                    <li>产品</li>
                    <li>法律</li>
                    <li>体检</li>
                    <li>案例</li>
                    <li>新增</li>
                </ul>
    </div>


style:

 .list{
          width:100%;
          background-color:#f2f2f2;
          margin-top:10px;


      }
      .list ul{
          list-style:none;
      }
      .list li{
            height:40px;
            /*border-bottom:1px solid red;*/
            padding-left:30px;
            position:relative;
      }
      .list li:after{
          content:'';
          width:calc(100% - 40px);/*如果两边都要留空位的话就需要计算宽度*/
          height:1px;
        background-color:red;
        bottom:0;
        left:40px;
        position:absolute;

      }

效果如下:













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值