当前位置: 首页 > news >正文

弹性盒子(display: flex)布局超全讲解|Flex 布局教程

文章目录

  • 弹性盒子
    • flex
      • 什么是弹性布局?
      • 弹性布局的特点?
      • justify-content
      • align-item
      • flex-direction (主轴的方向:水平或者垂直)
      • flex-wrap
      • flex-flow
      • align-content
      • flex-grow 属性
      • flex-shrink 属性
      • flex-basis 属性
      • flex 属性
      • align-self 属性

弹性盒子

flex

什么是弹性布局?

弹性布局(Flex 布局)是一种现代的 CSS 布局方式,通过使用 display: flex 属性来创建一个弹性容器,并在其中使用灵活的盒子模型来进行元素的排列和定位。

弹性布局的特点?

弹性布局具有以下特点:

1、主轴与交叉轴:弹性容器具有主轴(main axis)和交叉轴(cross axis)。默认情况下,主轴是水平方向,交叉轴是垂直方向。

2、弹性容器:通过将父元素的 display 属性设置为 flex 或 inline-flex 来创建弹性容器。
子元素的弹性项目:弹性容器中的每个子元素都成为弹性项目。子元素可以指定各自在主轴和交叉轴上的大小、顺序以及对齐方式等。

3、主轴对齐:弹性项目可以在主轴上按照一定比例分配空间,也可以使用 justify-content 属性定义主轴的对齐方式。

4、交叉轴对齐:弹性项目可以在交叉轴上进行对齐,包括顶部对齐、底部对齐、居中对齐等,使用 align-items 属性定义交叉轴对齐方式。

5、换行与自动调整:可控制弹性项目是否换行,并且具备自动调整元素大小的能力。

弹性布局简化了网页布局的开发过程,提供了更灵活、响应式的布局方式。它适用于各种屏幕尺寸和设备类型,并能够快速适应不同的布局需求。

在这里插入图片描述

justify-content

justify-content属性定义了项目在主轴上的对齐方式。(内容在主轴5种对方式:左对齐、右对齐、居中对齐、两端对齐、space-around[两端间距对齐])

flex-start(默认值):左对齐flex-end:右对齐center: 居中space-between:两端对齐,项目之间的间隔都相等。space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

初始代码

<style>.father {width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;margin: 100px 100px;padding-top: 25px;box-sizing: border-box;}.demo {width: 100px;height: 50px;line-height: 50px;font-size: 20px;font-weight: bold;text-align: center;background-color: rgb(135, 135, 255);border-radius: 4px;}
</style><div class="father"><div class="demo">1</div><div class="demo">2</div><div class="demo">3</div>
</div>

效果
在这里插入图片描述

代码加弹性盒子:(display: flex)

<style>.father {display: flex;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;}
</style>

效果

在这里插入图片描述

flex-start(默认值):左对齐

代码加弹性盒子:(display: flex)、左对齐:(justify-content: flex-start)

<style>.father {display: flex;justify-content: flex-start;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;}
</style>

效果

在这里插入图片描述

flex-end:右对齐

代码加弹性盒子:(display: flex)、右对齐:(justify-content: flex-end)

<style>.father {display: flex;justify-content: flex-end;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;}
</style>

效果
在这里插入图片描述

center: 居中

代码加弹性盒子:(display: flex)、居中对齐:(justify-content: center)

<style>.father {display: flex;justify-content: center;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;}
</style>

效果
在这里插入图片描述

space-between: 两端对齐

代码加弹性盒子:(display: flex)、两端对齐:(justify-content: between)

<style>.father {display: flex;justify-content: space-between;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;}
</style>

效果
在这里插入图片描述

space-between: 两端间距对齐

代码加弹性盒子:(display: flex)、两端间距对齐:(justify-content: space-around)

<style>.father {display: flex;justify-content: space-around;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;}
</style>

效果
在这里插入图片描述

align-item

