CSS 的弹性布局

简介: CSS弹性布局通过`display:flex`实现,可灵活控制容器内子元素的排列、对齐与分布。支持主轴与交叉轴方向设置、伸缩比例(flex-grow/shrink)、换行及多行对齐方式,大幅提升网页布局灵活性与响应性。

#CSS 的弹性布局

在此之前,我们写的页面上元素是按照先后顺序排列的,块元素总是占据一行,不受我们控制。 例如之前 语义化 章节中的示例代码中,aside 块作为侧边栏,却并没有显示在侧边,而是单独占据一行。

本节将学习 CSS 中最常用的布局方式——弹性布局,它可以方便地控制容器内项目的排列、对齐和分布方式。

通过将一个元素样式的 display 属性设为 flex,可以将该元素设为弹性布局的 容器, 容器的直接子元素将不再占据一行。

示例:

<div style="display:flex; height:100px;">
    <main style="background-color:#212121; color:cyan;">
        main
    </main>
    <aside style="background-color:#666; color:yellow;">
        aside
    </aside>
</div>

#主轴和交叉轴

弹性容器中,默认水平方向为主轴,竖直方向为交叉轴,子元素在主轴上依次排列。

可以通过容器的 flex-direction 属性改变方向:

  • row - 水平方向为主轴,竖直方向为交叉轴
  • column - 竖直方向为主轴,水平方向为交叉轴

示例:

<div style="display:flex; flex-direction:column; height:100px;">
    <main style="background-color:#212121; color:cyan;">
        main
    </main>
    <aside style="background-color:#666; color:yellow;">
        aside
    </aside>
</div>

这个示例将弹性布局的主轴设为了数值方向;相应的,交叉轴为水平方向。

#flex-grow 和 flex-shrink

当子元素的总长度小于主轴长度时,可以给子元素的样式添加 flex-grow 使其长度增加。

这个属性的值表示当弹性容器没有被占满时,子元素增加的长度占剩余长度的比例。

下面这个示例将 <main> 元素的 flex-grow 值设为 5,<aside>flex-grow 元素设为 1。

因此剩余空间的 将分配给 <main> 元素, aside 元素。

<div style="display:flex; height:100px;">
    <main style="flex-grow:5; background-color:#212121; color:cyan;">
        main
    </main>
    <aside style="flex-grow:1; background-color:#666; color:yellow;">
        aside
    </aside>
</div>

相似的,当子元素的总长度大于主轴长度时,可以给子元素的样式添加 flex-shrink 使其长度减少。

这个属性的值表示当弹性容器内容溢出时,子元素减少的长度占溢出长度的比例。

#主轴上的对齐方式

默认情况下,子元素在容器主轴上向起始边缘对齐,可以通过容器的 justify-content 属性修改对齐方式。

  • start - 子元素在容器主轴上向起始边缘对齐
  • end - 子元素在容器主轴上向末端边缘对齐
  • center - 子元素在容器主轴上居中对齐
  • space-between - 子元素在容器主轴均匀分布,两端不留空
  • space-around - 子元素在容器主轴均匀分布,两端留空为间隔的一半
  • space-evenly - 子元素在容器主轴均匀分布,两端留空和间隔一致

下面的示例可以清晰的查看这六种对齐方式的差异。

<div style="display:flex; justify-content:start; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; justify-content:end; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; justify-content:center; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; justify-content:space-between; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; justify-content:space-around; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; justify-content:space-evenly; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

#交叉轴上的对齐方式

默认情况下,子元素在容器交叉轴上两端对齐,即占满整个交叉轴。可以通过容器的 align-items 属性修改对齐方式。

  • stretch - 子元素在容器交叉轴上两端对齐
  • start - 子元素在容器交叉轴上向起始边缘对齐
  • end - 子元素在容器交叉轴上向末端边缘对齐
  • center - 子元素在容器交叉轴上居中对齐

下面的示例可以清晰的查看这四种对齐方式的差异。

