如何实现双飞翼(圣杯)布局?

圣杯布局与双飞翼布局最终要实现的功能相同,都是为了实现一个两侧宽度固定,中间宽度自适应的三栏布局。

效果图:

 方法1、利用定位实现两侧固定中间自适应

1.1)父盒子设置左右 padding 值
1.2)给左右盒子的 width 设置父盒子的 padding 值,然后分别定位到 padding 处.
1.3)中间盒子自适应
具体 CSS 代码:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .father {
        height: 400px;
        background-color: pink;
        position: relative;
        padding: 0 200px;
      }
      .left,
      .right {
        width: 200px;
        height: 300px;
        background-color: yellow;
        position: absolute;
        top: 0;
      }
      .left {
        left: 0;
      }
      .right {
        right: 0;
      }
      .center {
        background-color: blue;
        height: 350px;
      }
    </style>
  </head>
  <body>
    <div class="father">
      <div class="left"></div>
      <div class="center"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

方法2、利用 flex 布局实现两侧固定中间自适应

2.1)父盒子设置 display:flex;
2.2)左右盒子设置固定宽高
2.3)中间盒子设置 flex:1 ;
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .father {
        height: 400px;
        background-color: pink;
        display: flex;
      }
      .left,.right {
        width: 200px;
        height: 300px;
        background-color: yellow;
      }
      .center {
        flex: 1;
        background-color: blue;
      }
    </style>
  </head>
  <body>
    <div class="father">
      <div class="left"></div>
      <div class="center"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

方法3、利用 bfc 块级格式化上下文, 实现两侧固定中间自适应

3.1)左右固定宽高,进行浮动
3.2)中间 overflow: hidden;
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .father {
        height: 500px;
        background-color: pink;
      }
      .left {
        float: left;
        width: 200px;
        height: 400px;
        background-color: yellow;
      }
      .right {
        float: right;
        width: 200px;
        height: 400px;
        background-color: yellow;
      }
      .center {
        height: 450px;
        background-color: blue;
        overflow: hidden;
      }
    </style>
  </head>
  <body>
    <div class="father">
   <!-- 注意:left 和 right 必须放在 center 前面 -->
      <div class="left"></div>
      <div class="right"></div>
      <div class="center"></div>
    </div>
  </body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值