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

则效果:

相关推荐
DeathGhost6 小时前
CSS container容器查询
前端·css
不会敲代码11 天前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Sailing1 天前
🚀 别再乱写 16px 了!CSS 单位体系已经进入“计算时代”,真正的响应式布局
前端·css·面试
球球pick小樱花2 天前
游戏官网前端工具库:海内外案例解析
前端·javascript·css
AAA阿giao3 天前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
掘金安东尼3 天前
用 CSS 打造完美的饼图
前端·css
掘金安东尼4 天前
纯 CSS 实现弹性文字效果
前端·css
前端Hardy4 天前
HTML&CSS&JS:打造丝滑的3D彩纸飘落特效
前端·javascript·css
前端Hardy4 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
parade岁月5 天前
Tailwind CSS v4 — 当框架猜不透你的心思
前端·css