Flex布局之学习(三)

此篇文章是上一篇文章Flex布局之学习(二)的进一步完善,加入了每一块中的内容,并实现内容水平垂直居中。

实现效果

 实现方法

  •  在Header和Footer中比较简单,只需要设置
text-align: center; <!--水平居中-->
line-height: 68px; <!--垂直居中-->
  • 在content中则不能用此方法,因为其高度是不固定的,将line-height设置成100%显然是不可行的,所以在本篇文章中,我介绍一下用flex布局的实现方法。

代码

HTML部分

<div class="container">
     <div class="header">Header</div>
     <div class="content">
         <div class="content-left">
            <div class="left">left</div>
         </div>
            
         <div class="content-middle">
             <div class="middle">middle</div>
         </div>

         <div class="content-right">
             <div class="right">right</div>
         </div>  
     </div>
     <div class="footer">Footer</div>
</div>

CSS部分

body{
        margin: 0;
        padding: 0;
        font-size: 30px;
    }
    .container {
        width: 100%;
        min-height: 100%px;
        background-color: lightsalmon;
    }
    .header {
        height: 68px;
        width: 100%;
        text-align: center;
        line-height: 68px;
        position: absolute;
        background-color: lightsalmon;
    }
    .content {
      display: flex;
      display: -webkit-flex;
      /*纵向排列*/
      /* flex-direction: column; */
      width: 100%;    
      /* 水平居中 */
      justify-content: center;
      /*垂直居中*/
      align-items: center;
      background-color:lightgreen;
      position: absolute;
      top:68px;
      bottom:68px;
      overflow: auto; /*超出的部分,滚动条显示*/
  } 

  .content-left {
      background-color: aquamarine;
      width: 320px;
      /* 不放大 不缩小 固定宽度320px */
      flex: 0 0 320px;
      height: 100%;
  }
  .content-middle {
      background-color: yellow;
      height: 100%;
      flex: 1 1 auto;
  }
  .content-right {
      background-color: aquamarine;
      width: 320px;
      height: 100%;
      /* 不放大 不缩小 固定宽度320px */
      flex: 0 0 320px;
  }
  .content-left,.content-middle,.content-right {
      display: flex;
      justify-content: center;
      align-items: center;
  }
  .footer {
        width: 100%;
        height: 68px;
        position: absolute;
        bottom: 0;
        background-color: lightsalmon;
        text-align: center;
        line-height: 68px;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值