页面布局

注意方案多样性、各自原理、各自优缺点、如果不定高呢、兼容性如何

三栏自适应布局,左右两侧300px,中间宽度自适应

(1) 给出5种方案

  • 方案一: float (左右浮动,中间不用给宽,设置margin左右留出位置)

html部分,center放到后面

        <section class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
<div class="content">content</div>
</section>

css部分

        .wrapper {
height: 100px;
}
.left {
float: left;
width: 300px;
height: 100%;
background: red;
}
.right {
float: right;
width: 300px;
height: 100%;
background: yellow;
}
.content {
background: skyblue;
height: 100%;
margin: 0 300px;
}
  • 方案二: position定位 (中间设置left 300 right 300 ,宽度就自适应了)

html不变

        <section class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
<div class="content">content</div>
</section>

css部分

        .wrapper {
position: relative;
height: 100px;
}
.left {
position: absolute;
left: 0;
width: 300px;
height: 100%;
background: red;
}
.right {
position: absolute;
right: 0;
width: 300px;
height: 100%;
background: yellow;
}
.content {
position: absolute;
left: 300px;
right: 300px;
background: skyblue;
height: 100%;
}
  • 方案三: flex伸缩布局

html不变

        <section class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
<div class="content">content</div>
</section>

css部分

        .wrapper {
display: flex;
height: 100px;
}
.left {
width: 300px;
height: 100%;
background: red;
}
.right {
width: 300px;
height: 100%;
background: yellow;
}
.content {
flex: 1;
background: skyblue;
height: 100%;
}
  • 方案四: 表格布局 (设置父元素为display:table,子元素都是display:table-cell;然后给两边设置width,中间不设置就自动了,记得父元素给width:100%)

html部分,将center改到中间位置

        <section class="wrapper">
<div class="left">left</div>
<div class="content">content</div>
<div class="right">right</div>
</section>

css部分

       .wrapper {
display: table;
width: 100%;
height: 100px;
}
.left {
display: table-cell;
width: 300px;
height: 100%;
background: red;
}
.right {
display: table-cell;
width: 300px;
height: 100%;
background: yellow;
}
.content {
display: table-cell;
background: skyblue;
height: 100%;
}
  • 方案五: 网格布局 Grid第一个专门为解决布局问题而创建的CSS模块 (设置容器类型,然后设置列宽和行高)

html部分不变,center居中

        <section class="wrapper">
<div class="left">left</div>
<div class="content">content</div>
<div class="right">right</div>
</section>

css部分十分简洁

        .wrapper {
display: grid;
width: 100%;
grid-template-rows: 200px 100px 10px;
grid-template-columns: 300px auto 300px;
}
.left {
background: red;
}
.right {
background: yellow;
}
.content {
background: skyblue;
}

(2) 各自的优缺点。

方案一、方案二:

float和position方案的有点是兼容性好,因为都是比较老的解决方案了,
缺点是float之后需要清除浮动造成的影响,
定位的话就是绝对定位之后脱离文档流了,你要注意用position:relative包裹一下

方案三:

flex是目前移动端主流的方案,css的语法,缺点就是IE8以下不支持。

方案四:

语义化不太好,

方案五:

有严重的兼容性问题

(3) 如果不定高,哪些方案会有问题

如果不定高float / 定位的方案会有问题

三栏自适应布局,上下固定,中间高度自适应 (自适应的地方设置top300 bottom300,高度就自适应了)

方案一: 定位

html

    <section class="wrapper">
<div class="top">top</div>
<div class="content">content</div>
<div class="bottom">bottom</div>
</section>

css

        .wrapper {
height: 800px;
position: relative;
}
.top {
position: absolute;
top: 0;
height: 100px;
width: 100%;
background: red;
}
.bottom {
position: absolute;
bottom: 0 ;
height: 100px;
width: 100%;
background: blue;
}
.content {
position: absolute;
top: 100px;
bottom: 100px;
width: 100%;
background: skyblue;
}

方案二: flex布局

html

    <section class="wrapper">
<div class="top">top</div>
<div class="content">content</div>
<div class="bottom">bottom</div>
</section>

