Table of Contents
总结
Home Web Front-end HTML Tutorial CSS3 flex布局应用介绍_html/css_WEB-ITnose

CSS3 flex布局应用介绍_html/css_WEB-ITnose

Jun 21, 2016 am 08:45 AM

上一篇介绍了flex弹性盒子的语法,本篇用flex来实际布局一下。例如我们以前会用inline-block或float配合%百分比来实现自适应的三列等高布局。但margin / padding计算起来比较复杂,加加减减维护起来很麻烦。用flex弹性盒模型就简单多了。

首先弄出原始的HTML结构,左右侧边栏定宽220px

1

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

Copy after login

现在将内容容器(#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独享。

1

* { 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;}

Copy after login

总结一下自适应三列等高布局的思路,body应用垂直的flex模型,让页头,页面,页脚垂直排列,其中只有页面部分有flex:auto;将自适应高度。页面部分应用flex模型,内容,左侧栏,右侧栏将水平排列,其中只有内容部分有flex:auto;将自适应宽度。如果为了SEO优化,在DOM中将内容部分放在左右侧边栏上面的话,通过order调整顺序。

更多布局的例子,可以看看Solved by Flexbox

用flex弹性盒子可以轻松实现等比例布局:

1

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

Copy after login

也可以实现部分固定比例,剩余部分自适应布局:

评论区布局:

代码页面上都有,极其简单,可以自行参考。

总结

flex用于布局真的非常方便,原先用inline-block,float写的一堆既丑且难维护的代码,用flex可以很优雅地实现,什么垂直居中都是浮云。唯一需要的只是时间,等那些旧式浏览器都死透了,弹性盒子的春天就来了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

See all articles