CSS多栏布局-两栏布局和三栏布局

目录

一、两栏布局

左列定宽,右列自适应

        1.利用浮动,margin实现

2.浮动+BFC

3. 定位

4.flex

5.table布局 

二、三栏布局

左右两列定宽,中间自适应

         1.浮动+margin 

2.浮动+BFC

3.flex

4.table布局

5.定位


相信大家在面试的时候也会经常碰到两栏、三栏布局,一下列出来几种方式以供大家参考,废话不多说,直接上干货,如有不足之处,请大家补充。

一、两栏布局

左列定宽,右列自适应

1.利用浮动,margin实现

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 200px;
      height: 100%;
      background: rebeccapurple;
      text-align: center;
      float: left;
    }
    .right {
      height: 100%;
      margin-left: 200px;
      background: pink;
      text-align: center;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right自适应</div>
  </div>

 2.浮动+BFC

.container {
      width: 500px;
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 200px;
      height: 100%;
      background: rgb(177, 141, 214);
      text-align: center;
      float: left;
    }
    .right {
      height: 100%;
      /* 触发BFC */
      overflow: hidden;
      background: rgb(205, 29, 58);
      text-align: center;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right自适应</div>
  </div>

 

3. 定位

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
      position: relative;
    }
    .left {
      width: 200px;
      height: 100%;
      background: orange;
      text-align: center;
      position: absolute;
      left: 0;
    }
    .right {
      height: 100%;
      background: blue;
      text-align: center;
      position: absolute;
      left: 200px;
      right: 0;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right自适应</div>
  </div>

 

 4.flex

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
      display: flex;
    }
    .left {
      width: 200px;
      height: 100%;
      background: orange;
      text-align: center;
    }
    .right {
      height: 100%;
      background: blue;
      text-align: center;
      flex: 1;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right自适应</div>
  </div>

5.table布局 

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
      display: table;
    }
    .left {
      width: 200px;
      height: 100%;
      background: orange;
      text-align: center;
      display: table-cell;
    }
    .right {
      height: 100%;
      background: blue;
      text-align: center;
      display: table-cell;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right自适应</div>
  </div>

二、三栏布局

左右两列定宽,中间自适应

1.浮动+margin 

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 100px;
      height: 100%;
      float: left;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      float: right;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      margin-left: 100px;
      margin-right: 100px;
      background: oldlace;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right固定</div>
    <div class="center">中间自适应</div>
  </div>

 2.浮动+BFC

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 100px;
      height: 100%;
      float: left;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      float: right;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      /* 触发BFC */
      overflow: hidden;
      background: oldlace;
      text-align: center;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right固定</div>
    <div class="center">中间自适应</div>
  </div>

3.flex

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
      display: flex;
    }
    .left {
      width: 100px;
      height: 100%;
      float: left;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      float: right;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      flex: 1;
      background: oldlace;
      text-align: center;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="center">中间自适应</div>
    <div class="right">right固定</div>
  </div>

4.table布局

注意容器的顺序

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
      display: table;
    }
    .left {
      width: 100px;
      height: 100%;
      display: table-cell;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      display: table-cell;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      display: table-cell;
      background: oldlace;
      text-align: center;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="center">中间自适应</div>
    <div class="right">right固定</div>
  </div>

5.定位

注意容器的顺序

<style>
    .container {
      width: 500px;
      height: 200px;
      line-height: 200px;
      position: relative;
    }
    .left {
      width: 100px;
      height: 100%;
      background: orange;
      text-align: center;
      position: absolute;
      left: 0;
    }
    .right {
      width: 100px;
      height: 100%;
      background: blue;
      text-align: center;
      position: absolute;
      right: 0;
    }

    .center {
      height: 100%;
      background: oldlace;
      text-align: center;
      margin: 0 100px;
    }
  </style>
  <div class="container">
    <div class="left">left固定</div>
    <div class="right">right固定</div>
    <div class="center">中间自适应</div>
  </div>

欢迎在评论区交流。如果文章对你有所帮助,不要忘了点上宝贵的一赞!

我的博客原文:CSS多栏布局-两栏布局和三栏布局

  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要使用flex实现两栏布局,你可以使用flex布局的属性来实现。具体的实现方式有多种,以下是其中的一种方法: 首先,在父元素上应用flex布局属性,比如设置display为flex,这样父元素的子元素就可以通过flex属性进行布局。 然后,设置左边元素的固定宽度,可以使用flex属性中的flex-basis来指定宽度,比如设置为200px。 接着,设置右边元素的flex属性为1,这样它会自动填充剩余的空间。 最后,根据需要设置元素的高度和背景颜色等样式。 以下是一个使用flex实现两栏布局的示例代码: ```html <div class="outer"> <div class="left"></div> <div class="right"></div> </div> ``` ```css .outer { display: flex; height: 100px; } .left { width: 200px; height: 100%; background: lightcoral; } .right { flex: 1; height: 100%; background: lightseagreen; } ``` 这样,左边的元素会固定宽度为200px,右边的元素会自动填充剩余的空间。你可以根据实际需求调整元素的高度和样式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [实现常用的两栏布局(左侧固定+右侧自适应)以及三栏布局(圣杯布局和双飞翼布局)](https://blog.csdn.net/hzy199772/article/details/125639600)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [经典布局 (flex和传统两种实现) 左右两栏式](https://blog.csdn.net/m0_49515138/article/details/129136780)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

富朝阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值