网页中“复杂”的布局需求实现

作为一名前端“攻城狮”,实现所有需求的页面布局是最基本的技能。下面的内容,给大家介绍一些复杂的布局。

内容提纲

当页面内容不足一屏的高度时,底部内容出现在屏幕底部;当页面内容高度一屏的高度时,内容处于正常的文档流底部;

效果图:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  *{
    margin: 0;
    padding: 0;
  }
  html, body{
    height: 100%;
  }
  body {
    display: table;
    width: 100%;
  }
  .main{
    /*height: 2000px;*/
    background: #000;
    color: #fff;
  }
  .footer{
    background: #f00;
    display: table-footer-group;
  }
  </style>
</head>
<body>
  <div class="main">这里是主体区域</div>
  <div class="footer">这里是底部</div>
</body>
</html>

触屏端页面未知顶部高度,主体容器撑满剩余空间;(现实场景:页面初始化时顶部状态栏是出现的,但是主体容器的内容是需要调接口的,在接口未返回结果前,页面出现loading的动画,且loading在剩余空间内水平、垂直居中)

效果图:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  *{
    margin: 0;
    padding: 0;
  }

  html,body{
    height: 100%;
  }
  .wrap{
    display: flex;
    flex-direction: column;
    height: 100%;
  }
  .head{
    background: #000;
    color: #fff;
    text-align: center;
  }
  .loading{
    position: relative;
    background: #f00;
    flex-grow: 1;
  }
  .loading-content{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #fff;
  }

  </style>
</head>
<body>
  <div class="wrap">
    <div class="head">
      <p>head</p>
      <p>head</p>
    </div>
    <div class="loading">
      <div class="loading-content">loading...</div>
    </div>
  </div>
</body>
</html>

触屏端未知顶部、底部高度,中间内容自适应;

效果图:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Document</title>
 <style>
 *{
   margin: 0;
   padding: 0;
 }
 html, body{
   height: 100%;
 }

 .wrap{
   display: flex;
   flex-direction: column;
   height: 100%;
 }

 .header{
   background: #000;
   color: #fff;
   text-align: center;
 }
 .main{
   flex-grow: 1;
   background: #f00;
   color: #fff;
 }
 .footer{
   background: #007fff;
   color: #fff;
   text-align: center;
 }
 </style>
</head>
<body>
 <div class="wrap">
   <div class="header">
     <p>header</p>
     <p>header</p>
   </div>
   <div class="main">这里是主体区域</div>
   <div class="footer">
     <p>footer</p>
     <p>footer</p>
   </div>
 </div>
</body>
</html>

PC端实现上下高度已知切固定在顶部,主体内容高度占满剩余空间;(PC端对于flexbox有兼容性问题)

效果图:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  *{
    margin: 0;
    padding: 0;
  }
  html, body{
    height: 100%;
  }

  .oh{
    overflow: hidden;
  }

  .header{
    position: absolute;
    top: 0;
    width: 100%;
    height: 100px;
    line-height: 100px;
    background: #000;
    color: #fff;
    text-align: center;
  }

  .main{
    height: 100%;
    padding: 100px 0;
    background: #f00;
    color: #fff;
  }

  .footer{
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 100px;
    line-height: 100px;
    background: #007fff;
    color: #fff;
    text-align: center;
  }
  </style>
</head>
<body class="oh">
  <div class="header">
    header
  </div>
  <div class="main">
    这里是主体区域
  </div>
  <div class="footer">
    footer
  </div>
</body>
</html>

左中右布局,左侧、右侧宽度固定,中间宽度自适应;

效果图:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .clearfix:after {
      content: ".";
      display: block;
      height: 0;
      clear: both;
      visibility: hidden
    }

    .clearfix {
      *+height: 1%
    }

    .left{
      float: left;
      width: 200px;
      height: 100px;
      background: #000;
      color: #fff;
      margin-left: -100%;
    }

    .right{
      float: left;
      width: 100px;
      height: 100px;
      background: #000;
      color: #fff;
      margin-left: -100px;
    }

    .middle{
      float: left;
      width: 100%;
      background: #f00;
      color: #fff;
    }

    .middle-content{
      margin: 0 100px 0 200px;
    }
  </style>
</head>
<body>
  <div class="wrap clearfix">
    <div class="middle">
      <div class="middle-content">
        middle
      </div>
    </div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
</body>
</html>
  • 左中右布局,且三列的高度均与根据三列的最大高度保持一致;

效果图:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .clearfix:after {
      content: ".";
      display: block;
      height: 0;
      clear: both;
      visibility: hidden
    }

    .clearfix {
      *+height: 1%
    }

    .left{
      float: left;
      width: 200px;
      background: #000;
      color: #fff;
      margin-left: -100%;
    }

    .right{
      float: left;
      width: 100px;
      background: #000;
      color: #fff;
      margin-left: -100px;
    }

    .middle{
      float: left;
      width: 100%;
      background: #f00;
      color: #fff;
    }

    .middle-content{
      margin: 0 100px 0 200px;
    }

    .wrap{
      overflow: hidden;
    }

    .height{
      padding-bottom: 9999px;
      margin-bottom: -9999px;
    }
  </style>
</head>
<body>
  <div class="wrap clearfix">
    <div class="middle height">
      <div class="middle-content">
        <p>middle</p>
        <p>middle</p>
        <p>middle</p>
        <p>middle</p>
        <p>middle</p>
      </div>
    </div>
    <div class="left height">left</div>
    <div class="right height">right</div>
  </div>
</body>
</html>

希望这篇文章对您有用(最后附上2篇文章)~

1.常见的两列、三列布局,宽高自适应
2.经典的三列布局——双飞翼布局与圣杯布局

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,628评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,544评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,596评论 4 59
  • 月近中秋 中秋是月分外明,总是一抹忧愁挂枝头 散不尽的是寂寞,抒不完的是情怀, 古人见月愁相思,今起看日望尘埃 吹...
    庄里豹哥说阅读 96评论 0 0
  • 又是一年开学季,又来到新的学校、迎来了新的同学。憧憬着新生活的美好与放纵,却没曾想,自己,想多了。 一 开学前的那...
    贾淼阅读 539评论 0 0