双飞翼布局(css)

本文介绍了双飞翼布局的DOM结构与圣杯布局的区别,详细阐述了如何通过CSS设置各列宽度与浮动,以及如何为左右两列预留空间。通过逐步添加元素并调整布局,展示了双飞翼布局的形成过程,最终给出了实现该布局的完整CSS代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. DOM结构

    <div class="head"></div>
    <div class="container column">
      <div class="center"></div>
    </div>
    <div class="left column"></div>
    <div class="right column"></div>
    <div class="footer"></div>

双飞翼布局的DOM结构与圣杯布局的区别是用container仅包裹住center,另外将.column类从center移至container上。

2. CSS代码

按照与圣杯布局相同的思路,首先设置各列的宽度与浮动,并且为左右两列预留出空间,以及为footer设置浮动清除:

.container {
  width: 100%;
}

.column {
  float: left;
}

.center {
  margin-left: 200px;
  margin-right: 150px;
}

.left {
  width: 200px; 
}

.right {
  width: 150px; 
}

.footer {
  clear: both;
}

得到如下效果示意图:

 

以上代码将container,left,right设置为float: left,而在container内部,center由于没有设置浮动,所以其宽度默认为container的100%宽度,通过对其设置margin-leftmargin-right为左右两列预留出了空间。

left放置到预留位置:

.left {
  width: 200px; 
  margin-left: -100%;
}

得到:

 

right放置到预留位置:

.right {
  width: 150px; 
  margin-left: -150px;
}

 

最后计算最小页面宽度:由于双飞翼布局没有用到position:relative进行定位,所以最小页面宽度应该为200+150=350px。但是当页面宽度缩小到350px附近时,会挤占中间栏的宽度,使得其内容被右侧栏覆盖,如下所示:

 

因此在设置最小页面宽度时,应该适当增加一些宽度以供中间栏使用(假设为150px),则有:

body {
  min-width: 500px;
}

至此双飞翼布局大功告成!其布局整体代码为:

       body{
            min-width: 550px;
        }
      .head{
        height: 200px;
        background: tomato;
      }
      .container{
          width: 100%;

          height: 200px;
      }
      .column{
          float: left;
      }
      .center{
          height: 200px;
          margin-left: 200px;
          margin-right: 150px;
          background: deeppink;
      }
      .left{
          height: 200px;
          width: 200px;
          background: blue;
          margin-left: -100%;
      }
      .right{
          height: 200px;
          width: 150px;
          background: yellow;
          margin-left: -150px;
      }
      .footer{
        clear: both;
        height: 200px;
        background: wheat;
      }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值