css

        .wrapper {
display: flex;
height: 700px;
flex-direction: column;
}
.top {
height: 100px;
background: red;
}
.bottom {
height: 100px;
background: blue;
}
.content {
flex: 1;
background: skyblue;
}

方案三: 网格布局grid (设置grid-template-rows: 300px auto 300px)

html不变

    <section class="wrapper">
<div class="top">top</div>
<div class="content">content</div>
<div class="bottom">bottom</div>
</section>

css

      .wrapper {
display: grid;
height: 700px;
grid-template-rows: 100px auto 100px;
}
.top {
background: red;
}
.bottom {
background: blue;
}
.content {
background: skyblue;
}

两栏自适应,右侧固定,左侧自适应

方案一: 利用BFC的渲染规则,BFC不会和浮动的元素互相重叠
html

    <section class="wrapper">
<div class="right">right</div>
<div class="left">left</div>
</section>

css 避免左侧侵入到右侧,给左侧div创建一个BFC,因为BFC的渲染机制就是独立的容器,不会和浮动的元素重叠

        .left {
height: 200px;
background: red;
overflow: hidden;
}
.right {
float: right;
width: 300px;
background: blue;
}

方案二: 定位

html

    <section class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
</section>

css

        .wrapper {
position: relative;
}
.left {
position: absolute;
left: 0;
right: 300px;
background: red;
}
.right {
position: absolute;
width: 300px;
right: 0;
background: blue;
}

方案三: flex

html

    <section class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
</section>

css

      .wrapper {
display: flex;
}
.left {
flex: 1;
background: red;
}
.right {
width: 300px;
background: blue;
}

方案四: 表格布局,注意给父元素表格要设置width:100%

html

    <section class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
</section>

css

        .wrapper {
display: table;
width: 100%;
}
.left {
display: table-cell;
background: red;
}
.right {
display: table-cell;
width: 300px;
background: blue;
}

方案五: grid网格布局

css

        .wrapper {
display: grid;
grid-template-columns: auto 300px;
}
.left {
background: red;
}
.right {
background: blue;
}

html

<section class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
</section>

两栏自适应,上侧固定,下侧自适应

方案一: 定位

设置content部分的top: 100px botton: 0

html

        <section class="wrapper">
<div class="top">top</div>
<div class="content">content</div>
</section>

css

        .wrapper {
position: relative;
height: 100%;
width: 100%;
}
.top {
position: absolute;
top: 0;
height: 100px;
background: red;
width: 100%; }
.content {
position: absolute;
width: 100%;
top: 100px;
bottom: 0;
background: skyblue;
}

方案二: flex

top高度100px,然后content设置flex: 1

html

        <section class="wrapper">
<div class="top">top</div>
<div class="content">content</div>
</section>

css

               display: flex;
height: 800px;
}
.top {
height: 100px;
background: red; }
.content {
flex: 1;
background: skyblue;
}

方案三: grid网格布局

思路,就是设置display:grid后 设置列宽或者行高,有多少列就设置多少个参数,多少行就设多少参数。

html

     <section class="wrapper">
<div class="top">top</div>
<div class="content">content</div>
</section>

css

     .wrapper {
display: grid;
height: 800px;
grid-template-rows: 100px auto;
}
.top {
background: red;
}
.content {
background: skyblue;
}

