【CSS入门学习】Flex布局设置div水平、垂直分布与居中

水平平均分布

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            display: flex;
            justify-content: space-between;           
        }
        .item {
            width: 200px;
            height: 100px;
            background-color: antiquewhite;
            border: 1px solid;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">左侧内容</div>
        <div class="item">中间内容</div>
        <div class="item">右侧内容</div>
    </div>
</body>
</html>

一左一右

<div class="item">中间内容</div>注释掉,只保留左右两个div:

还可以分别设置左右div的宽度:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            display: flex;
            justify-content: space-between;            
        }
        .item {
            width: 200px;
            height: 100px;
            background-color: antiquewhite;
            border: 1px solid;
        }
        .left {
            width: 30%;
        }
        .right {
            width: 60%;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item left">左侧内容</div>
        <div class="item right">右侧内容</div>
    </div>
</body>
</html>

水平居中

再item类中增加一条样式:

css 复制代码
text-align: center;

则只能水平居中。

水平、垂直居中

使用Flex进行水平垂直居中,在item类中增加样式:

css 复制代码
display: flex;
/* 水平居中 */
justify-content: center;
/* 垂直居中 */
align-items: center;

所以要让一个div里面的内容水平、垂直居中,可以将这个div变成flex布局,再设置justify-content和align-items属性。

垂直平均分布

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            display: flex;
            flex-direction: column;
            justify-content: space-between;       
        }
        .item {
            width: 200px;
            height: 100px;
            background-color: antiquewhite;
            border: 1px solid;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">上侧内容</div>
        <div class="item">中间内容</div>
        <div class="item">下侧内容</div>
    </div>
</body>
</html>

则效果:

可以为container 类增加一个高度样式:

css 复制代码
height: 500px;

则效果:

再在container类增加样式,使水平居中:

css 复制代码
align-items: center;

效果图:

再在item类中增加样式,使各个小div中文本水平垂直居中:

css 复制代码
display: flex;
justify-content: center;
align-items: center;

则效果:

相关推荐
星空3 分钟前
css+html案例
css·html·css3
m0_6515625213 分钟前
2026/3/26 学习笔记——终端复用工具screen
笔记·学习
sjg2001041415 分钟前
GoFrame学习随便记5 (quasar)(待续)
学习
路小雨~19 分钟前
RabbitMQ 全面学习资料
分布式·学习·rabbitmq
惠惠软件40 分钟前
AI 龙虾 | 对学习工作的影响和未来前瞻
人工智能·学习
happymaker06261 小时前
web前端学习日记——DAY08(jQuery,json文件格式,bootstrap)
前端·学习·jquery
weixin_443478511 小时前
FLUTTER组件学习之进度指示器
学习·flutter
be to FPGAer1 小时前
ASIC设计与综合-1
学习
大迪deblog1 小时前
系统架构设计-学习建议
学习·系统架构
黑眼圈子1 小时前
牛客刷题记录5
java·开发语言·学习·算法