【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;

则效果:

相关推荐
Summer_Uncle23 分钟前
【C++学习】指针
c++·学习
光影少年1 小时前
AIGG人工智能生态及学习路线和应用领域
人工智能·学习
递归不收敛1 小时前
多模态学习大纲笔记(未完成)
人工智能·笔记·学习·自然语言处理
泷羽Sec-静安2 小时前
Less-1 GET-Error based-Single quotes-String GET-基于错误-单引号-字符串
前端·css·网络·sql·安全·web安全·less
FPGA-李宇航2 小时前
FPGA中,“按键控制LED灯实验”学习中常见问题、解决思路和措施以及经验总结!!!(新手必看)
学习·fpga开发·按键控制led灯
正经教主3 小时前
【Trae+AI】和Trae学习搭建App_01:项目架构设计与环境搭建
学习·app
普蓝机器人3 小时前
面向智慧农业的自主移动果蔬采摘机器人:融合视觉识别与自动驾驶的智能化农作系统研究
人工智能·学习·机器人·移动机器人·三维仿真导航
lazily-c4 小时前
Web后端开发总结
java·学习·web·1024程序员节
一只学java的小汉堡5 小时前
HTML 01入门:从概念到开发环境搭建与页面头部配置
前端·css·html
于顾而言6 小时前
【笔记】Comprehensive Rust语言学习
笔记·学习·rust