<div style="display:flex; align-items:stretch; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; align-items:start; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; align-items:end; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

<div style="display:flex; align-items:center; height:100px; background-color:#212121;">
    <div style="background-color:red;color:white;">item</div>
    <div style="background-color:green;color:white;">item</div>
    <div style="background-color:blue;color:white;">item</div>
</div>

#换行

默认情况下,当子元素的总长度大于主轴长度时,子元素会溢出显示。 可以通过容器的 flex-wrap 属性让溢出的元素换行。

  • nowrap - 不换行
  • wrap - 当空间不足时自动换行
  • wrap-reverse - 当空间不足时自动换行,并且行的顺序逆转

下面的示例中容器宽度在不断变化,可以清晰的查看这三种方式的差异。

<div style="display:flex; align-items:center; flex-wrap:nowrap; background-color:#212121;">
    <div style="background-color:red;color:white; width:80px;">item</div>
    <div style="background-color:green;color:white; width:80px;">item</div>
    <div style="background-color:blue;color:white; width:80px;">item</div>
    <div style="background-color:orange;color:white; width:80px;">item</div>
    <div style="background-color:purple;color:white; width:80px;">item</div>
    <div style="background-color:cyan;color:white; width:80px;">item</div>
</div>

<div style="display:flex; align-items:center; flex-wrap:wrap; background-color:#212121;">
    <div style="background-color:red;color:white; width:80px;">item</div>
    <div style="background-color:green;color:white; width:80px;">item</div>
    <div style="background-color:blue;color:white; width:80px;">item</div>
    <div style="background-color:orange;color:white; width:80px;">item</div>
    <div style="background-color:purple;color:white; width:80px;">item</div>
    <div style="background-color:cyan;color:white; width:80px;">item</div>
</div>

<div style="display:flex; align-items:center; flex-wrap:wrap-reverse; background-color:#212121;">
    <div style="background-color:red;color:white; width:80px;">item</div>
    <div style="background-color:green;color:white; width:80px;">item</div>
    <div style="background-color:blue;color:white; width:80px;">item</div>
    <div style="background-color:orange;color:white; width:80px;">item</div>
    <div style="background-color:purple;color:white; width:80px;">item</div>
    <div style="background-color:cyan;color:white; width:80px;">item</div>
</div>

#行在交叉轴上的对齐方式

通过容器的 align-content 属性设置行在交叉轴上的对齐方式。

  • start - 行在容器交叉轴上向起始边缘对齐
  • end - 行在容器交叉轴上向末端边缘对齐
  • center - 行在容器交叉轴上居中对齐
  • space-between - 行在容器交叉轴上均匀分布,两端不留空
  • space-around - 行在容器交叉轴上均匀分布,两端留空为间隔的一半
  • space-evenly - 行在容器交叉轴上均匀分布,两端留空为和间隔一致

下面的示例可以清晰的查看这六种对齐方式的差异。

<div style="display:flex; align-content:start; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
    <div style="background-color:red;color:white; width:40px;">item</div>
    <div style="background-color:green;color:white; width:40px;">item</div>
    <div style="background-color:blue;color:white; width:40px;">item</div>
</div>

<div style="display:flex; align-content:end; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
    <div style="background-color:red;color:white; width:40px;">item</div>
    <div style="background-color:green;color:white; width:40px;">item</div>
    <div style="background-color:blue;color:white; width:40px;">item</div>
</div>

<div style="display:flex; align-content:center; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
    <div style="background-color:red;color:white; width:40px;">item</div>
    <div style="background-color:green;color:white; width:40px;">item</div>
    <div style="background-color:blue;color:white; width:40px;">item</div>
</div>

<div style="display:flex; align-content:space-between; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
    <div style="background-color:red;color:white; width:40px;">item</div>
    <div style="background-color:green;color:white; width:40px;">item</div>
    <div style="background-color:blue;color:white; width:40px;">item</div>
</div>

