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

则效果:

相关推荐
滴滴滴嘟嘟嘟.24 分钟前
Qt UDP通信学习
qt·学习·udp
努力的小帅35 分钟前
C++_哈希
开发语言·c++·学习·算法·哈希算法·散列表
zzZ65651 小时前
支持向量机(SVM)学习报告
学习·机器学习·支持向量机
Hello_Embed10 小时前
STM32HAL 快速入门(十九):UART 编程(二)—— 中断方式实现收发及局限分析
笔记·stm32·单片机·嵌入式硬件·学习
天上的光11 小时前
关于学习的一些感悟
学习
red_redemption11 小时前
自由学习记录(95)
学习
东风西巷12 小时前
Balabolka:免费高效的文字转语音软件
前端·人工智能·学习·语音识别·软件需求
XISHI_TIANLAN14 小时前
【多模态学习】Q&A3:FFN的作用?Embedding生成方法的BERT和Word2Vec?非线性引入的作用?
学习·bert·embedding
@大迁世界15 小时前
用 popover=“hint“ 打造友好的 HTML 提示:一招让界面更“懂人”
开发语言·前端·javascript·css·html
伍哥的传说15 小时前
Tailwind CSS v4 终极指南:体验 Rust 驱动的闪电般性能与现代化 CSS 工作流
前端·css·rust·tailwindcss·tailwind css v4·lightning css·utility-first