美文网首页前端学习程序员让前端飞
网页中“复杂”的布局需求实现

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

作者: webCoder | 来源:发表于2016-11-01 19:29 被阅读1194次

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

内容提纲

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

效果图:

<!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.经典的三列布局——双飞翼布局与圣杯布局

相关文章

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

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

  • CSS-布局1-浮动三列布局

    1、三列布局需求 网页中,常见的实现3列布局,左右两边宽宽固定,中间自适应的布局。本节介绍一种,最简单的实现思路,...

  • GridLayoutManager 如何实现一会一行一会二行呢

    参考文章 Recyclerview根据setSpanSizeLookup实现复杂布局(不用嵌套) 需求 说明 这个...

  • jquery多列布局插件--columnizer-jquery-

    多列布局可以用column,参见《复杂的多列布局的翻页与打印实现》 但是有个需求,用column不太好实现,例如当...

  • 你真的了解ConstraintLayout吗?

    先说下,怎么接触到这个到,在一次工作中,有个动画需求,对布局要求比较复杂,然后通过相对布局实现了。然后要完...

  • HTML中的JavaScript

    网页中的 JavaScript 能让页面完成复杂的逻辑交互,实现各种各样的功能需求,下面就让我们来了解一下 HTM...

  • CSS层叠样式表-盒子模型

    盒子模型 作用:实现网页布局,在网页中画盒子组成:边框 border内边距 padding外边距 margin 1...

  • Grid布局

    1.网格布局(Grid)是强大的CSS布局方案,它将网页划分为一个个的网格,通过任意组合这些网格来实现不同需求的布...

  • 菜鸟学习笔记:表格布局和div+css布局

    网页布局可以通过表格和div元素来实现(注:table布局已经淘汰),首先我们来看看table布局 实现效果 **...

  • CSS 系列——Grid布局学习笔记

    Grid 布局,就是网格布局。 简单的需求,垂直居中、水平居中等,有 Flex 布局。 网格布局,对应网页设计或者...

网友评论

    本文标题:网页中“复杂”的布局需求实现

    本文链接:https://www.haomeiwen.com/subject/jacmuttx.html