博主信息
博文 12
粉丝 0
评论 0
访问量 9294
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
居中布局列举
sea
原创
1185人浏览过

水平居中

当前元素在父级元素容器中,水平方向是居中显示的

  1. 文本/行内元素/行内块元素实现水平居中
  • text-align
    只控制行内内容;属性会继承影响到后代行内内容;如果子元素宽度大于父元素宽度则无效
  1. <div id="box">水平文字居中</div>
  1. #box {
  2. width: 200px;
  3. height: 200px;
  4. background: #c9394a;
  5. text-align: center;
  6. }

2.单个块级元素实现水平居中

  • margin
    该单个块级元素必须给定一个宽度(不能是 auto),并且宽度要小于父元素
  1. <div id="parent">
  2. <div id="child">水平布局</div>
  3. </div>
  1. #parent {
  2. width: 100%;
  3. height: 200px;
  4. background: #ccc;
  5. }
  6. #child {
  7. width: 200px; /* 必须定宽 */
  8. height: 200px;
  9. background: #c9394a;
  10. margin: 0 auto;
  11. }

3.多个块级元素实现水平居中

块级元素改为行内块,换行、空格会产生间隔

  1. <div id="parent">
  2. <div class="child1"></div>
  3. <div class="child2"></div>
  4. <!-- 手动去掉换行符即可 -->
  5. </div>
  1. #parent {
  2. width: 100%;
  3. height: 200px;
  4. background: #ccc;
  5. text-align: center; /* 父元素加 */
  6. }
  7. .child1 {
  8. width: 300px;
  9. height: 200px;
  10. background: #c9394a;
  11. display: inline-block; /* 子元素变为行内块 */
  12. }
  13. .child2 {
  14. width: 300px;
  15. height: 200px;
  16. background: green;
  17. display: inline-block;
  18. }

4.利用绝对定位水平居中

  1. <div id="parent">
  2. <div id="child"></div>
  3. </div>
  1. #parent {
  2. width: 100%;
  3. height: 200px;
  4. background: #ccc;
  5. position: relative;
  6. }
  7. #child {
  8. width: 300px;
  9. height: 200px;
  10. background: #c9394a;
  11. position: absolute;
  12. left: 50%;
  13. /* margin-left: -150px; 自身宽度的一半 */
  14. transform: translateX(-50%);
  15. }

5.任意个元素(flex)

PC 端兼容性不好,一般用于移动端

  1. <div id="parent">
  2. <div id="child"></div>
  3. </div>
  1. #parent {
  2. width: 100%;
  3. height: 200px;
  4. background: #ccc;
  5. display: flex; /* 加这两句即可 */
  6. justify-content: center;
  7. }
  8. #child {
  9. width: 300px;
  10. height: 200px;
  11. background: #c9394a;
  12. }

垂直居中

  1. 文本/行内元素/行内块元素实现水平居中
  • line-height
    行高与高度相等,只能用于单行文本,并且要给定高度
  1. <div id="child">垂直居中</div>
  1. #child {
  2. width: 200px;
  3. height: 200px;
  4. background: #c9394a;
  5. line-height: 200px;
  6. }

2.图片垂直居中

  1. <div id="parent">
  2. <img src="./images/头像.png" alt="" />
  3. </div>
  1. #parent {
  2. width: 400px;
  3. height: 600px;
  4. background: #ccc;
  5. line-height: 600px; /* 父级元素行高与高度相等 */
  6. }
  7. img {
  8. width: 200px;
  9. height: 200px;
  10. vertical-align: middle; /* 中线对齐;默认基线对齐 */
  11. }

3.利用绝对定位垂直居中

  1. <div id="parent">
  2. <div id="child"></div>
  3. </div>
  1. #parent {
  2. width: 100%;
  3. height: 600px;
  4. background: #ccc;
  5. position: relative;
  6. }
  7. #child {
  8. width: 200px;
  9. height: 200px;
  10. background: #c9394a;
  11. position: absolute;
  12. top: 50%;
  13. /* margin-top: -100px; 自身高度的一半 */
  14. transform: translateY(-50%);
  15. }
  16. }

水平垂直居中

  1. 利用 table-cell

    会导致父级元素的文本垂直居中

  1. <div id="parent">
  2. <div id="child"></div>
  3. </div>
  1. #parent {
  2. width: 1000px;
  3. /* 必须给定一个具体的宽值,不然只能被子元素撑开 */
  4. height: 600px;
  5. background: #ccc;
  6. display: table-cell;
  7. vertical-align: middle; /* 垂直居中 */
  8. }
  9. #child {
  10. width: 200px;
  11. height: 200px;
  12. background: #c9394a;
  13. margin: 0 auto; /* 水平居中 */
  14. }

2.利用绝对定位

  1. <div id="parent">
  2. <div id="child"></div>
  3. </div>
  1. #parent {
  2. position: relative;
  3. width: 100%;
  4. height: 600px;
  5. background: #ccc;
  6. }
  7. #child {
  8. position: absolute;
  9. top: 50%;
  10. left: 50%;
  11. width: 200px;
  12. height: 200px;
  13. background: #c9394a;
  14. /* margin-left: -100px; */
  15. /* margin-top: -100px; */
  16. transform: translate(-50%, -50%);
  17. }

3.利用 flex

PC 端兼容性不好,适用于移动端

  1. <div id="parent">
  2. <div id="child"></div>
  3. </div>
  1. parent {
  2. width: 1000px;
  3. height: 600px;
  4. background: #ccc;
  5. display: flex;
  6. justify-content: center; /* 水平排列 */
  7. align-items: center; /* 垂直排列 */
  8. }
  9. #child {
  10. width: 200px;
  11. height: 200px;
  12. background: #c9394a;
  13. }
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学