zoukankan      html  css  js  c++  java
  • absolute元素水平居中

    Solution 1:

    给absolute元素的left设为50%, margin-left设为absolute元素宽度一半的负数

    .con{
      width:200px;
      height:200px;
      background:#ccc;
      position:relative;
    }
    .abs{
      width:40px;
      height:20px;
      background:steelblue;
      position:absolute;
      bottom:0;
    
      left:50%;
      margin-left:-20px;
    }

    Solution 2:

    原理和1相似,设left:50%,但使用css3的transform:translate(x,y);

    .con{
      width:200px;
      height:200px;
      background:#ccc;
      position:relative;
    }
    .abs{
      width:40px;
      height:20px;
      background:steelblue;
      position:absolute;
      bottom:0;
    
      left:50%;
      transform:translate(-50%);
    }


    Solution 3:

    margin:auto;实现居中,但是absolute元素一定要有宽度,并且如果宽度不合适(常见于ul li)也是不会居中的

    .con{
      width:200px;
      height:200px;
      background:#ccc;
      position:relative;
    }
    .abs{
      width:40px;
      height:20px;
      background:steelblue;
      position:absolute;
      bottom:0;
      left:0;
      right:0;
      margin:0 auto;
    }
  • 相关阅读:
    vue day6 分页显示
    vue day5 分页控件
    vue day4 table
    c# excel xlsx 保存
    diff算法
    Web Workers
    多线程
    Http请求优化
    高效编写代码
    渲染引擎
  • 原文地址:https://www.cnblogs.com/magicg/p/15566008.html
Copyright © 2011-2022 走看看