flex、绝对定位的三栏布局,以及flex的扩充

在上一篇文章写了两栏等高、双飞翼和圣杯三栏布局。今天来继续完善三栏布局方式以及对flex布局继续完善。

三栏布局依然是下面这样。

图像

flex

<section class='body'>
	<artical class='content'>content</artical>
	<aside class='left'>left</aside>
	<aside class='right'>right</aside>
</section>
.body {display: flex;}
.left,.right {/*左右两列定宽200px*/flex:0 0 200px;height: 200px;background-color: pink;}
.left{order:-1}
.content {flex:1;height: 200px;background-color: yellow;}

绝对定位

<section class='body'>
	<aside class='content'>content</aside>
	<aside class='left'>left</aside>
	<aside class='right'>right</aside>
</section>
.body{position:relative;min-width:500px}
.content,.left,.right{height:200px;top:0}
.left,.right{background:pink;position:absolute;width:200px}
.left{left:0}
.right{right:0}
.content{margin:0 200px 0 200px;background:red}

上面用flex弹性盒子和position:absolute绝对定位完成了三栏布局,下面将对flex组成的三栏布局进行扩充,成下面这样

以下是相关代码

<header>header</header>
	<section class='body'>
		<article class='content'>content</article>
		<aside class='left'>left</aside>
		<aside class='right'>right</aside>
	</section>
<footer>footer</footer>
body{min-height:100vh;display:flex;flex-direction: column;}
header,footer{flex:1}
header{background:red}
footer{background:blue}
.body{flex:3;display:flex}
.content,.left,.right{}
.left,.right{flex:0 0 12rem}
.left{background:pink;order:-1}
.right{background:gray}
.content{background:yellow;flex:1}

下面继续来完善,当窗口缩小到768px以下时,呈现出:

只需要加上媒体查询,就可以实现上图

@media (max-width:768px){
 		.body{flex-direction:column;flex:8}
 		.left,.right{flex:1}
 		.content{flex:4}
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现flex实现三栏布局,可以按照以下步骤进行操作: 1. 首先,创建一个外层容器,可以使用`<div>`元素,并为其设置`display: flex;`来启用flex布局。[2] 2. 在外层容器中创建三个子元素,代表三栏布局的左、中、右栏。 3. 对左、右栏设置一个固定的宽度,例如`width: 300px;`。对中间栏不设置特定的宽度。 4. 对三个子元素设置`flex: 1 1 auto;`,这将使它们平分剩余空间。这意味着中间栏的宽度将自适应。 5. 可以根据需要为三个子元素添加其他样式,比如背景色、边框等。 以下是一个使用flex实现三栏布局的示例代码: ``` <div class="container"> <div class="left"></div> <div class="middle"></div> <div class="right"></div> </div> ``` ``` .container { display: flex; } .left, .right { width: 300px; } .middle { flex: 1 1 auto; } ``` 通过设置外层容器的`display: flex;`和子元素的相应样式,就可以实现使用flex布局三栏等分布局。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [flex实现三栏等分布局](https://blog.csdn.net/az44yao/article/details/117676640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [css3弹性盒子flex实现三栏布局的实现](https://download.csdn.net/download/weixin_38726441/14919543)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值