阅读 115

CSS知识点

实现div盒子水平垂直居中的几种方式

  
      


  

  

  1.第一种方式元素已知宽度高度,绝对定位absolute+margin一半

  #middle {
    background: red;

    width: 200px;

    height: 200px;

    position: absolute;

    top: 50%;

    left: 50%;

    margin-left: -100px;

    margin-top: -100px;

  }

  2. 第二种方式元素未知,绝对定位absolute+translate(平移一半)

  #middle {
    background: red;

    width: 200px;

    height: 200px;

    position: absolute;

    top: 50%;

    left: 50%;

    transform: translate(-50%,-50%);

  }

  3. 第三种方式元素未知,绝对定位absolute+margin:auto

  #middle {
    background: red;

    width: 200px;

    height: 200px;

    position: absolute;

    left: 0;

    right: 0;

    top: 0;

    bottom: 0;

    margin: auto;

  }

  4. 第二种方式元素未知,display:flex

  #container{

    display: flex;

    justify-content: center;

    align-items: center;

  }

原文:https://www.cnblogs.com/vofill/p/14801986.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