DIV 布局之上下布局上固定下自动适应

效果
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <title>顶部固定下部自动适应</title>
    <style type="text/css">
      html,body {height: 100%;padding: 0; margin: 0; }
      .parent{
        width:600px;
        height: 450px;
        margin:100px auto;
        position: relative;
        background-color: red;
      }
      .top{
        background-color: #BBE8F2;
        height:100px;
      }
      .bottom{
        background-color: #D9C666;
        position: absolute;
        top: 100px;
        bottom:0;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="top">top</div>
      <div class="bottom">
        bottom
      </div>
    </div>
  </body>  
</html>

1. 初始化

三个div:parent 、top、bottom
parent:宽600px、高450px、红色背景、整体采用相对定位。
top:浅蓝色背景
bottom:土黄色背景

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <title>顶部固定下部自动适应</title>
    <style type="text/css">
      html,body {height: 100%;padding: 0; margin: 0; }
      .parent{
        width:600px;
        height: 450px;
        margin:100px auto; 
        background-color: red;
        position: relative;
      }
      .top{
        background-color: #BBE8F2;
      }
      .bottom{
        background-color: #D9C666;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="top">top</div>
      <div class="bottom">
        bottom
      </div>
    </div>
  </body>  
</html>
image.png

2. 指定顶部高度为100

height:100px;

image.png

3. 将下部的div撑满

将下部的div定位修改成绝对定位,距离顶部距离修改成100px。底部距离为0
position: absolute; top: 100px; bottom:0;

image.png

修改底部宽度为100%
width:100%

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 6,872评论 0 11
  • 翻译自“Auto Layout Guide”。 2 自动布局细则手册 2.1 堆栈视图 接下来的章节展示了如何使用...
    lakerszhy阅读 5,881评论 3 9
  • 本课来自http://www.imooc.com/learn/9请不要用作商业用途。 HTML5 HTML介绍 H...
    PYLON阅读 8,628评论 0 5
  • 请参看我github中的wiki,不定期更新。https://github.com/ivonzhang/Front...
    zhangivon阅读 12,206评论 2 19
  • 有人总是说鸡汤文,鸡汤文,笨笨的我不知什么是鸡汤文。 有一天我买了一只鸡,做了一盆鸡汤,喝了几碗汤,还对着鸡汤,写...
    击文阅读 5,942评论 41 82

友情链接更多精彩内容