CSS Flexbox 布局

详细介绍CSS Flexbox布局model, includingcontainersproperty and projectproperty, 以及common Flexbox布局模式

1. Flexbox Introduction

Flexbox (弹性盒子) is CSS3引入 一种 new 布局模式, 用于更flexible, 更 high 效地布局, for 齐 and 分配containers in 元素 空间, 即使它们 big small is 未知 or 动态 .

提示

Flexbox布局主要用于一维布局 (行 or 列) , 而CSS Grid则用于二维布局 (行 and 列) . Flexbox 出现解决了传统布局in垂直居in, etc. high 列, flexible分配空间etc.难题.

1.1 Flexbox basicconcepts

Flexbox布局由两个主要部group成:

  1. Flexcontainers (Flex Container) : 设置了display: flex or display: inline-flex 元素, 它 所 has 直接子元素都会成 for Flexproject.
  2. Flexproject (Flex Item) : Flexcontainers 直接子元素, 它们会按照Flexbox规则for排列.

Flexbox basicstructureexample

<div class="flex-container">  
    <div class="flex-item">1</div>  
    <div class="flex-item">2</div>  
    <div class="flex-item">3</div>  
</div>
.flex-container {
    display: flex; /* 将containers设置 for Flexcontainers */
}
1
2
3

2. Flexcontainersproperty

Flexcontainers has 以 under 主要property:

2.1 display

用于将元素定义 for Flexcontainers.

/* 将元素设置 for 块级Flexcontainers */
.flex-container {
    display: flex;
}

/* 将元素设置 for  in 联Flexcontainers */
.flex-container {
    display: inline-flex;
}

2.2 flex-direction

用于定义Flexproject in containersin 排列方向, 决定了主轴 方向.

/* 默认值, 主轴 for 水平方向,  from  left  to  right  */
.flex-container {
    flex-direction: row;
}

/* 主轴 for 水平方向,  from  right  to  left  */
.flex-container {
    flex-direction: row-reverse;
}

/* 主轴 for 垂直方向,  from  on  to  under  */
.flex-container {
    flex-direction: column;
}

/* 主轴 for 垂直方向,  from  under  to  on  */
.flex-container {
    flex-direction: column-reverse;
}

flex-direction example

flex-direction: row; (默认值)

1
2
3

flex-direction: row-reverse;

1
2
3

flex-direction: column;

1
2
3

flex-direction: column-reverse;

1
2
3

2.3 flex-wrap

用于定义Flexproject in 主轴方向 on is 否换行.

/* 默认值, 不换行, project会被压缩 */
.flex-container {
    flex-wrap: nowrap;
}

/* 换行, 第一行 in  on 方 */
.flex-container {
    flex-wrap: wrap;
}

/* 换行, 第一行 in  under 方 */
.flex-container {
    flex-wrap: wrap-reverse;
}

flex-wrap example

flex-wrap: nowrap; (默认值)

1
2
3
4
5
6

flex-wrap: wrap;

1
2
3
4
5
6

flex-wrap: wrap-reverse;

1
2
3
4
5
6

2.4 flex-flow

flex-flow is flex-direction and flex-wrap 简写property.

/* 语法: flex-flow: flex-direction flex-wrap; */
.flex-container {
    flex-flow: row wrap;
    /* etc.同于: 
       flex-direction: row;
       flex-wrap: wrap;
    */
}

2.5 justify-content

用于定义Flexproject in 主轴方向 on for 齐方式.

/* 默认值, project靠主轴起点 for 齐 */
.flex-container {
    justify-content: flex-start;
}

/* project靠主轴终点 for 齐 */
.flex-container {
    justify-content: flex-end;
}

/* project in 主轴 on 居in for 齐 */
.flex-container {
    justify-content: center;
}

/* project之间 间隔相etc., 首尾project and containersedge缘没 has 间隔 */
.flex-container {
    justify-content: space-between;
}

/* project之间 间隔相etc., 首尾project and containersedge缘 间隔 is project之间间隔 一半 */
.flex-container {
    justify-content: space-around;
}

/* project之间 间隔相etc., 首尾project and containersedge缘 间隔也相etc. */
.flex-container {
    justify-content: space-evenly;
}

justify-content example

justify-content: flex-start; (默认值)

1
2
3

justify-content: flex-end;

1
2
3

justify-content: center;

1
2
3

justify-content: space-between;

1
2
3

justify-content: space-around;

1
2
3

justify-content: space-evenly;

1
2
3

2.6 align-items

用于定义Flexproject in 交叉轴方向 on for 齐方式.

