实现双飞翼布局

实现双飞翼布局说到圣杯布局和双飞翼布局 始终都是面试的高频考点 问题方式也大同小异 比如 实现一个左右固定 中间自适应的布局 实现一个中间部分优先加载的布局 现如今 很多 web 端的应用 他们的布局方式都是圣杯布局和双飞翼布局 既然用到的这么多 那么 双飞翼布局是如何实现的呢 带着这个问题 咱们一起把他说道说道双飞翼布局的实现方式一 position margin float 首先我们从上面简介部分得知 双飞翼布局是一个两边固定 中间自适应的布局 并且中间部分

        说到圣杯布局和双飞翼布局,始终都是面试的高频考点,问题方式也大同小异,比如,实现一个左右固定,中间自适应的布局;实现一个中间部分优先加载的布局……

        现如今,很多web端的应用,他们的布局方式都是圣杯布局和双飞翼布局,既然用到的这么多,那么,双飞翼布局是如何实现的呢?带着这个问题,咱们一起把他说道说道

双飞翼布局的实现方式一、(position+margin+float)

        首先我们从上面简介部分得知,双飞翼布局是一个两边固定,中间自适应的布局,并且中间部分还可以优先加载,那么从此条消息中,我们可以构想出一个HTML结构,结构如下

 
  
header
center
left
right

下面我们来一步步实现css部分

        1. 首先我们把基础的东西写好 ,在这里为了样式的好看,将box容器的宽度调整到了80%,并用margin-left给他实现了居中(其中实现居中的方式有很多很多很多种,对居中不太熟悉的同学可以自行百度下)

.box { width: 80%; margin-left: 10%; } .header { width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aqua; font-weight: 600; } .footer { clear: both; width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aquamarine; font-weight: 600; } .main { height: 150px; } .center { width: 100%; height: 150px; background-color: red; } .left { width: 150px; height: 150px; background-color: royalblue; } .right { width: 150px; height: 150px; background-color: sandybrown; } 

效果图:

实现双飞翼布局

注意:center的宽度一定要设置为100%

 2. 让center,left,right都设置为左浮动。这时因为center设置了宽度100%,所以left和right被“挤”到了下面

.box { width: 80%; margin-left: 10%; } .header { width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aqua; font-weight: 600; } .footer { clear: both; width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aquamarine; font-weight: 600; } .main { height: 150px; } .center { float: left; width: 100%; height: 150px; background-color: red; } .left { float: left; width: 150px; height: 150px; background-color: royalblue; } .right { float: left; width: 150px; height: 150px; background-color: sandybrown; } 

效果图:

实现双飞翼布局

3. 现在我们项让left和right变到上面去,即跟center在同一行,但是呢,center的宽度为100% ,没有left和right的空间了,怎么办呢?我们可以先通过给main加padding,把left和right的空间预留出来,这里有一个需要注意的点。如果一个元素,同时设置了width和padding的话,盒子的总宽度会增加,什么意思呢,看下面这张图

代码:

.main { height: 150px; width: 100%; padding: 0 150px; } /* 这里相对于上面的代码,我只对main进行了更改 */

效果图:

实现双飞翼布局

 但是呢,如果只存在padding,没有width的话,效果图是这样的

实现双飞翼布局

对于padding和width的关系。不懂的同学可以自行百度查一下(博主有点懒。。。。),到这里,是不是发现,我们已经预留出来了right和left的位置!!

4. 我们将left和right移动到我们预留出来的位置,这里用到了-margin

 

.box { width: 80%; margin-left: 10%; } .header { width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aqua; font-weight: 600; } .footer { clear: both; width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aquamarine; font-weight: 600; } .main { height: 150px; padding: 0 150px; } .center { float: left; width: 100%; height: 150px; background-color: red; } .left { float: left; margin-left: -100%; width: 150px; height: 150px; background-color: royalblue; } float: left; margin-left: -150px; width: 150px; height: 150px; background-color: sandybrown; } 

效果图:

实现双飞翼布局

这里,同学有没有发现一个问题,left和right把center的左右两端挡住了(center这个文案不见了),思考下怎么解决,答案马上揭晓

 是的!!!就是position属性,给他加定位!!!分别向左向右移动自身大小!!!

 最终代码如下

