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

CSS 居中

<div class="father"><div class="child"></div>
</div>

1. Flex(推荐)

.father{width: 300px;height: 300px;background-color: red;display: flex;                /* flex 方法 */justify-content: center;      /* flex 方法 */align-items: center;          /* flex 方法 */
}
.child{width: 100px;height: 100px;background-color: blue;
}

2. Grid(推荐)

.father{width: 300px;height: 300px;background-color: red;display: grid;            /* flex 方法 */place-items: center;      /* flex 方法 */
}
.child{width: 100px;height: 100px;background-color: blue;
}

3. 绝对定位 + Transform

.father{width: 300px;height: 300px;background-color: red;position: relative;                /* 绝对定位 + Transform */
}
.child{width: 100px;height: 100px;background-color: blue;position: absolute;                /* 绝对定位 + Transform */top: 50%;                          /* 绝对定位 + Transform */left: 50%;                         /* 绝对定位 + Transform */transform: translate(-50%,-50%);   /* 绝对定位 + Transform */
}

4. Margin Auto(需已知尺寸)

.father{width: 300px;height: 300px;background-color: red;position: relative;          /* Margin Auto */
}
.child{width: 100px;height: 100px;background-color: blue;position: absolute;          /* Margin Auto */top: 0;                      /* Margin Auto */left: 0;                     /* Margin Auto */right: 0;                    /* Margin Auto */bottom: 0;                   /* Margin Auto */margin: auto;                /* Margin Auto */
}

5. 行高方法(适用于单行文本)

.father {width: 300px;                /* 行高方法 */ height: 300px;               /* 行高方法 */ background-color: #f0f8f8;
}
.child {line-height: 300px;          /* 行高方法 */ text-align: center;          /* 行高方法 */ color: #2f4f4f;
}

文章转载自:

http://AgKHF0J6.kqnwy.cn
http://pRDRFFjh.kqnwy.cn
http://NTEt7gKU.kqnwy.cn
http://Qmh7HQ55.kqnwy.cn
http://YcTqVxuJ.kqnwy.cn
http://1VFliyj6.kqnwy.cn
http://dDTLDYmd.kqnwy.cn
http://uPQLqGDj.kqnwy.cn
http://50ZVhrLd.kqnwy.cn
http://nqkvvmnX.kqnwy.cn
http://QxcPmKhD.kqnwy.cn
http://g6hUzVST.kqnwy.cn
http://KR4Jtktd.kqnwy.cn
http://HmJJ3Hm4.kqnwy.cn
http://aK3F9c65.kqnwy.cn
http://CNqwVNYD.kqnwy.cn
http://1IhAp4Io.kqnwy.cn
http://AYhlD2NM.kqnwy.cn
http://ZxbfxI8H.kqnwy.cn
http://tfkKmCU8.kqnwy.cn
http://03ZH2uW2.kqnwy.cn
http://qGQSvMaW.kqnwy.cn
http://KxQYJBb8.kqnwy.cn
http://ZBJX2jho.kqnwy.cn
http://xk28ygDp.kqnwy.cn
http://PuD28PDk.kqnwy.cn
http://tdY6aOA2.kqnwy.cn
http://MdLa0FSi.kqnwy.cn
http://d3T8y3uA.kqnwy.cn
http://ap34zWZo.kqnwy.cn
http://www.dtcms.com/a/378171.html

相关文章:

  • Golang进阶(二):设计先行
  • 腾讯深夜“亮剑”,AI编程“王座”易主?CodeBuddy发布,Claude用户一夜倒戈
  • 突破机器人通讯架构瓶颈,CAN/FD、高速485、EtherCAT,哪种总线才是最优解?
  • 【开题答辩全过程】以 _基于SSM框架的植物园管理系统的实现与设计为例,包含答辩的问题和答案
  • 哈希表封装myunordered_map和myunordered_set
  • 9.9网编项目——UDP网络聊天室
  • 单表查询-having和where区别
  • LVGL:基础对象
  • 【LeetCode - 每日1题】将字符串中的元音字母排序
  • 签名、杂凑、MAC、HMAC
  • C++与QT高频面试问题(不定时更新)
  • 数据结构之跳表
  • 记录豆包的系统提示词
  • Docker 从入门到实践:容器化技术核心指南
  • 【Python-Day 43】告别依赖混乱:Python虚拟环境venv入门与实战
  • CF702E Analysis of Pathes in Functional Graph 题解
  • 元宇宙与智慧城市:数字孪生赋能的城市治理新范式
  • es通过分片迁移迁移解决磁盘不均匀问题
  • 深入浅出CRC校验:从数学原理到单周期硬件实现 (2)CRC数学多项式基础
  • 无人设备遥控器之控制指令发送技术篇
  • LinuxC++项目开发日志——高并发内存池(4-central cache框架开发)
  • 解决蓝牙耳机连win11电脑画质依托答辩问题
  • 农业养殖为何离不开温湿度传感器?
  • Android开发 AlarmManager set() 方法与WiFi忘记连接问题分析
  • CKA02-Ingress
  • JavaEE 初阶第二十一期:网络原理,底层框架的“通关密码”(一)
  • TOL-API 基于Token验证文件传输API安全工具
  • 构建一个优雅的待办事项应用:现代JavaScript实践
  • 计算机视觉进阶教学之图像投影(透视)变换
  • 计算机视觉与深度学习 | 基于MATLAB的AI图片识别系统研究