/* 默认值, project拉伸以填满交叉轴 */
.flex-container {
    align-items: stretch;
}

/* project靠交叉轴起点 for 齐 */
.flex-container {
    align-items: flex-start;
}

/* project靠交叉轴终点 for 齐 */
.flex-container {
    align-items: flex-end;
}

/* project in 交叉轴 on 居in for 齐 */
.flex-container {
    align-items: center;
}

/* project 基线 for 齐 */
.flex-container {
    align-items: baseline;
}

align-items example

align-items: stretch; (默认值)

1
2
3

align-items: flex-start;

1
2
3

align-items: flex-end;

1
2
3

align-items: center;

1
2
3

align-items: baseline;

1
2
3

2.7 align-content

用于定义 many 行Flexproject in 交叉轴方向 on for 齐方式, 仅 in project换行时 has 效.

/* 默认值, project in 交叉轴 on 拉伸 */
.flex-container {
    align-content: stretch;
}

/* project in 交叉轴 on 靠起点 for 齐 */
.flex-container {
    align-content: flex-start;
}

/* project in 交叉轴 on 靠终点 for 齐 */
.flex-container {
    align-content: flex-end;
}

/* project in 交叉轴 on 居in for 齐 */
.flex-container {
    align-content: center;
}

/* project in 交叉轴 on 均匀分布, 首尾project and containersedge缘没 has 间隔 */
.flex-container {
    align-content: space-between;
}

/* project in 交叉轴 on 均匀分布, 首尾project and containersedge缘 间隔 is project之间间隔 一半 */
.flex-container {
    align-content: space-around;
}

/* project in 交叉轴 on 均匀分布, 首尾project and containersedge缘 间隔也相etc. */
.flex-container {
    align-content: space-evenly;
}

3. Flexprojectproperty

Flexproject has 以 under 主要property:

3.1 order

用于定义Flexproject 排列顺序, 数值越 small , 排列越靠 before , 默认值 for 0.

/* 更改project 排列顺序 */
.flex-item {
    order: 2;
}

order example

.flex-item:nth-child(1) { order: 3; }
.flex-item:nth-child(2) { order: 1; }
.flex-item:nth-child(3) { order: 2; }
1 (order: 3)
2 (order: 1)
3 (order: 2)

3.2 flex-grow

用于定义Flexproject in 剩余空间in 放 big 比例, 默认值 for 0 (不放 big ) .

/* project会根据剩余空间for放 big  */
.flex-item {
    flex-grow: 1;
}

flex-grow example

flex-grow: 1; (所 has project平均分配剩余空间)

1 (flex-grow: 1)
2 (flex-grow: 1)
3 (flex-grow: 1)

不同 flex-grow 值 (project会根据比例分配剩余空间)

1 (flex-grow: 1)
2 (flex-grow: 2)
3 (flex-grow: 1)

3.3 flex-shrink

用于定义Flexproject in 空间不足时 缩 small 比例, 默认值 for 1 (会缩 small ) .

/* project不会缩 small  */
.flex-item {
    flex-shrink: 0;
}

/* project会缩 small  */
.flex-item {
    flex-shrink: 1;
}

flex-shrink example

flex-shrink: 0; (project不会缩 small )

1 (flex-shrink: 0)
2 (flex-shrink: 1)
3 (flex-shrink: 1)

3.4 flex-basis

用于定义Flexproject in 主轴方向 on 初始 big small , 默认值 for auto.

/* 设置project 初始宽度 for 200px */
.flex-item {
    flex-basis: 200px;
}

/* 设置project 初始宽度 for 50% */
.flex-item {
    flex-basis: 50%;
}

3.5 flex

flex is flex-grow, flex-shrink and flex-basis 简写property, 默认值 for 0 1 auto.

/* 语法: flex: flex-grow flex-shrink flex-basis; */
.flex-item {
    flex: 1 1 auto;
}

/* 常用值: flex: 1; etc.同于 flex: 1 1 0%; */
.flex-item {
    flex: 1;
}

提示

建议usingflex简写property, 而不 is 单独设置flex-grow, flex-shrink and flex-basis, 因 for 浏览器会自动计算默认值, 避免一些意 out behavior.

3.6 align-self

用于定义单个Flexproject in 交叉轴方向 on for 齐方式, 会覆盖containers align-itemsproperty.

/* 单个project  for 齐方式 */
.flex-item {
    align-self: center;
}

align-self example

1 (默认)
2 (align-self: flex-start)
3 (align-self: center)
4 (align-self: flex-end)

4. common Flexbox布局模式

4.1 水平居in

.container {
    display: flex;
    justify-content: center;
}

水平居inexample

