前端学习——常见两栏和三栏布局

一、两栏布局

左边宽度固定,右边宽度自适应

1.1 实现方法总结

  • 左侧浮动+ margin-left(右侧)
  • 左侧浮动 + 右侧overflow: hidden
  • 左侧绝对定位 + margin-left(右侧)
  • 右侧绝对定位(left为左侧宽度,必须设置top、bottom、right为0)
  • 父级元素flex布局,右侧flex:1 (自动放大占满剩余空间)

html结构如下:

<div class="contain">
    <div class="left"></div>
    <div class="right"></div>
</div>

1、左侧浮动+ margin-left

.contain {
    height: 300px;
}
.left {
    float: left;
    height: 100%;
    width: 200px;
    background-color: pink;
}
.right {
    overflow: hidden;
    height: 100%;
    background-color: blue;
}

1.2 左侧浮动+ 右侧overflow: hidden

.contain {
    height: 300px;
}
.left {
    float: left;
    height: 100%;
    width: 200px;
    background-color: pink;
}
.right {
    overflow: hidden;
    height: 100%;
    background-color: blue;
}

1.3 左侧定位+ margin-left(右侧)

.contain {
    height: 300px;
    position: relative;
}
.left {
    position: absolute;
    height: 100%;
    width: 200px;
    background-color: pink;
}
.right {
    margin-left: 200px;
    height: 100%;
    background-color: blue;
}

1.4 右侧定位(left为左侧宽度)

.contain {
    height: 300px;
    position: relative;
}
.left {
    height: 100%;
    width: 200px;
    background-color: pink;
}
.right {
    position: absolute;
    left: 200px;
    top: 0;
    bottom: 0;
    right: 0;
    height: 100%;
    background-color: blue;
}

1.5 父元素flex布局,右侧flex:1

.contain {
    height: 300px;
    display: flex;
}
.left {
    height: 100%;
    width: 200px;
    background-color: pink;
}
.right {
    flex: 1;
    left: 200px;
    height: 100%;
    background-color: blue;
}

二、三栏布局

2.1 浮动实现

经典的三栏布局分为常见的双飞翼布局和圣杯布局,都是中间宽度自适应,两侧宽度固定
区别: 双飞翼布局是通过margin实现,圣杯布局是通过padding实现
圣杯布局:三者都浮动,外容器在padding中留出左右两边的宽度,利用相对定位定位出来
双飞翼布局:三者都浮动,利用margin实现,没有采用相对定位

2.1.1 圣杯布局

<div class="contain">
	<!-- 优先渲染center,将它放在最前面 -->
    <div class="center"></div>
    <div class="left"></div>
    <div class="right"></div>
</div>
* {
    padding: 0;
    margin: 0;
}
.contain {
    overflow: hidden;
    padding: 0 220px 0 200px;
    height: 400px;
}
body {
    min-width: 700px;
}
.center,
.left,
.right {
    float: left;
    height: 100%;
}
.center {
    width: 100%;
    background-color: pink;
}
.left {
    width: 200px;
    background-color: skyblue;
    margin-left: -100%;
    position: relative;
    right: 200px;
}
.right {
    width: 220px;
    background-color: blue;
    margin-right: -220px;
}

2.1.2 双飞翼布局

<div class="contain">
    <div class="center">
        <div class="content">中间</div>
    </div>
    <div class="left">左边</div>
    <div class="right">右边</div>
</div>
.contain {
    height: 500px;
    overflow: hidden;
}
.center,
.left,
.right {
    float: left;
    height: 100%;
}
.center {
    width: 100%;
    background-color: pink;
}
.left {
    width: 220px;
    background-color: skyblue;
    margin-left: -100%;
}
.right {
    width: 200px;
    background-color: red;
    margin-left: -200px;
}
.content {
    margin: 0 200px 0 220px;
}

2.2 flex布局实现

flex布局下圣杯和双飞翼布局实现方法一样

.contain {
    display: flex;
    height: 200px;
}
.left {
    width: 100px;
    height: 100%;
    order: -1;
    background-color: red;
}
.right {
    width: 200px;
    height: 100%;
    background-color: blue;
}
.center {
    flex: 1;
    background-color: pink;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,那我可以告诉您如何实现一个基础的前端三栏布局页面。首先,我们可以使用HTMLCSS来完成这个页面。 HTML部分: ``` <!DOCTYPE html> <html> <head> <title>三栏布局页面</title> <style type="text/css"> /* 设置外层容器的宽度和居中 */ .container { width: 960px; margin: 0 auto; } /* 设置三栏的样式 */ .left-column, .right-column, .content { float: left; height: 500px; position: relative; } /* 设置左侧栏的样式 */ .left-column { width: 200px; background-color: #EEE; } /* 设置右侧栏的样式 */ .right-column { width: 200px; background-color: #EEE; } /* 设置中间区域的样式 */ .content { width: 560px; background-color: #FFF; } /* 清除浮动 */ .clear { clear: both; } </style> </head> <body> <div class="container"> <div class="left-column"> <!-- 左侧栏内容 --> </div> <div class="content"> <!-- 中间内容 --> </div> <div class="right-column"> <!-- 右侧栏内容 --> </div> <div class="clear"></div> </div> </body> </html> ``` CSS部分: ``` /* 设置外层容器的宽度和居中 */ .container { width: 960px; margin: 0 auto; } /* 设置三栏的样式 */ .left-column, .right-column, .content { float: left; height: 500px; position: relative; } /* 设置左侧栏的样式 */ .left-column { width: 200px; background-color: #EEE; } /* 设置右侧栏的样式 */ .right-column { width: 200px; background-color: #EEE; } /* 设置中间区域的样式 */ .content { width: 560px; background-color: #FFF; } /* 清除浮动 */ .clear { clear: both; } ``` 以上就是一个基础的前端三栏布局页面的实现方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值