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

则效果:

相关推荐
maxruan1 小时前
PyTorch学习
人工智能·pytorch·python·学习
MYX_3092 小时前
第三章 线型神经网络
深度学习·神经网络·学习·算法
_李小白2 小时前
【Android Gradle学习笔记】第八天:NDK的使用
android·笔记·学习
Jose_lz3 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#
QT 小鲜肉3 小时前
【个人成长笔记】Qt 中 SkipEmptyParts 编译错误解决方案及版本兼容性指南
数据库·c++·笔记·qt·学习·学习方法
A9better3 小时前
嵌入式开发学习日志41——stm32之SPI总线基本结构
stm32·单片机·嵌入式硬件·学习
533_5 小时前
[css] border 渐变
前端·css
Lynnxiaowen5 小时前
今天我们学习python编程常用模块与面向对象
运维·python·学习·云计算
昔人'6 小时前
css`text-underline-offset` 为文本下划线设置偏移量
前端·css
Han.miracle6 小时前
数据结构——排序的学习(一)
java·数据结构·学习·算法·排序算法