水平居in in 容

4.2 垂直居in

.container {
    display: flex;
    align-items: center;
    height: 200px; /* 需要设置containers high 度 */
}

垂直居inexample

垂直居in in 容

4.3 水平垂直居in

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 200px; /* 需要设置containers high 度 */
}

水平垂直居inexample

水平垂直居in in 容

4.4 两栏布局 (一侧固定, 一侧自适应)

.container {
    display: flex;
}

.sidebar {
    width: 200px; /* 固定宽度 */
    flex-shrink: 0; /* 防止缩 small  */
}

.main-content {
    flex: 1; /* 自适应宽度 */
}

两栏布局example

侧edge栏 (固定宽度200px)
主 in 容区 (自适应宽度)

4.5 三栏布局 (两侧固定, in间自适应)

.container {
    display: flex;
}

.left-sidebar {
    width: 200px; /* 固定宽度 */
    flex-shrink: 0; /* 防止缩 small  */
}

.main-content {
    flex: 1; /* 自适应宽度 */
}

.right-sidebar {
    width: 200px; /* 固定宽度 */
    flex-shrink: 0; /* 防止缩 small  */
}

三栏布局example

left 侧栏 (150px)
主 in 容区 (自适应)
right 侧栏 (150px)

4.6 etc. high 列

.container {
    display: flex;
}

.column {
    flex: 1;
    /* 不需要设置 high 度, 会自动etc. high  */
}

etc. high 列example

列 1

这 is 第一列 in 容, package含 many 行文本.

Flexbox会自动使所 has 列etc. high .

列 2

这 is 第二列 in 容, 只 has 一行文本.

列 3

这 is 第三列 in 容, package含 many 行文本.

即使 in 容how many不同, 所 has 列 high 度也会保持一致.

这 is Flexbox 一个 important features.

5. 练习

练习 1: BasicsFlexbox布局

  1. creation一个HTMLfile, package含以 under structure:
    <div class="container">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
    </div>
  2. creation一个CSSfile, 添加以 under 样式:
    • .container设置 for Flexcontainers
    • 设置project in containersin水平居in for 齐
    • 设置project in containersin垂直居in for 齐
    • for project添加适当 样式 and 间距
  3. in HTMLfilein引入CSSfile, 查看效果

练习 2: response式导航栏

  1. creation一个HTMLfile, package含以 under structure:
    <nav class="navbar">
        <div class="logo">Logo</div>
        <ul class="nav-links">
            <li><a href="#">首页</a></li>
            <li><a href="#">About Us</a></li>
            <li><a href="#">service</a></li>
            <li><a href="#">Contact Us</a></li>
        </ul>
    </nav>
  2. creation一个CSSfile, 添加以 under 样式:
    • .navbar设置 for Flexcontainers
    • 将导航链接 right for 齐
    • 确保导航栏 in 不同屏幕尺寸 under 都能正常显示
    • for 导航项添加适当 样式 and 交互效果
  3. in HTMLfilein引入CSSfile, test效果

练习 3: 卡片布局

  1. creation一个HTMLfile, package含以 under structure:
    <div class="card-container">
        <div class="card">
            <img src="https://via.placeholder.com/300x200" alt="Card 1">
            <div class="card-content">
                <h3>卡片标题 1</h3>
                <p>这 is 卡片 1  describes in 容. </p>
                <a href="#" class="btn">Understand更 many </a>
            </div>
        </div>
        <div class="card">
            <img src="https://via.placeholder.com/300x200" alt="Card 2">
            <div class="card-content">
                <h3>卡片标题 2</h3>
                <p>这 is 卡片 2  describes in 容, package含更 many  文本information. </p>
                <a href="#" class="btn">Understand更 many </a>
            </div>
        </div>
        <div class="card">
            <img src="https://via.placeholder.com/300x200" alt="Card 3">
            <div class="card-content">
                <h3>卡片标题 3</h3>
                <p>这 is 卡片 3  describes in 容, package含最 many  文本information, 用于testetc. high 布局. </p>
                <a href="#" class="btn">Understand更 many </a>
            </div>
        </div>
    </div>
  2. creation一个CSSfile, 添加以 under 样式:
    • .card-container设置 for Flexcontainers
    • implementation卡片 response式布局, in 不同屏幕尺寸 under 显示不同数量 卡片
    • 确保所 has 卡片 high 度相同
    • for 卡片添加适当 样式 and 阴影效果
    • 确保卡片 in 按钮始终位于卡片底部
  3. in HTMLfilein引入CSSfile, test效果
on 一节: CSS 浮动 and 定位 under 一节: CSS Grid 布局