自适应布局

  之前在几个CSS布局问题中,举了上下固定,中间自适应布局的例子,当时的例子父级元素的宽高都给定了,其实并不是个好例子,既然要自适应,父级元素就应该跟着页面大小变化,从而中间子元素也自适应。3种方法,其实是2种,因为是针对body,所以用fixed可以直接替换absolute来用。注意同时给html和body的height100%,只给一个不行。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style type="text/css">
# {
  margin: 0;
  padding: 0;
}
html {
  height: 100%;
}
  body {
    width: 100%;
    height: 100%;
    margin: 0;
    font-size: 60px;
  }
  .class1 {
    height: 200px;
    width: 100%;
    background: #ccc;
    position: absolute;
    /*position: fixed;*/
    top: 0;
  }

  .class2 {
    width: 100%;
    background: #888;
    position: absolute;
    top: 200px;
    bottom: 200px;
  }

  .class3 {
    height: 200px;
    width: 100%;
    background: #333;
    position: absolute;
    /*position: fixed;*/
    bottom: 0;
  }
</style>
<body>
  <div class="class1">1111</div>
  <div class="class2">2222</div>
  <div class="class3">3333</div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style type="text/css">
# {
  margin: 0;
  padding: 0;
}
html {
  height: 100%;
}
  body {
    width: 100%;
    height: 100%;
    margin: 0;
    display: flex;
    flex-direction: column;
    font-size: 60px;
  }
  .class1 {
    height: 200px;
    width: 100%;
    background: #ccc;
  }

  .class2 {

    width: 100%;
    background: #888;
    flex: 1;
  }

  .class3 {
    height: 200px;
    width: 100%;
    background: #333;
  }
</style>
<body>
  <div class="class1">1111</div>
  <div class="class2">2222</div>
  <div class="class3">3333</div>
</body>
</html>

注意用flex的时候,所有的position布局的属性都要删掉。

 

转载于:https://www.cnblogs.com/zhansu/p/11048290.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值