CSS让子元素div的高度填满父容器的剩余空间

在写项目中很多时候都会遇到这种情况,在父级中分为上下两个部分,其中头部的header是固定的,那main就要把剩下的区域自适应填充(即减去头部header的高度自适应)
在这里插入图片描述
对很多人都想到了直接给main设置height为100%不就行了,但是这种方式会让浏览器的滚动条出现,也就是你的高度没有减去顶部header的高度,这种写法是很不友好的

试了很多次最终用的是:让头部的header浮动并且让他的width为100%,main的高度还设为100%这样

不说了直接上代码:

<template>
  <div class="home">
    <el-container>
      <el-header>Header</el-header>
      <el-main>
       main
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Layout'
}
</script>

<style scoped>
.home{
  width: 100%;
  height: 100%;
}
.el-header{
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
  width: 100%;
  float: left;
}
.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  height:100%;
}

.el-container {
  /*margin-bottom: 40px;*/
  width: 100%;
  height: 100%;
}

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值