align-item 属性定义了项目在交叉轴上的对齐方式。(内容在主轴5种对方式:起点对齐、终点对齐、中点对齐、基线对齐、stretch(默认值)

flex-start:交叉轴的起点对齐。flex-end:交叉轴的终点对齐。center:交叉轴的中点对齐。baseline: 项目的第一行文字的基线对齐。stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

flex-start:交叉轴的起点对齐。

<style>.father {display: flex;align-items: flex-start;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;margin: 100px 100px;padding: 0 25px;box-sizing: border-box;}.demo {width: 100px;height: 50px;line-height: 50px;font-size: 20px;font-weight: bold;text-align: center;background-color: rgb(135, 135, 255);border-radius: 4px;}
</style><div class="father"><div class="demo">1</div><div class="demo">2</div><div class="demo">3</div>
</div>

效果
在这里插入图片描述

center:交叉轴的终点对齐

<style>.father {display: flex;align-items: flex-end;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;margin: 100px 100px;padding: 0 25px;box-sizing: border-box;}
</style>

效果
在这里插入图片描述

center:交叉轴的中点对齐

<style>.father {display: flex;align-items: center;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;margin: 100px 100px;padding: 0 25px;box-sizing: border-box;}
</style>

效果
在这里插入图片描述

baseline: 项目的第一行文字的基线对齐

<style>.father {display: flex;align-items: baseline;width: 500px;height: 100px;background-color: rgb(255, 199, 199);border-radius: 10px;margin: 100px 100px;padding: 0 25px;box-sizing: border-box;}
</style>

效果
在这里插入图片描述

stretch(默认值):如果项目未设置高度或设为 auto,将占满整个容器的高度。

<style>.father {display: flex;align-items: stretch;width: 300px;background-color: rgb(255, 199, 199);border-radius: 10px;margin: 100px 100px;padding: 0 25px;box-sizing: border-box;}
</style><div class="father"><div class="demo">1</div><div class="demo">2</div><div class="demo">3</div>
</div>

效果
在这里插入图片描述

flex-direction (主轴的方向:水平或者垂直)

<style>.box {flex-direction: row | row-reverse | column | column-reverse;}
</style>

row(默认值):主轴为水平方向,起点在左端。

row-reverse:主轴为水平方向,起点在右端。

column:主轴为垂直方向,起点在上沿。

column-reverse:主轴为垂直方向,起点在下沿。

效果
在这里插入图片描述

flex-wrap

换行不换行以及换行的方向

<style>.box {flex-wrap: nowrap | wrap | wrap-reverse;}
</style>

nowrap(默认):不换行

wrap:换行,第一行在上方。

wrap-reverse:换行,第一行在下方。

效果
在这里插入图片描述

flex-flow

以上两种的简写方式

<style>.box {flex-flow: <flex-direction> || <flex-wrap>;}
</style>

align-content

align-content 属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

<style>.box {align-content: flex-start | flex-end | center | space-between |space-around | stretch;}
</style>

flex-start:与交叉轴的起点对齐。

flex-end:与交叉轴的终点对齐。

center:与交叉轴的中点对齐。

space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。

space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。

stretch(默认值):轴线占满整个交叉轴。

效果
在这里插入图片描述

order 属性
order 属性定义项目的排列顺序。数值越小,排列越靠前,默认为 0。

<style>.item {order: <integer>;}
</style>

效果
在这里插入图片描述

flex-grow 属性

flex-grow 属性定义项目的放大比例,默认为 0,即如果存在剩余空间,也不放大。

<style>.item {flex-grow: <number>; /* default 0 */}
</style>

如果所有项目的 flex-grow 属性都为 1,则它们将等分剩余空间(如果有的话)。如果一个项目的 flex-grow 属性为 2,其他项目都为 1,则前者占据的剩余空间将比其他项多一倍。

效果
在这里插入图片描述

flex-shrink 属性

flex-shrink 属性定义了项目的缩小比例,默认为 1,即如果空间不足,该项目将缩小。

<style>.item {flex-shrink: <number>; /* default 1 */}
</style>

如果所有项目的 flex-shrink 属性都为 1,当空间不足时,都将等比例缩小。如果一个项目的 flex-shrink 属性为 0,其他项目都为 1,则空间不足时,前者不缩小。

效果
在这里插入图片描述

负值对该属性无效。

flex-basis 属性

flex-basis 属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为 auto,即项目的本来大小。

<style>.item {flex-basis: <length> | auto; /* default auto */}
</style>

它可以设为跟 width 或 height 属性一样的值(比如 350px),则项目将占据固定空间。

flex 属性

flex 属性是 flex-grow, flex-shrink 和 flex-basis 的简写,默认值为 0 1 auto。后两个属性可选。

<style>.item {flex: none | [ < 'flex-grow' > < 'flex-shrink' >? || < 'flex-basis' >];}
</style>

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。

建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。

align-self 属性

align-self 属性允许单个项目有与其他项目不一样的对齐方式,可覆盖 align-items 属性。默认值为 auto,表示继承父元素的 align-items 属性,如果没有父元素,则等同于 stretch。

<style>.item {align-self: auto | flex-start | flex-end | center | baseline | stretch;}
</style>

该属性可能取 6 个值,除了 auto,其他都与 align-items 属性完全一致。

效果
在这里插入图片描述

http://www.lryc.cn/news/510309.html

相关文章:

  • 无问社区-无问AI模型
  • 如何记录日常笔记
  • 【魅力golang】之-反射
  • git--批量修改本地用户名和邮箱
  • Rofin罗芬激光PowerLine L300 PL400 Manual 软件
  • 【 thefuck 安装与使用】Linux 终端自动纠错工具:一头GitHub上的“草泥马“ - thefuck,妈妈再也不用担心我打错命令行了!
  • 牛客网刷题 ——C语言初阶——BC112小乐乐求和
  • 【PyTorch】(基础七)---- 完整训练流程
  • 01- 三自由度串联机械臂位置分析
  • Flutter实现可拖拽操作Draggable
  • Vue BPMN Modeler流程图
  • 写在公司40周年前夕
  • Python调用Elasticsearch更新数据库
  • 测试基础之测试分类
  • 太阳能LED路灯智能控制系统(论文+源码)
  • 文本数据处理
  • Liunx环境下安装人大金仓数据库V8R6版本
  • Android使用PorterDuffXfermode模式PorterDuff.Mode.SRC_OUT橡皮擦实现马赛克效果,Kotlin(3)
  • python 怎么引入类
  • Day35汉明距离
  • 中文学习系统:客户服务与学习支持
  • 华为麦芒5(安卓6)termux记录 使用ddns-go,alist
  • 餐厅下单助手系统(Java+MySQL)
  • Go操作MySQL
  • Linux(Ubuntu/CentOS)配置开机自启动服务
  • springboot3版本结合knife4j生成接口文档
  • 谈谈 Wi-Fi 的 RTS/CTS 设计
  • JVM 详解
  • 【debug】
  • PCB注意事项