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

97 阅读1分钟

1.利用定位实现两侧固定中间自适应(定位 + padding)

1.1)父盒子设置左右 padding 值

1.2)给左右盒子的 width 设置父盒子的 padding 值,然后分别定位到 padding 处

1.3)中间盒子自适应

<style>

     .father {

         height: 400px;

         background-color: pink;

         position: relative;

         padding: 0 200px;

     }

     .left{

         width: 200px;

         height: 300px;

         background-color: yellow;

         position: absolute;

         top: 0;

         left: 0;

     }

     .right {

         width: 200px;

         height: 300px;

         background-color: yellow;

         position: absolute;

         top: 0;

         right: 0;

     }

     .center {

         background-color: blue;

         height: 350px;

     }

</style>

 

<div class="father">

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

    <div class="center"></div>

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

</div>

2、利用 flex 布局实现两侧固定中间自适应(display:flex + flex:1)

2.1)父盒子设置 display:flex;

2.2)左右盒子设置固定宽高

2.3)中间盒子设置 flex: 1;

<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>

 <div class="father">

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

    <div class="center"></div>

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

</div>

3、利用 bfc 块级格式化上下文, 实现两侧固定中间自适应(浮动 + overflow: hidden)

3.1)左右固定宽高,进行浮动

3.2)中间 overflow: hidden;

<style>

    .father {

         height: 500px;

         background-color: pink;

     }

     .left {

         float: left;

         width: 200px;

         height: 400px;

         background-color: blue;

     }

     .right {

         float: right;

         width: 200px;

         height: 400px;

         background-color: blue;

     }

     .center {

         height: 450px;

         background-color: green;

         overflow: hidden;

     }

 </style>

 html 结构

<!-- 注意: left 和 right 必须放在 center 前面 -->

<div class="father">

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

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

 <div class="center"></div>

</div>