<div style="display:flex; align-content:space-around; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
    <div style="background-color:red;color:white; width:40px;">item</div>
    <div style="background-color:green;color:white; width:40px;">item</div>
    <div style="background-color:blue;color:white; width:40px;">item</div>
</div>

<div style="display:flex; align-content:space-evenly; align-items:start; flex-wrap:wrap; height:100px; width:80px; background-color:#212121;">
    <div style="background-color:red;color:white; width:40px;">item</div>
    <div style="background-color:green;color:white; width:40px;">item</div>
    <div style="background-color:blue;color:white; width:40px;">item</div>
</div>

CSS 的弹性布局》 是转载文章,点击查看原文

相关文章
|
前端开发 JavaScript
前端知识(十)———JavaScript 使用URL跳转传递数组对象数据类型的方法
前端知识(十)———JavaScript 使用URL跳转传递数组对象数据类型的方法
588 0
|
小程序
微信小程序中识别HTML标签的方法
微信小程序中识别HTML标签的方法
|
前端开发 JavaScript 开发者
掌握 CSS 弹性布局(Flexbox):构建复杂页面布局的高效秘籍与实战案例
CSS弹性布局(Flexbox)是现代网页设计中构建复杂页面布局的高效工具。本文将深入浅出地介绍Flexbox的核心概念、使用技巧及实际应用案例,帮助读者快速掌握这一强大布局方法。
|
缓存 安全 数据安全/隐私保护
如何根据请求场景选择 GET 或 POST 请求方法?
【10月更文挑战第27天】根据不同的请求场景,综合考虑数据传输目的、安全性、数据量大小、幂等性要求以及缓存需求等因素,合理地选择GET或POST请求方法,能够更好地实现客户端与服务器之间的数据交互,提高系统的性能和安全性。
572 64
|
前端开发 安全 UED
React 文件预览组件:File Preview
在现代Web应用中,文件上传和预览功能至关重要。本文基于React库,详细介绍如何构建文件预览组件,涵盖文件选择器、图片预览、文件大小限制及多种文件类型支持。通过实际代码示例,解析常见问题如跨域请求、文件路径处理和状态管理,并提供解决方案。帮助开发者提升用户体验,减少误操作。
871 2
|
前端开发 开发者 容器
彻底学会CSS 弹性布局flex
【4月更文挑战第1天】 彻底学会CSS 弹性布局flex
313 0
|
前端开发 容器
css布局-弹性布局学习笔记
这篇文章是关于CSS弹性布局的学习笔记,详细介绍了flex容器和元素的相关属性,包括flex-direction、flex-wrap、flex-flow、justify-content、align-items、align-content以及order、flex-grow、flex-shrink、flex-basis、flex和align-self等,解释了这些属性在弹性盒子布局中的作用和用法。
|
前端开发 容器
CSS Flexbox(弹性布局)
CSS Flexbox(弹性布局)
756 2
|
JavaScript
基于Vue2.X对WangEditor 5富文本编辑器进行封装与使用,支持单个或多个图片点击、粘贴、拖拽上传,Vue3.X项目也可直接使用
这篇文章介绍了如何在Vue 2.X项目中封装和使用WangEditor 5富文本编辑器,支持图片的点击、粘贴和拖拽上传,同时提到封装的组件也适用于Vue 3.X项目,并提供了详细的使用示例和后端配置。
2023 1
基于Vue2.X对WangEditor 5富文本编辑器进行封装与使用,支持单个或多个图片点击、粘贴、拖拽上传,Vue3.X项目也可直接使用
|
机器学习/深度学习 存储 算法
【博士每天一篇论文-算法】Continual Learning Through Synaptic Intelligence,SI算法
本文介绍了一种名为"Synaptic Intelligence"(SI)的持续学习方法,通过模拟生物神经网络的智能突触机制,解决了人工神经网络在学习新任务时的灾难性遗忘问题,并保持了计算效率。
924 1
【博士每天一篇论文-算法】Continual Learning Through Synaptic Intelligence,SI算法