Skip to Content
CSS:权威指南,第 5 版
book

CSS:权威指南,第 5 版

by Eric Meyer, Estelle Weyl
May 2025
Intermediate to advanced
1128 pages
15h 21m
Chinese
O'Reilly Media, Inc.
Content preview from CSS:权威指南,第 5 版

第 11 章 灵活方框布局 灵活的方框布局

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

CSS Flexible Box Module Level 1,简称flexbox,让曾经困难重重的某些页面、小工具、应用程序和图库的布局任务变得几乎简单。 有了 flexbox,你往往不需要 CSS 框架。 在本章中,你将学会如何使用几行 CSS 来创建网站所需的几乎所有功能。

Flexbox 基础知识

Flexbox是一种简单而强大的页面组件布局方式,它可以决定空间分布、内容对齐和元素视觉排序的方式。内容可以轻松地纵向或横向排列,可以沿单一轴线布局,也可以多行排列。 还有更多

使用 flexbox,内容的外观可以不受源代码顺序的影响。虽然在视觉上有所改变,但 flex 属性不应影响屏幕阅读器阅读内容的顺序。

警告

规范规定屏幕阅读器应遵循源代码顺序,但截至 2022 年底,Firefox 遵循的是视觉顺序。截至本文撰写之时,有一项提案要求添加一个 CSS 属性,用于指定是遵循源代码顺序还是视觉顺序,因此不久就可以自行决定了。

也许最重要的是,有了灵活的盒式模块布局,就可以使元素在不同屏幕尺寸和不同显示设备上的表现具有可预见性。Flexbox 与响应式网站配合得非常好,因为当提供的空间增大或减小时,内容的大小也会随之增大或减小。

Flexbox 的工作原理是父子关系。通过在元素上声明display: flexdisplay: inline-flex ,即可激活 Flexbox 布局。 元素将成为一个柔性容器,在提供的空间内安排其子元素并控制它们的布局。该柔性容器的子元素则成为柔性项。 请看下面的样式和标记,如图 11-1 所示:

div#one {display: flex;}
div#two {display: inline-flex;}
div {border: 1px dashed; background: silver;}
div > * {border: 1px solid; background: #AAA;}
div p {margin: 0;}
<div id="one">
    <p>flex item with<br>two longer lines</p>
    <span>flex item</span>
    <p>flex item</p>
</div>
<div id="two">
    <span>flex item with<br>two longer lines</span>
    <span>flex item</span>
    <p>flex item</p>
</div>
Adding display: flex; or display: inline-flex creates a flex container
图 11-1. 两种柔性容器
提示

查找 "播放 "符号 了解在线示例的可用性。本章的所有示例均可在 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
学习 React,第二版

学习 React,第二版

Alex Banks, Eve Porcello

Publisher Resources

ISBN: 9798341657021