上一篇介绍了flex弹性盒子的语法,本篇用flex来实际布局一下。例如我们以前会用inline-block或float配合%百分比来实现自适应的三列等高布局。但margin / padding计算起来比较复杂,加加减减维护起来很麻烦。用flex弹性盒模型就简单多了。
首先弄出原始的HTML结构,左右侧边栏定宽220px
* { margin: 0; padding: 0; }#header, #footer { width: 100%; }#left, #right { width: 220px; }<div id="header">header</div><div id="page"> <div id="main">main</div> <div id="left">left</div> <div id="right">right</div></div><div id="footer">footer</div>
现在将内容容器(#page)设成flex弹性盒模型:#page { display: flex; }
中间main就是个普通的div,因此宽度好像没有自适应,很简单设#main { flex: auto; }即可。因为两个侧边栏没有设flex,因此剩余的宽度将被main独享
main的位置不对,调整位置非常简单,设置#main { … order: 1; }即可。当然别忘了同步给right设#right { … order: 2; }。left不设默认为0。
你可能会疑惑,直接在DOM按left->main->right顺序排列不就行了,为何排列成main->left->right?确实调整DOM顺序也能达到同样效果,但将main越置前,对SEO搜索越友好。这其实并无标准答案,如果你觉得left的内容同样重要,那left->main->right的DOM顺序也是没问题的。
现在只剩最后一步,将footer置底并拉升page部分。先将html和body的height设成100%,为全屏做准备。由于body里包含了header,page,footer,因此为body设置flex弹性盒模型,同时设置flex-direction: column;让3个子元素垂直排列body { … display: flex; flex-direction: column; }
最后将page拉伸就简单了,思路和上面main一样,只要给page设置flex:auto;,因为header和footer没有设flex,因此剩余的高度将被page独享。
* { margin: 0; padding: 0; }html, body { height: 100%; }body { display: flex; flex-direction: column;}#header, #footer { width: 100%; }#page { display: flex; flex:auto;}#left, #right { width: 220px; }#right { order: 2; }#main { flex: auto; order: 1;}
总结一下自适应三列等高布局的思路,body应用垂直的flex模型,让页头,页面,页脚垂直排列,其中只有页面部分有flex:auto;将自适应高度。页面部分应用flex模型,内容,左侧栏,右侧栏将水平排列,其中只有内容部分有flex:auto;将自适应宽度。如果为了SEO优化,在DOM中将内容部分放在左右侧边栏上面的话,通过order调整顺序。
更多布局的例子,可以看看Solved by Flexbox
用flex弹性盒子可以轻松实现等比例布局:
.Grid { display: flex; }.Grid-cell { flex: 1; }<div class="Grid"> <div class="Grid-cell">…</div> <div class="Grid-cell">…</div> <div class="Grid-cell">…</div></div>
也可以实现部分固定比例,剩余部分自适应布局:
评论区布局:
代码页面上都有,极其简单,可以自行参考。
总结
flex用于布局真的非常方便,原先用inline-block,float写的一堆既丑且难维护的代码,用flex可以很优雅地实现,什么垂直居中都是浮云。唯一需要的只是时间,等那些旧式浏览器都死透了,弹性盒子的春天就来了。

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.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools