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

    温习一下元素水平垂直居中的几种方法

    元素有具体宽度

    1、absolute+负边距

    .LV_center{
      border: 1px solid red;
      position: absolute;
       100px;
      height: 100px;
      top:50%;
      left: 50%;
      margin-top:-原高度/2 ;
      margin-left: -原高度/2
    }

    2、absolute+calc

    .LV_center{
      border: 1px solid red;
      position: absolute;
       100px;
      height: 100px;
      top:calc(50% - 原高度/2);
      left:calc(50% - 原高度/2);
    }

     3、absolute+margin auto

    .LV_center{
      border: 1px solid red;
      position: absolute;
       100px;
      height: 100px;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      margin: auto;
    }

    元素宽度不定

    1、absolute+transform,css3特性2d平移

    .LV_center{
      border: 1px solid red;
      position:absolute;
      top:50%;
      left:50%;
      transform:translate(-50%,-50%);
    }

    2、line-height,外层设置行高,内层继承行高

    .box{
      line-height: 300px;
    }
    .LV_center{
      border: 1px solid red;
      display: inline-block;
      line-height: inherit;
      vertical-align: middle;
    }

    3、table-cell,模拟td特性,让元素像表格里的td

    .box{
      border: 1px solid red;
       300px;
      height: 300px;
      display: table-cell;
      text-align: center;
      vertical-align: middle;
    }
    .LV_center{
      border: 1px solid red;
      display: inline-block;
    }

    4、flex弹性盒子

    .LV_center{
    border: 1px solid red;
      display: flex;
      justify-content: center;  //主轴对齐方式
      align-items: center;  //位于主轴中心
    }

    推荐一位讲得更详细的 : https://www.jianshu.com/p/1b3337214941

  • 相关阅读:
    三次请求(读-改-读)引出nibernate 一级缓存
    算法竞赛入门经典第一、二章摘记
    uva 10905 Children's Game
    uva 11205 The broken pedometer
    uva 10160 Servicing stations
    uva 208 Firetruck
    uva 167 The Sultan's Successors
    zoj 1016 Parencodings
    uva 307 Sticks
    uva 216 Getting in Line
  • 原文地址:https://www.cnblogs.com/tenfly/p/11446479.html
Copyright © 2011-2022 走看看