当前位置: 首页 > news >正文

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

水平平均分布

在这里插入图片描述

<!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的宽度:

在这里插入图片描述

<!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类中增加一条样式:

text-align: center;

则只能水平居中。

在这里插入图片描述

水平、垂直居中

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

display: flex;
/* 水平居中 */
justify-content: center;
/* 垂直居中 */
align-items: center;

在这里插入图片描述

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

垂直平均分布

<!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 类增加一个高度样式:

height: 500px;

则效果:

在这里插入图片描述

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

align-items: center;

效果图:

在这里插入图片描述

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

display: flex;
justify-content: center;
align-items: center;

则效果:
在这里插入图片描述

http://www.lryc.cn/news/527348.html

相关文章:

  • 9. 神经网络(一.神经元模型)
  • R 语言 | future 包,非阻塞的执行耗时脚本
  • UE学习日志#12 Niagara特效大致了解(水文,主要是花时间读了读文档和文章)
  • 【数据结构】_链表经典算法OJ:合并两个有序数组
  • Mongodb副本集群为什么选择3个节点不选择4个节点
  • 基于 WEB 开发的手机销售管理系统设计与实现内容
  • LeetCode - Google 大模型校招10题 第1天 Attention 汇总 (3题)
  • Vue3 provide/inject用法总结
  • Linux——网络基础(1)
  • 【记录】日常|从零散记录到博客之星Top300的成长之路
  • 【二分查找】力扣373. 查找和最小的 K 对数字
  • 池化层Pooling Layer
  • 力扣算法题——11.盛最多水的容器
  • 自由学习记录(32)
  • VScode+Latex (Recipe terminated with fatal error: spawn xelatex ENOENT)
  • 「蓝桥杯题解」蜗牛(Java)
  • PHP EOF (Heredoc) 详解
  • pyautogui操控Acrobat DC pro万能PDF转Word,不丢任何PDF格式样式
  • Day32:字符串的复制
  • 基于Mybatis继承AbstractRoutingDataSource使用自定义注解实现动态数据源
  • ZooKeeper 数据模型
  • 【VUE】Vue2中Vue.extend方法
  • MaskGAE论文阅读
  • Mybatis-plus 更新 Null 的策略踩坑记
  • Oracle迁移DM数据库
  • HTML特殊符号的使用示例
  • 数据结构基础之《(15)—排序算法小结》
  • Linux系统下速通stm32的clion开发环境配置
  • 【2024年 CSDN博客之星】我的2024年创作之旅:从C语言到人工智能,个人成长与突破的全景回顾
  • Python 轻松扫描,快速检测:高效IP网段扫描工具全解析