css常见常用样式:居中/布局/清除浮动

167 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第3天,点击查看活动详情

1、居中

水平居中

  • 行内元素: text-align: center
.parent-box {
  text-align: center
}
.child-box {
  display: inline;
  /* display: inline-block; */
} 
  • 定宽块状元素: 左右 margin 值为 auto

    .child-box {
      width: 100px;
      margin: 0 auto;
    }
    
  • 不定宽块状元素: position + transform

    .parent-box {
      position: relative;
    }
    .child-box {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
    }
    
  • 使用flex-box布局,指定justify-content属性为center

    .parent-box{
      display: flex;
      justify-content: center;
    }
    

垂直居中

  • 对于单行文本, line-height 设置成和 height 值

  • 子元素定高:marginposition + margin(负值)

    /* 定高方案1 */
    .child-box {
      height: 100px;
      margin: 50px 0;   
    }
    /* 定高方案2 */
    .parent-box{
      position: relative;
    }
    .child-box {
      height: 100px;  /*子元素高度*/
      position: absolute;
      top: 50%;
      margin-top: -50px;  /*自身高度一半*/
    }
    
  • 子元素不定高:position + transformflexIFC + vertical-align:middle

    • 不定高方案1:position + transform
    .center {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }
    
    • 不定高方案2:flex布局
    .parent-box {
      display: flex;
      align-items: center;
    }
    .child-box {
      width: 100%;
    }
    
    • 不定高方案3:设置 inline-block 则会在外层产生 IFC,高度设为 100% 撑开 父元素 的高度
    .parent-box::before {
      content: '';
      height: 100%;
      display: inline-block;
      vertical-align: middle;
    }
    .parent-box {
      text-align: center;
    }
    .child-box {
      display: inline-block;  
      vertical-align: middle;
    }
    

垂直水平居中

  • 对于已知子元素宽高的:
.parent-box {
  position: relative;
  .child-box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100px;
    height: 100px;
    margin: -50px 0 0 -50px;  // 上和左的margin为自身宽高的一半
  }
}
  • 对于未知宽高的:

    • position + transform
    .parent-box {
      position: relative;
      .child-box {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
    
    • 弹性盒模型
    .parent-box {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100px;
    }
    
    • table布局
    .parent-box {
      display: table;
      .child-box {
        display: table-cell;
        vertical-align: middle;
      }
    }
    

2、布局

左边定宽,右边自适应方案

  • 设置float + margin
.parent{
  width:100%
}
.left {
  width: 120px;
  float: left;
}
.right {
  margin-left: 120px;  /* 不加margin-left右边元素也可以自适应 */
}
  • 设置float + calc
.left {
  width: 120px;
  float: left;
}
.right {
  width: calc(100% - 120px);
  float: left;
}
  • 弹性盒flex布局+自适应元素为flex:1
.parent{
    display: flex;
    width: 100%;
    height: 200px;
    div{
        height: 200px;
    }
    .left{
        width: 200px;
    }
    .right{
        flex: 1
    }
}

左右两边定宽,中间自适应

  • 流体布局:左右两边都float,中间元素设置两边的margin
.parent{
  width: 100%;
  height: 200px;
  div{
    height: 100%
  }
  .left {
    float: left;
    width: 120px;
  }
  .right {
    float: right;
    width: 120px;
  }
  .center {
    margin: 0 120px; 
  }
}
  • 圣杯布局(设置BFC,margin负值法)

    要求:三列布局;中间主体内容前置,且宽度自适应;两边内容定宽

    • 好处:重要的内容放在文档流前面可以优先渲染
    • 原理:利用相对定位、浮动、负边距布局,而不添加额外标签
<div class="parent">
    <div class="main"></div>
    <div class="left"></div>
    <div class="right"></div>
</div>
.parent {
      padding-left: 150px;
      padding-right: 190px;
  }
  .main {
      float: left;
      width: 100%;
  }
  .left {
      float: left;
      width: 190px;
      margin-left: -100%;
      position: relative;
      left: -150px;
  }
  .right {
      float: left;
      width: 190px;
      margin-left: -190px;
      position: relative;
      right: -190px;
  }
  • float + calc
.left {
  float: left;
  width: 120px;
}
.right {
  float: right;
  width: 120px;
}
.center {
  width: calc(100% - 240px);
  margin-left: 120px;
}
  • flex布局
.parent {
  display: flex;
  .left {
    width: 120px;
  }
  .right {
    width: 120px;
  }
  .center {
    flex: 1;
  }
}

3、清除浮动

  • 在浮动元素后面添加 clear:both的空 div 元素
.parent{
    .left{
        float: left;
        width: 40%
    }
    .right{
        float: left;
        width: 60%;
    }
    /* 添加一个空元素 */
    .empty{
        clear: both;
        height: 0;
        overflow:hidden;
    }
}
  • 给父元素添加 overflow:hidden 或者 auto 样式,触发BFC
.parent{
    width: 300px;
    background-color: #aaa;
    overflow:hidden;
    zoom:1;   /*IE6*/
}
  • 万能清除浮动:使用伪元素,也是在元素末尾添加一个点并带有 clear: both 属性的元素实现的。
.parent{
    zoom: 1; /*IE6*/
}
.parent:after{
    content: ".";
    height: 0;
    clear: both;
    display: block;
    visibility: hidden;
}

推荐使用第三种方法,不会在页面新增div,文档结构更加清晰