html两栏 自适应布局,【CSS】三栏/两栏宽高自适应布局大全

页面布局

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

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

(1) 给出5种方案

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

html部分,center放到后面

left
right
content

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不变

left
right
content

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不变

left
right
content

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改到中间位置

left
content
right

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居中

left
content
right

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

top
content
bottom

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

top
content
bottom

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不变

top
content
bottom

css

.wrapper {

display: grid;

height: 700px;

grid-template-rows: 100px auto 100px;

}

.top {

background: red;

}

.bottom {

background: blue;

}

.content {

background: skyblue;

}

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

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

html

right
left

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

.left {

height: 200px;

background: red;

overflow: hidden;

}

.right {

float: right;

width: 300px;

background: blue;

}

方案二: 定位

html

left
right

css

.wrapper {

position: relative;

}

.left {

position: absolute;

left: 0;

right: 300px;

background: red;

}

.right {

position: absolute;

width: 300px;

right: 0;

background: blue;

}

方案三: flex

html

left
right

css

.wrapper {

display: flex;

}

.left {

flex: 1;

background: red;

}

.right {

width: 300px;

background: blue;

}

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

html

left
right

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

left
right

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

方案一: 定位

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

html

top
content

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

top
content

css

display: flex;

height: 800px;

}

.top {

height: 100px;

background: red;

}

.content {

flex: 1;

background: skyblue;

}

方案三: grid网格布局

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

html

top
content

css

.wrapper {

display: grid;

height: 800px;

grid-template-rows: 100px auto;

}

.top {

background: red;

}

.content {

background: skyblue;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: <div style="width:100%;"> <div style="float:left;width:30%;">左栏内容</div> <div style="float:left;width:40%;margin-left:2%;">中间栏内容</div> <div style="float:right;width:30%;">右栏内容</div> </div> ### 回答2: 可以使用HTMLCSS来创建一个三栏布局,其中左右两列的度固定,而中间列的度自适应。 首先,我们需要使用HTML创建一个三列的容器,可以使用`<div>`标签来实现。代码如下: ```html <div class="container"> <div class="left-column"></div> <div class="middle-column"></div> <div class="right-column"></div> </div> ``` 接下来,我们可以使用CSS来设置样式,以实现固定度和自适应度的效果。首先,我们设置三列容器的样式,将其设置为一行布局,并使用`display: flex;`属性,使其自动伸缩。同时,设置容器的度为100%,以确保占据整个容器的度。代码如下: ```css .container { display: flex; width: 100%; } ``` 接下来,我们为左中右三列设置样式。设置左右两列的度为固定值,例如200像素。代码如下: ```css .left-column { width: 200px; } .right-column { width: 200px; } ``` 为了让中间列自适应度,我们可以使用`flex-grow`属性,设置中间列的伸缩比例为1,使其自动填充剩余的度。代码如下: ```css .middle-column { flex-grow: 1; } ``` 最后,为了使布局更加美观,我们可以为三列容器和内部的列添加一些样式,例如背景色和内边距。代码如下: ```css .container { background-color: #f1f1f1; padding: 10px; } .left-column, .middle-column, .right-column { background-color: #e0e0e0; padding: 10px; margin: 5px; } ``` 通过这样设置,我们就可以得到一个具有固定度左右列和自适应度中间列的三栏布局。 请注意,以上代码只是一个示例,你可以根据自己的需求进行调整和修改。 ### 回答3: 要实现一个左右度固定中间自适应的三栏布局,可以使用HTMLCSS进行编写。 首先,在HTML中,我们可以使用```<div>```标签来创建三个元素,分别代表左栏、中栏和右栏。例如: ``` <div class="left-column">左栏内容</div> <div class="middle-column">中栏内容</div> <div class="right-column">右栏内容</div> ``` 然后,我们需要使用CSS来设置这些元素的样式。首先,设置左栏和右栏的度固定,可以使用```width```属性进行设置,例如: ``` .left-column { width: 200px; } .right-column { width: 200px; } ``` 接下来,设置中栏的度自适应,可以使用```flexbox```布局来实现。在CSS中,将父元素设置为```display: flex;```,然后将中栏设置为```flex-grow: 1;```,这样中栏的度将会自适应。例如: ``` body { display: flex; } .middle-column { flex-grow: 1; } ``` 最后,可以添加一些样式来美化布局,如设置背景颜色、边框样式等。 完整的HTMLCSS代码如下: ``` <!DOCTYPE html> <html> <head> <style> .left-column { width: 200px; background-color: lightgray; } .middle-column { flex-grow: 1; background-color: white; } .right-column { width: 200px; background-color: lightgray; } </style> </head> <body> <div class="left-column">左栏内容</div> <div class="middle-column">中栏内容</div> <div class="right-column">右栏内容</div> </body> </html> ``` 通过上述HTMLCSS代码,我们实现了一个左右度固定中间自适应的三栏布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值