问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

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

创作时间:
作者:
@小白创作中心

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

引用
CSDN
1.
https://blog.csdn.net/qq_41988669/article/details/144752984

弹性盒子布局(Flexbox)是CSS3提供的一种新的布局方式,它能够更方便地实现复杂的页面布局。本文将详细介绍Flexbox布局的基本概念、各个相关属性的使用方法以及具体的应用示例。

弹性盒子

flex

什么是弹性布局?

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

弹性布局的特点?

弹性布局具有以下特点:

  1. 主轴与交叉轴:弹性容器具有主轴(main axis)和交叉轴(cross axis)。默认情况下,主轴是水平方向,交叉轴是垂直方向。
  2. 弹性容器:通过将父元素的 display 属性设置为 flexinline-flex 来创建弹性容器。
  3. 子元素的弹性项目:弹性容器中的每个子元素都成为弹性项目。子元素可以指定各自在主轴和交叉轴上的大小、顺序以及对齐方式等。
  4. 主轴对齐:弹性项目可以在主轴上按照一定比例分配空间,也可以使用 justify-content 属性定义主轴的对齐方式。
  5. 交叉轴对齐:弹性项目可以在交叉轴上进行对齐,包括顶部对齐、底部对齐、居中对齐等,使用 align-items 属性定义交叉轴对齐方式。
  6. 换行与自动调整:可控制弹性项目是否换行,并且具备自动调整元素大小的能力。

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

justify-content

justify-content 属性定义了项目在主轴上的对齐方式。主要有以下五种对齐方式:

  • 左对齐
  • 右对齐
  • 居中对齐
  • 两端对齐
  • 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(默认值):左对齐

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

效果

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: 居中

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

效果

space-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: 左右两端对齐

<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 属性定义了项目在交叉轴上的对齐方式。主要有以下五种对齐方式:

  • 起点对齐
  • 终点对齐
  • 中点对齐
  • 基线对齐
  • 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>

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

flex 属性

flex 属性是 flex-grow, flex-shrinkflex-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 属性完全一致。

效果

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号