美文网首页简记
简要记录水平垂直居中方法!

简要记录水平垂直居中方法!

作者: 白小纯kl | 来源:发表于2019-10-25 13:58 被阅读0次

1、绝对定位

.father{
  width:500px;
  height:500px;
  background:yellow;
  position:relative;
  .son{
    width:100px;
    height:100px;
    background:red;
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    margin:auto;
  }
}

2、相对位移+transform位移

.father{
    width: 500px;
    height: 500px;
    background: yellow;
    .son{
      width: 100px;
      height: 100px;
      background: red;
      position: relative;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
    }
  }

3、相对位移+transform位移+margin

.father{
      width: 500px;
      height: 500px;
      background: yellow;
      .son{
        width: 100px;
        height: 100px;
        background: red;
        position: relative;
        top: 50%;
        margin: 0 auto;
        transform: translateY(-50%);  
      }
    }

4、相对位移+破坏非空白的折叠条件+margin

.father{
      width: 500px;
      height: 500px;
      background: yellow;
      margin: 0;
      /*border: 1px solid yellow;*/  //破坏非空白的折叠条件(子元素margin-top会影响父元素位置)
      padding: 0.1px;   //同上作用
      //overflow:hidden; //同上作用
      .son{
        width: 100px;
        height: 100px;
        background: red;
        position: relative;
        top: 50%;
        margin: 0 auto;
        margin-top: -50px;
      }
    }

5、flex布局

.father{
      width: 500px;
      height: 500px;
      background: yellow;
      display: flex;
      align-items: center; /*定义body的元素垂直居中*/
      justify-content: center; /*定义body的里的元素水平居中*/
      .son{
        width: 100px;
        height: 100px;
        background: red;
      }
    }

6、vertival-align:middle+伪类

.father{
      width: 500px;
      height: 500px;
      background: yellow;
      text-align: center;
      &::after{
        content: '';
        display: inline-block;
        height: 100%;
        vertical-align: middle;
      }
      .son{
        width: 100px;
        height: 100px;
        background: red;
        display: inline-block;
        vertical-align: middle;
      }
    }

7、table-cell +子元素display:inline-block

.father{
      width: 500px;
      height: 500px;
      background: yellow;
      display: table-cell;
      text-align:center;
      vertical-align:middle;
      .son{
        width: 100px;
        height: 100px;
        background: red;
        display: inline-block;
        vertical-align: middle;
      }
    }

8、绝对定位+计算calc

.father{
    width: 500px;
    height: 500px;
    background: yellow;
    position: relative;
    .son{
      width: 100px;
      height: 100px;
      background: red;
      position: absolute;
      left:-webkit-calc((500px - 100px)/2);
      top:-webkit-calc((500px - 100px)/2);
      left:-moz-calc((500px - 100px)/2);
      top:-moz-calc((500px - 100px)/2);
      left:calc((500px - 100px)/2);
      top:calc((500px - 100px)/2);
    }
  }
1.png

相关文章

  • 简要记录水平垂直居中方法!

    1、绝对定位 2、相对位移+transform位移 3、相对位移+transform位移+margin 4、相对位...

  • 常用的居中方法

    本文将介绍的居中方法有 ,水平居中,垂直居中和水平垂直居中。 一水平居中 二水平垂直居中

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

  • 垂直居中的多种写法

    1、垂直居中 2、水平居中 3、垂直水平居中 方法1:使用绝对定位 方法二:使用display:flex 扩展1:...

  • 用CSS实现元素居中的N种方法

    网页布局中,元素水平居中比垂直居中简单不少,同时实现水平垂直居中也不难,难的是想出多种实现水平垂直居中的方法并从中...

  • CSS解决盒模型居中的问题

    CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法 CSS实现盒子模型水平居中的方法 全局样式 第一种:...

  • CSS居中布局方案

    水平居中 垂直居中 水平垂直居中

  • day08

    A我今天学到了什么 垂直水平居中的3种方法 1.用transform垂直水平居中 2.用position水平居中 ...

  • 关于水平垂直居中的问题

    文字水平居中 文字水平垂直居中 设置padding高度自动撑开 flex 子元素在父元素中水平垂直居中 方法一 ...

  • 实现元素水平居中的五种方法

    概述 我们平时看到的居中效果主要分为三大类,水平居中,垂直居中和水平垂直居中,在这里总结以下元素水平居中的方法。 ...

网友评论

    本文标题:简要记录水平垂直居中方法!

    本文链接:https://www.haomeiwen.com/subject/bzlsvctx.html