今天聊聊一个经典的布局实例:
实现一个三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化
可能很多朋友已经笑了,这玩意儿通过双飞翼布局就能轻松实现。不过,还请容我在双飞翼之外,循序渐进地介绍一下我们可以如何实现一个三列布局。
1. 首先,使用浮动布局来实现一下
See the Pen float-three-columns by xal821792703 ( @honoka ) on CodePen .
- 左侧元素与右侧元素优先渲染,分别向左和向右浮动
- 中间元素在文档流的最后渲染,并将 width 设为 100%,则会自动插入到左右两列元素的中间,随后设置 margin 左右边距分别为左右两列的宽度,将中间元素调整到正确的位置。
这是一种比较便利的实现方式,无需额外的元素辅助定位,同时兼容性也比较优秀。但有一个缺点就是该布局方式只能实现左右两列宽度固定,中间自适应这一种三列布局,灵活性不强。
2. 其实,也可以试试利用 BFC
See the Pen bfc-three-columns by xal821792703 ( @honoka ) on CodePen .
昨天的《 CSS 布局实例系列(二)如何通过 CSS 实现一个左边固定宽度、右边自适应的两列布局 》已经谈到了利用 BFC 原理实现多列布局的方法。BFC 元素不会与浮动元素叠加,自然也可以利用 BFC 原理完成这个实例。
- 同样的左右两列元素优先渲染,并分别左右浮动。
- 接下来将中间元素设置 overflow: hidden; 成为 BFC 元素块,不与两侧浮动元素叠加,则自然能够插入自己的位置啦。
3. 接下来就尝试一下大名鼎鼎的双飞翼布局吧
See the Pen grid-three-columns by xal821792703 ( @honoka ) on CodePen .
双飞翼是由淘宝玉伯等前端大牛提出的一种多列布局方法,主要利用了浮动、负边距、相对定位三个布局属性,使三列布局就像小鸟一样,拥有中间的身体和两侧的翅膀。
接下来就简单介绍一下双飞翼的实现过程:
- 假设我们现在需要一个如实例说明一样的三列布局,写出如下 div 结构:
<div class="grid"> <div id="div-middle-02"><span>div-middle</span></div> <div id="div-left-02"><span>div-left</span></div> <div id="div-right-02"><span>div-right</span></div></div>
- 首先我们将中间元素放在文档流最前面优先渲染,然后使其向左浮动,并设置 width 为 100%:
#div-middle-02 { float: left; background-color: #fff9ca; width: 100%; height: 50px;}
中间元素直接占满全列,形成小鸟的身体。
- 接下来我们开始为小鸟加上双翼,将左右两列元素均设为左浮动,然后通过调整负边距将其定位在各自的位置上:
#div-middle-02 { float: left; background-color: #fff9ca; width: 100%; height: 50px;}#div-left-02 { float: left; background-color: red; width: 150px; margin-left: -100%; height: 50px;}#div-right-02 { float: left; background-color: yellow; width: 200px; margin-left: -200px; height: 50px;}
看起来,双翼安装成功啦。
-
这样三列布局就大功告成了?No,no,no,仔细看看上面的效果图,可以发现 div-middle 的字块消失了。这是因为通过负边距调整浮动元素位置时,会产生层叠的效果,上面的布局其实只是左右两列元素分别定位在自己的位置上并覆盖中间元素的那部分而已,中间元素的定位并未成功。中间元素要怎样定位在自己的位置上呢?小鸟的身体不是还缺少骨架嘛,那么我们在小鸟体内加上骨架吧:
<div id="div-middle-02"> <div id="middle-wrap-02"><span>div-middle</span></div></div>
在中间元素中再增加一层包裹,通过这层骨架我们就可以方便地控制小鸟身体的位置啦,方法就是调整骨架的左右边距,使其分别等于左右两列的宽度:
#div-middle-02 { float: left; background-color: #fff9ca; width: 100%; height: 50px;}#middle-wrap-02 { margin: 0 200px 0 150px;}#div-left-02 { float: left; background-color: red; width: 150px; margin-left: -100%; height: 50px;}#div-right-02 { float: left; background-color: yellow; width: 200px; margin-left: -200px; height: 50px;}
好啦,一个左右定宽,中间自适应的三列布局以双飞翼的方式成功完成。
- 总结整个过程,就是先放好身体,再加上翅膀,然后让身体包裹一层骨架,通过骨架将身体定位到正确的位置。这就是双飞翼布局的完全体吗?当然不是,接下来我们要请出大杀器相对布局啦,就像小鸟可以通过各种不同的姿势飞翔一般,通过 position: relative; 双飞翼可以实现任意的三列或双列布局。本实例加上相对定位,便成为了这样的完全体:
#div-middle-02 { float: left; background-color: #fff9ca; width: 100%; height: 50px;}#middle-wrap-02 { margin: 0 200px 0 150px;}#div-left-02 { float: left; position: relative; background-color: red; width: 150px; margin-left: -100%; height: 50px;}#div-right-02 { float: left; position: relative; background-color: yellow; width: 200px; margin-left: -200px; height: 50px;}
- 双飞翼能够兼容到 IE6,其可以实现的各种布局在此便不作展开了,有兴趣可以参考玉伯分享的 DEMO
4. 跟上潮流,试试 flex
See the Pen flex-three-columns by xal821792703 ( @honoka ) on CodePen .
看完了强大的双飞翼布局,是不是已经心急火燎地想亲手试试啦。别急,客官,再听我唠唠 CSS3 的新布局 flex 呗。先让我说明一下上面的 DEMO 中是怎样实现本次实例的:
- 设计一个弹性容器包裹需定位的三个元素,然后将该弹性容器的排列属性设为水平排列(flex-flow: row)
- 现在三个元素已经是三列布局了,再将三列元素分别设定一下宽度就行了,左右元素设定为定宽,自适应的中间元素设定为 100%。
.flex { display: flex; flex-flow: row;}#div-left-03 { background-color: red; width: 150px; height: 50px;}#div-middle-03 { background-color: #fff9ca; width: 100%; height: 50px;}#div-right-03 { background-color: yellow; width: 200px; height: 50px;}
效果如下图:
- 搞定收工!大哥你瞪着我是怎么回事儿?~ 什么?效果不对?我的代码怎么可能不对?!~ 哎呦,别打我,我马上检查(哭)好吧,宽度不对,左右两侧的宽度均不符合设定的定值。什么情况呢?原来在 flex 布局中不能将被定位的元素宽度或高度设定为 100%,这样会影响其他定值大小的元素。那么该如何设置中间元素的宽度呢,flex: 1; 即可,可以试一下 DEMO 中去掉注释与不去掉的区别。
- 最后简单介绍一下 flex:flex 是 CSS3 的一种弹性容器布局,通过 flex,几行简单的 CSS 语句便可以实现各种布局(对!我就是 flex NC粉~被拍飞~)。那么 flex 有什么缺点呢?对,就是兼容性!
- 所以在使用 flex 的时候还请注意是否兼容当前浏览器,是否需要 -webkit- 标签。flex 的具体语法和各类实例因为篇(lan)幅(de)过(xie)多的原因,也不做过多介绍了,可以参考阮一峰老师的博文:
Flex 布局教程:语法篇
Flex 布局教程:实例篇
最后感谢大家的阅读,欢迎前往我的 repo 查看源代码整理,有任何问题也请尽情向我吐槽。

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment