zoukankan      html  css  js  c++  java
  • css使子元素在父元素居中的各种方法

    html结构:

    <div class="parent">
      <div class="child"></div>
    </div>
    

    方法一: display:flex

      .parent {
         500px;
        height: 500px;
        background: red;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .child {
         100px;
        height: 100px;
        background: blue;
      }
    

    方法二:display:table-cel

      .parent{
         500px;
        height: 500px;
        background: red;
        display: table-cell;
        vertical-align: middle;
      }
      .child{
         100px;
        height: 100px;
        background: blue;
        margin: auto;
      }
    

    方法三:绝对定位和0

      .parent{
         500px;
        height: 500px;
        background: red;
        position: relative;
      }
      .child{
         100px;
        height: 100px;
        background: blue;
        margin: auto;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;    
      }
    

    方法四:负边距

      .parent{
         500px;
        height: 500px;
        background: red;
        position: relative;
      }
      .child{
         100px;
        height: 100px;
        background: blue;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -50px;
        margin-top: -50px;  
      }
    

    以上四种方法都可以完成用css实现子元素在父元素实现水平和垂直居中。

     

  • 相关阅读:
    个人技术博客
    第十七周19年春学习总结
    第十六周第四次实验设计报告
    第十五周第三次实验设计报告
    第十四周第二次试验设计报告
    第十三周
    第十二周
    第十一周
    第十周作业
    只为粗暴看一下ES6的字符串模板的性能
  • 原文地址:https://www.cnblogs.com/clicklin/p/9821036.html
Copyright © 2011-2022 走看看