【CSS】三栏/两栏宽高自适应布局大全的更多相关文章

  1. 从零开始学习前端开发 — 7、CSS宽高自适应

    一.宽度自适应 语法:width:100%; 注: a)块状元素的默认宽度为100% b) 当给元素设置宽度为100%时,继承父元素的宽度 c) 通常使用宽度自适应实现通栏效果 二.高度自适应 语法: ...

  2. css 两列布局中单列定宽单列自适应布局的6种思路

    前面的话 说起自适应布局方式,单列定宽单列自适应布局是最基本的布局形式.本文将从float.inline-block.table.absolute.flex和grid这六种思路来详细说明如何巧妙地实现 ...

  3. css+background实现 图片宽高自适应,拉伸裁剪不变形

    图片宽高不固定 ,一样实现自适应,拉伸裁剪不变形,适应各大兼容性.  下面咱们在网上找两张宽高不一样的照片:     No.1                                      ...

  4. CSS布局 两列布局之单列定宽,单列自适应布局思路

    前言 说起自适应布局方式,单列定宽单列自适应布局是最基本的布局形式.比如斗鱼的直播间,后台管理系统都是常用的 我们将从 float, inline-block, table, absolute, fl ...

  5. jquery操作html中图片宽高自适应

    在网站制作中如果后台上传的图片不做宽高限制,在前台显示的时候,经常会出现图片变形,实用下面方法可以让图片根据宽高自适应,不论是长图片或者高图片都可以完美显示. $("#myTab0_Cont ...

  6. CSS 实现:两栏布局(等宽布局)

    ☊[实现要求]:两栏等宽布局 <div class="demo3"> <div class="col-1"></div> & ...

  7. CSS 实现:两栏布局(一边固定,一边自适应)

    ☊[实现要求]:CSS实现左边固定,右边自适应父容器宽度的两栏布局. <body> <div class="left"></div> <d ...

  8. css 宽高自适应的div 元素 如何居中 垂直居中

    在我们 编写css 样式的时候经常会遇见一个问题 那就是一个 宽高未知的元素 要让他 垂直居中如何实现这个呢 下面是我常用的两种方法 上代码 下面的是 结构代码 <div class=" ...

  9. 翻屏类 h5 适配方案:解决宽高自适应难题

    表格 图片等 宽度自适应  :width:100%;  box-sizing: border-box; 基于淘宝适配方案flexible + 翻屏h5 适配方案adaptive flexible解读及 ...

随机推荐

  1. (四)创建基于maven的javaFX+springboot项目,用户界面与后台逻辑分离方式

    下面来介绍创建maven的javaFX+springboot项目,基于用户界面与后天逻辑分离的方式,用户界面使用fxml文件来常见,类似于jsp,可以引入css文件修饰界面 maven依赖 <d ...

  2. Docker 部署mysql、tomcat笔记

    Docker 笔记整理 #.环境:Ubuntu 18.* #.安装 mysql 5.6.tomcat #.docker search mysql 报错:Error response from daem ...

  3. day8 socket

    代码: 例子1:socket tcp 通讯 server端 import socketserver = socket.socket()ip_port = ("127.0.0.1", ...

  4. Spring与junit4集成测试

    一.应用场景:普通java web集成spring test, 项目结构:使用的是加入jar的方式,不是maven加入依赖的方式 注意:如果是引入jar包的方式,例如:spring-test4.0.5 ...

  5. odoo xml中添加数据的数字代表含义

    参考原文:https://alanhou.org/odoo12-import-export-data/ <?xml version="1.0"?> <odoo n ...

  6. FASTCGI/CGI

    在了解这两个协议之前,我们先谈一下动态网页 动态网页 是指跟静态网页相对的一种网页编程技术.静态网页,随着html代码的生成,页面的内容和显示效果就基本上不会发生变化了--除非你修改页面代码.而动态网 ...

  7. SDK更新失败问题解决

    环境:1. win102. Have over fire wall 解决办法:1.启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『A ...

  8. 如何在GitBook中使用Git管理

    1.在GitBook中新建[Book],找到对应目录下的位置,拷贝[book]名称后删除文件夹以及文件,新建文件夹重命名为[book]名称 2.打开文件夹,在根目录下选择创建git仓库,即 git i ...

  9. Ubuntu 18.04 系统 Update 后内核升级到5.0 导致 VMware Workstation 不正常工作

    Ubuntu 18.04 系统升级后,发现内核从 4.18 升级到 5.0,从而导致 VMware Workstation 启动主动更新,但总会显示 vmmon 和 vmnet 俩模块编译失败. 感谢 ...

  10. jsfuck-原理

    jsfuck真的fuck,第一眼就是WTF?? Example The following source will do an alert(1): [][(![]+[])[+[]]+([![]]+[] ...