美文网首页
几种常用flex布局

几种常用flex布局

作者: 地主家也没余粮叻 | 来源:发表于2019-10-14 11:33 被阅读0次
1. 一左一右布局
<div class="box">
  <div class=“item”></div>
  <div class="item item2"></div>
</div>
.box{
  display:flex;
  flex-flow:row nowrap;
  justify-content:space-between;
}
.item{
  width:100px;
  height:100px;
  background:pink;
}
.item2{
  background:coral;
}
一左一右
2. footer居底部,不受内容高度影响
<div class="box">
  <header></header>
  <div class="main"></div>
  <footer></footer>
</div>
.box{
  display:flex;
  min-height:100vh;
  flex-direction:column;
}
main{
  flex:1;
}
footer居底部,不随中间内容高度影响
3. 绝对居中
html,body{
  height:100%;
}
.box{
 display:flex;
 align-items:center;
 justify-content:center;
}
.box-con{
 width:100px; // 可不写,只是为了方便看效果
 height:100px; // 可不写,只是为了方便看效果
 background:#fff;
}
不写宽高,水平垂直居中
4. 最便携垂直水平居中
.box {
      width: 500px;
      height: 500px;
      background: pink;
      display: flex;
}
        
 .con {
     width: 40px;
     height: 40px;
     margin: auto;
     border: 1px solid #fff;
}
<div class="box">
     <div class="con"></div>
</div>
最便携垂直水平居中
5. 绝对定位水平垂直居中
方法一:
// 绝对定位的自动伸缩性特性
.box{ // 推荐
  position:absolute;
  top:0;
  left:0;
  bottom:0;
  right:0;
  margin:auto;
}
方法二:
.box2{ // 不推荐
  position:absolute;
  top:50%;
  left:50%;
  margin:-50px 0  0 -50px; //margin-top:设为height的一半取负,margin-left:设为width的一半取负
  width:100px;
  height:100px;
}
注意:
1. 方法二 中left和right的值不能超过其相对元素width减去它自身width的一半,否则绝对定位元素会优先取left的值进行定位(文档流从左到右渲染),但top和bottom就没有这个限制。
2. 方法二如果长宽改了,margin-top、margin-left就要进行相应的换算
故推荐方法一

6. 一行平均排列
<div class="box">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>
.box{
 display:flex;
 justify-content:space-around;
}
.item{
  flex:1;
  height:40px;
  margin-right:20px;
  border:1px solid red;
  &:last-child{
    margin-right:0;  
  }
}
平均排列
7. 多列布局
.item{
    display:flex;
    flex-wrap:wrap; // 换行
    flex-direction:row // 主轴排列方向,起点在左端
}
.child{
    width:200px;
    border:1px solid red;
    height:60px;
    margin:0 20px 20px 0;
}
<div class="item">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>
响应式多列布局
8. 不知宽高图片垂直水平居中
.main{
       display:table-cell;
        vertical-align: middle;
        text-align: center;
        overflow:hidden;
}
<div class="main">
<img src="">
</div>

相关文章

  • 几种常用flex布局

    1. 一左一右布局 2. footer居底部,不受内容高度影响 3. 绝对居中 4. 最便携垂直水平居中 5. ...

  • Good Luck

    概念 HTML以及CSS篇,集中在CSS 说下你常用的几种布局方式,集中往盒模型、flex布局说(至于grid布局...

  • 2018-06-12 CSS中的flex布局详解

    前言:之前我写过的一篇博客介绍CSS常用的几种布局方式,PC端最常见的就是浮动布局和flex布局,而在移动端,由于...

  • flex布局常用汇总

    flex布局常用汇总

  • 梳理flex布局

    现在flex布局最为常用,面试也经常会问到 Flex 布局是什么? Flex 是 Flexible Box 的缩写...

  • 02-小程序:Flex布局

    一、简介 1.1、flex 布局 (Flexible布局,弹性布局)是在小程序里面常用的布局方式官方文档:flex...

  • flex布局之flex 属性(一看就会)

      flex是弹性布局(Flexible Box)中常用到的属性,是flex-grow, flex-shrink ...

  • flex布局

    flex弹性布局常用https://www.ruanyifeng.com/blog/2015/07/flex-gr...

  • flex布局应用

    codepen 常用布局页脚固定 flex:1的含义

  • 前端面试题整理二

    CSS 1、垂直居中的方法 文本内容:使用line-height flex布局扩展:flex常用属性1、flex-...

网友评论

      本文标题:几种常用flex布局

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