三列布局方式

第一种

{/*
利用 overflow: hidden;的特性;
三栏的顺序分别为左-右-中。
左右两栏分别设置宽度以及左浮动和右浮动,脱离普通流,这时如果让中间栏高度大于2个边栏会发现两边栏实际上是叠在 main 上面的,因为 main 是块状元素,独占一行,浮动元素向相应的方法浮动,直到遇到容器的边框。
中间栏设置 overflow:hidden 创建BFC,这样就可以利用BFC不和浮动元素重叠的特性,让 main 的宽度自适应。
*/}

<div className="ThreeColumn1">
    <div className="left">左</div>
    <div className="right">右</div>
    <div className="main">自适应</div>
</div>
.ThreeColumn1 {
  margin: 50px;
}
.left {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    width: 200px;
    height: 200px;
    float: left;
    background: lightblue;
  }
  .right {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    width: 200px;
    height: 200px;
    float: right;
    background: rgb(230, 199, 173);
  }
  .main {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    height: 200px;
    overflow: hidden;
    background: rgb(173, 230, 201);
  }

第二种

{/*
内空Padding圣杯模式:
三栏的顺序为中-左-右,并包裹在ThreeColumn2容器内。
三栏同时设置float:left,左右两栏定宽、中间宽度100%。
利用负边距分别把left和right,这时视觉上left和right是叠在main上面的。
设置ThreeColumn2的左右padding分别为左右栏的宽度。
*/}

<div className="ThreeColumn2">
    <div className="main2">自适应</div>
    <div className="left2">左</div>
    <div className="right2">右</div>
</div>
.ThreeColumn2 {
  height: 200px;
  margin: 50px;
  padding: 0 120px 0 300px;
}
.main2 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    width: 100%;
    float: left;
    background-color: rgb(251, 141, 141);
    height: 200px;
  }
  .left2 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    float: left;
    background-color: rgb(182, 182, 245);
    height: 200px;
    width: 300px;
    margin-left: -100%;
    position: relative;
    left: -300px;
  }
  .right2 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    float: left;
    background-color: rgb(241, 241, 162);
    height: 200px;
    width: 120px;
    margin-left: -120px;
    position: relative;
    right: -120px;
  }

第三种

{/*
外框子 Margin 双飞翼模式:
双飞翼布局跟圣杯很像,区别在于圣杯利用padding控制main的位置,双飞翼用margin控制main的位置。
需要额外在main里面加一层div,用于margin控制,但是可以免去左右两栏的相对定位。
*/}

<div className="ThreeColumn3">
    <div className="main3">
        <div className="main_warp">自适应</div>
    </div>
    <div className="left3">左</div>
    <div className="right3">右</div>
</div>
.ThreeColumn3 {
    height: 200px;
    margin-top: 10px;
}
.main3 {
    width: 100%;
    float: left;
}
/*在双飞翼布局里,中间栏的边框、背景色要在`main-warp`里设置*/
.main_warp {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    height: 200px;
    background: rgb(240, 104, 240);
    margin: 0 230px 0 200px; /*设置main的位置*/
}
.left3 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    width: 200px;
    height: 200px;
    background: rgb(164, 245, 184);
    float: left;
    margin-left: -100%;
}
.right3 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    width: 230px;
    height: 200px;
    float: left;
    margin-left: -230px;
    background: rgb(243, 165, 178);
}

第四种

{/*
flex布局:
三栏的顺序为左-中-右,并包裹在warp容器内。
warp设置display: flex,如果三栏高度自适应又希望顶部对齐,需要添加align-items: flex-start。
左右栏各自设置定宽。
*/}

<div className="ThreeColumn4">
    <div className="left4">左</div>
    <div className="main4">自适应</div>
    <div className="right4">右</div>
</div>
.ThreeColumn4 {
    height: 200px;
    display: flex;
    align-items: flex-start;
    margin-top: 10px;
}
.main4 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    flex: 1;
    height: 200px;
    background: rgb(243, 135, 243);
}
.left4 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    width: 120px;
    height: 200px;
    background: pink;
  }
.right4 {
    text-align: center;
    color: white;
    font-weight: 500;
    font-size: 24px;
    line-height: 200px;
    width: 300px;
    height: 200px;
    background: greenyellow;
}

原文链接:https://blog.csdn.net/WangLuke_/article/details/118354314

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值