页面布局

作者: 邢走在云端 | 来源:发表于2019-11-07 12:21 被阅读0次
题目: 假设高度已知,请写出三栏布局,其中左栏右栏高度各为300px,中间自适应

慕课网视频

能写出有几种方案?

  • 方法一:float 浮动
  • 方法二:absoulte 绝对定位
  • 方法三:table 表格布局
  • 方法四:flex
  • 方法五:grid网格布局
<!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>页面布局</title>
</head>
<style>
    html * {
      margin: 0;
      padding: 0;
    }
</style>
<body>
  <p>假设高度已知,请写出三栏布局,其中左栏右栏高度各为300px,中间自适应</p>
  <div class="section">
    <style>
      .layout.float div {
        height: 100px;
      }
      .float .side {
        width: 300px;
      }
      .float .left {
        float: left;
        background: #0f0;
      }
      .float .right {
        float: right;
        background: #00f;
      }
      .float .center {
        background: #f00;
      }
    </style>
    <h1>方法一:浮动</h1>
    <div class="layout float">
      <div class="left  side"></div>
      <div class="right  side"></div>
      <div class="center"></div>
    </div>
  </div>
  <div class="section">
    <style>
      .layout.absolute div {
        height: 100px;
      }
      .absolute .side {
        width: 300px;
        position: absolute;
      }
      .absolute .left {
        left: 0;
        background: #0f0;
      }
      .absolute .right {
        right: 0;
        background: #00f;
      }
      .absolute .center {
        background: #f00;
      }
    </style>
    <h1>方法二:绝对定位</h1>
    <div class="layout absolute">
      <div class="left  side"></div>
      <div class="right  side"></div>
      <div class="center"></div>
    </div>
  </div>
  <div class="section">
    <style>
      .flex {
         display: flex;
      }
      .layout.flex div {
        height: 100px;
      }
      .flex .side {
        width: 300px;
        
      }
      .flex .left {
       
        background: #0f0;
      }
      .flex .right {
        
        background: #00f;
      }
      .flex .center {
        flex: 1;
        background: #f00;
      }
    </style>
    <h1>方法三:flex 弹性布局</h1>
    <div class="layout flex">
      <div class="left side"></div>
      <div class="center"></div>
      <div class="right side"></div>
    </div>
  </div>
  <div class="section">
    <style>
      .layout.table  {
        width: 100%;
        display: table;
        height: 100px;
      }
      .table > div {
        display: table-cell;
      }
      .table .side {
        width: 300px;
        
      }
      .table .left {
       
        background: #0f0;
      }
      .table .right {
        
        background: #00f;
      }
      .table .center {
        background: #f00;
      }
    </style>
    <h1>方法四:table 表格布局</h1>
    <div class="layout table">
      <div class="left side"></div>
      <div class="center"></div>
      <div class="right side"></div>
    </div>
  </div>
  <div class="section">
    <style>
      .grid {
        display: grid;
        width: 100%;
        grid-template-rows: 100px;
        grid-template-columns: 300px auto 300px;
      }
      .grid .side {
        width: 300px;
      }
  
      .grid .left {
        background: #0f0;
      }
  
      .grid .right {
        background: #00f;
      }
  
      .grid .center {
        background: #f00;
      }
    </style>
    <h1>方法五:grid 网格布局</h1>
    <div class="layout grid">
      <div class="left side"></div>
      <div class="center"></div>
      <div class="right side"></div>
    </div>
  </div>
</body>
</html>
截图
延伸

每个方案各自的优缺点

  • 浮动:
    缺点:脱离文档流 要清除浮动
    优点:兼容性好
  • 绝对定位
    缺点:脱离文档流,子元素也脱离
    优点:快捷,不易出错
  • flex
    缺点:IE兼容性不好 不能兼容IE8及以下浏览器。
    优点:比较完美 特别是移动端
  • table 布局
    缺点:当其中一个单元格高度超出的时候,两侧的单元格也是会跟着一起变高的,而有时候这种效果不是我们想要的。
    优点:兼容性好
  • 网格布局
    网格布局也是新出的一种布局方式

把高度已知去掉 哪些适用,哪些不适用

  • flex和table布局可以用,其他三种不行

相关文章

  • CoordinatorLayout嵌套CoordinatorLa

    页面布局:一级页面布局: 二级页面布局(即在一级页面ViewPager中的Fragment下): 这样布局的目的是...

  • 前端

    前端页面布局前端页面布局——三栏布局 - magi的博客 - CSDN博客 页面高度,位置简述前端页面内的高度、位...

  • Android自定义底部导航栏-Tabbar

    一、添加依赖 二、开始写布局 1、Tabbar布局页面 2、fragment_test.xml布局页面 这个页面就...

  • 左右条栏目 分层MVP RecyclerView

    页面的布局 主页面布局 左面的RecyclerView的布局 android:layout_width="matc...

  • Android----从相册选取图片

    导入包名 相册布局 相册条目布局 图片页面 图片条目布局 相册页面代码 图片页面代码 辅助类 MyImageSho...

  • 布局与定位

    布局 流体布局,扩展窗口时,页面中的内容随之扩展; 冻结布局,页面内容固定,不随页面的扩展而扩展;用一个 包含页面...

  • 【手把手】JavaWeb 入门级项目实战 -- 文章发布系统 (

    本节主要讲解详情页的页面布局和css样式,以及从主页面到详情页面的跳转问题。 1. 详情页面的布局 页面布局的话,...

  • 店铺装修基础:如何设置页面布局

    *讲解页面布局 三种不同的页面布局 旺铺基础版 旺铺专业版 如何设置网店页面布局 总体流程:进入布局管理——添加布...

  • IM社交App02

    阅读原文 注册 登录 3.3 功能三:主页面 页面布局 主界面代码 3.4 功能四:设置页面 页面布局 退出登录 ...

  • webx笔记-页面布局

    1.页面布局 • Screen,代表页面的主体。• Layout,代表页面的布局。• Control,代表嵌在sc...

网友评论

    本文标题:页面布局

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