zoukankan      html  css  js  c++  java
  • 总结几种居中布局及三列布局

    水平居中及垂直居中

    // 方案一(宽度固定情况)
    
    #parent{
        width: 100%;
        height:100%;
        position: relative;
    }
    #child{
        width: 400px;
        height: 200px;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        background-color: #ccc;
    }
    
    // 方案二(宽度固定情况)
    
    #parent{
        position: relative;
        width: 100%;
        height: 100%;
    }
    #child{
        position: absolute;
        left: 50%;
        top: 50%;
        width: 400px;
        height: 200px;
        margin-top: -100px;
        margin-left: -200px;
        background-color: #ccc;
    }
    
    // 方案三(宽度不固定情况)
    
    #parent{
        position: relative;
        width: 100%;
        height: 100%;
    }
    #child{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        background-color: #ccc;
    }
    
    // 方案四(宽度不固定情况)
    
    #parent{
        display: flex;
        justify-content:center;
        align-items:center;    
        width: 100%;
        height: 100%;
    }
    #child{
        background-color: #ccc;
    }

    两边固定中间自适应的三列布局

    方案一 浮动
    注意:中间的center结构要放到最后面 以及父元素伪类清除浮动,
    
    .left{
        float: left;
        width: 200px;
        height: 200px;
    }
    .right{
        float: right;
        width: 100px;
        height: 100px;
    }
    .center{
        margin:0 120px 0 220px;
    }
    
    方案二 定位
    注意:这种方案高度不能自适应会塌陷
    
    .parent{
        position: relative;
    }
    .left{
        position: absolute;
        width: 200px;
        height: 200px;
        top: 0;
        left: 0;
    }
    .right{
        position: absolute;
        top: 0;
        right: 0;
        width: 100px;
        height: 100px;
    }
    .center{
        margin:0 120px 0 220px;
    }
    
    方案三 双飞
    注意:这种布局的结构要将中间center结构放置到最前面并套上一个容器,结构样式如下
    
    <div>
      <center></center>
    </div>
    <left></left>
    <right></right>
    
    div {
      float: left;
      width: 100%;
    }
    center {
      margin: 0px 140px 0px 220px;
    }
    left {
      float: left;
      margin-left: -100%;
      width: 200px;
    }
    right {
      float: left;
      margin-left: -120px;
      width: 120px;
    }

    左右两栏布局:四种

    1.左右浮动  
    2.一边浮动 一边margin 
    3.position定位

    4.父:display:flex

    子1:flex:0 0 60px width:60px

    子2:flex:1

    垂直居中

    父:display:table 子:display: table-cell, vertical-align:可以调整对齐方式
    注意:用浮动方法分左右两栏时:左边用浮动 右边margin负值 外层一定记得overflow并清除浮动

  • 相关阅读:
    让我偷偷的告诉你:运维加薪的杀手锏是啥?
    网站页面优化必然趋势—WebP 图片!
    如何监控 Tomcat?Zabbix 与 Cloud Insight 对比
    网页增重不可控?试试 OneAPM Cloud Test
    提高 ASP.NET Web 应用性能的 24 种方法和技巧
    OneAlert 入门(三)——事件分析
    Cloud Insight 客户案例-晨芯时代科技有限公司
    Java开发快速上手
    微信小程序快速开发上手
    微信小程序快速开发上手
  • 原文地址:https://www.cnblogs.com/gr07/p/8006223.html
Copyright © 2011-2022 走看看