.box { width: 80%; margin-left: 10%; } .header { width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aqua; font-weight: 600; } .footer { clear: both; width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aquamarine; font-weight: 600; } .main { height: 150px; padding: 0 150px; } .center { float: left; width: 100%; height: 150px; background-color: red; } .left { position: relative; left: -150px; float: left; margin-left: -100%; width: 150px; height: 150px; background-color: royalblue; } .right { position: relative; left: 150px; float: left; margin-left: -150px; width: 150px; height: 150px; background-color: sandybrown; } 

效果图:

实现双飞翼布局

到这里,一个两边固定,中间自适应,并且中间优先加载的双飞翼布局就完成了!!!!!!

 

双飞翼布局的实现方式二、(flex)

        其实,flex布局很简单,因为flex本身的定义就是弹性盒,他有一个属性flex:1,就是专门解决自适应问题的,如果对flex不熟悉的同学可以戳   flex:1是什么?_CSDN_156的博客-CSDN博客   这篇文章(别问为什么这里有链接,问就是博主给自己做宣传哈哈哈哈哈哈~),废话不多说,代码如下

.box { width: 80%; margin-left: 10%; } .header { width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aqua; font-weight: 600; } .footer { clear: both; width: 100%; height: 50px; text-align: center; line-height: 50px; background-color: aquamarine; font-weight: 600; } .main { height: 150px; display: flex; } .center { flex: 1; display: flex; background-color: red; } .left { /* order order默认为0,不起作用,从小到大排序,1在前,以此类推 */ order: -1; flex-basis: 150px; background-color: royalblue; } .right { flex-basis: 150px; background-color: rgb(195, 196, 195); } 

效果图:

实现双飞翼布局

最后!有哪位好心的同学能教教我CSDN怎么放GIF的动图呢,可以私信教教我!!感激不尽

the last, 创作不易,如果这篇文章对您有些许帮助,请留下您的赞~感激不尽!!!

深夜创作,临表涕零,不知所言~~ 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213022.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午6:44
下一篇 2026年3月18日 下午6:44


相关推荐

  • Spring Batch 详解

    Spring Batch 详解JobLauncher 和JobRepository 对应着的Java接口分别是:JobLauncher和JobRepositoryJobLauncher.packageorg.springframework.batch.core.launch;(…)publicinterfaceJobLauncher{publicJobExecutionrun(

    2022年5月8日
    75
  • 像素密度的计算[通俗易懂]

    像素密度的计算[通俗易懂]手机屏幕5.0,指的是手机对角线的长度是5.0英寸,像素是960*1280,则像素密度的计算公式就是960的平方+1280的平方开根号除以5,得到的就是像素密度,一般有120,160,320,480

    2022年6月10日
    70
  • 纯CSS实现自定义单选框和复选框

    纯CSS实现自定义单选框和复选框<!DOCTYPEhtml><html> <head> <metacharset=”utf-8″> <title></title> <styletype=”text/css”> #main{ display:flex; justify-content:center; align-items:center; flex-wrap:wrap; } .

    2022年5月29日
    36
  • javachar转int_c中int转char

    javachar转int_c中int转charchar类型的数据转换成int类型的数字。本能反应是这么写的。publicstaticvoidmain(String[]args){charc=’1′;//本能反应是这么写的。inti=c;//或者这么写inti1=(int)c;System.out….

    2022年10月2日
    4
  • ubuntu卸载安装的方式

    ubuntu卸载安装的方式1、查找已经安装的插件查看全部:dpkg-l查看相关:dpkg-l|grep<筛选目标>例如:dpkg-l|grepgcc2、卸载apt-getremove<dpkg-l的结果>例如:apt-getremovelibgcc-4.8-dev注意:卸载过程中会有依赖关系而产生报错,需要自己确认相互依赖的关系,遵循…

    2022年5月29日
    36
  • mybatis拦截器修改sql_javaweb拦截器是什么

    mybatis拦截器修改sql_javaweb拦截器是什么/***Copyright2009-2016theoriginalauthororauthors.**LicensedundertheApacheLicense,Version2.0(the”License”);*youmaynotusethisfileexceptincompliancewiththeLicense.*…

    2025年10月10日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号