很多时候,我们需要让元素居中显示:1. 一段文本的水平居中,2. 一张图片的水平居中,3. 一个块级元素的水平居中;4. 单行文本的竖直居中,5. 不确定高度的一段文本竖直居中,6. 确定高度的块级元素竖直居中等等。现在分别对其进行总结下(这篇文章也在 imooc 里发表过手记,可是因为板式的原因不太容易读懂。):

1. 让元素水平居中,使用 text-align: center;

<div class="text-center">水平居中</div>        .text-center {width: 200px;height: 100px;text-align: center;  /* 让文本水平居中 */color: #fff;background-color: #f54;
}


2. 让图片水平居中,父元素使用 text-align: center;

<div class="img-center"><img src="fenjing.jpg" alt="蓝天白云青山绿水">
</div>    .img-center {width: 200px;height: 120px;text-align: center; /* 让图片水平居中 */background-color: #f54;
}

说明:

图片是行内元素,从一开始我视频学习的时候,有一个老师好像说过图片是行内块级元素(inline-block),听起来好像很有道理的,因为图片可以使用 text-align: center; 将其水平居中显示,并且还能设置宽和高,很长时间以来没有怀疑过!后来喜欢上了“溯本求源”,才发现了原来不是那么回事:

在 ie, edge, chrome, firefox, opera 中对于 img 的默认显示方式是: display: inline;

ie:

edge:

chrome:

firefox:

opera:

img 是 inline,还是比较容易想得通,像文本一样可以通过 text-align: center; 设置为水平居中


3. 块级元素水平居中,使用 margin-right: auto; margin-left: auto;

<div class="parent-box"><div class="child-box">块级元素水平居中</div>
</div> .parent-box {width: 250px;height: 150px;background-color: #f98;
}
.child-box {width: 200px;height: 100px;background-color: #f00;margin-left: auto;margin-right: auto;
}


4. 单行文本的垂直居中,让 line-height 和 height 相等。

<div class="text-middle">单行文本竖直居中</div>.text-middle {width: 200px;height: 100px;line-height: 100px;background-color: #f00;color: #fff;
}

注意:

这里说的 height 和 line-height 相等,有一个注意事项:

当 box-sizing: content-box; 时(这也是默认的值)。将 height 和 line-height 的值设置为一样就行了;当 box-sizing: border-box; 时, line-height 的值要从 height 里减去 padding-top, padding-bottom, border-top, border-bottom 四个的值,也就是和分配给内容的有效高度相等。


5. 不确定高度的一段文本竖直居中,这里不适用高度,使用 padding-top: ...; padding-bottom: ...; padding-top 和 padding-bottom 值相同.

<div class="text-middle-padding">不确定高度的一段文本竖直居中</div> .text-middle-padding {width: 150px;padding-top: 30px;padding-bottom: 30px;color: #fff;background-color: #f00;
}

说明:对于高度确定的元素,它的文本的行数不确定的情况下,怎么让文本垂直居中呢?在后面会提到。


6. 确定高度的块级元素竖直居中,使用 position: absolute; top: 50%; margin-top: ...;(margin-top的值为自身高度的值的一半的负值);

<div class="parent-box"><div class="child-box">确定高度的块级元素竖直居中</div>
</div> .parent-box {position: relative;width: 250px;height: 150px;background-color: #f00;
}
.child-box {position: absolute;top: 50%;width: 200px;height: 100px;margin-top: -50px;background-color: #f54;
}


7. 绝对定位实现水平垂直居中,使用 position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto;

<div class="parent-box"><div class="child-box">绝对定位实现水平垂直居中居中</div>
</div> .parent-box {position: relative;width: 250px;height: 150px;background-color: #f00;
}
.child-box {position: absolute;top: 0;right: 0;bottom: 0;left: 0;width: 200px;height: 100px;margin: auto;background-color: #f54;
}

说明:对于块儿级元素的垂直居中,推荐这么做,这也是我比较喜欢的方法。

需要注意的地方是,对父元素要使用 position: relative; 对子元素要使用 position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; 缺一不可。如果只需要垂直居中,right: 0; 和 left: 0; 可以省略不写,margin: auto; 可以换成 margin-top: auto; margin-bottom: auto;;如果只需要水平居中,top: 0; bottom: 0; 可以省略不写,margin: auto; 可以换成 margin-rihgt: auto; margin-left: auto; 。


8. 平移实现水平垂直居中法:通过使用 transform: translate(-50%,-50%); 添加厂商前缀 -webkit- 兼容 Safari 和 Chrome

<div class="parent-box"><div class="child-box">平移实现水平垂直居中法</div>
</div> .parent-box {width: 200px;height: 200px;background-color: #f00;
}
.child-box {position: relative;top: 50%;left: 50%;width: 150px;height: 150px;background-color: #f54;-webkit-transform: translate(-50%,-50%);transform: translate(-50%,-50%);
}


9. 让浏览器计算子元素的宽高并让其水平垂直居中:通过使用定位position: absolute; top:...; right: ...; bottom: ...; left: ...; 四个方向上的值缺一不可。

<div class="parent-box"><div class="child-box">让浏览器计算子元素的宽高并让其水平垂直居中</div>
</div> .parent-box {position: relative;width: 200px;height: 200px;background-color: #f00;
}
.child-box {position: absolute;top: 20%;right: 20%;bottom: 20%;left: 20%;background-color: #f54;
}

对于子元素,上下左右的定位值可以用 px 作为单位,也可以用 % 作为单位。


10. css3伸缩布局实现元素水平垂直居中,通过使用 display:flex;  align-items: center;  justify-content: center;

<div class="parent-box"><div class="child-box">我是子元素,这里使用了 css3 的弹性伸缩布局</div>
</div>.parent-box {width: 400px;height: 150px;display: flex;justify-content: center; /* 让子元素水平居中 */align-items: center; /* 让子元素垂直居中 */border: 1px solid #999;
}
.child-box {background-color: #fe5454;color: #fff;
}

说明:

ie 10 及以上版本浏览器支持,chrome, firefox, opera, edge 均支持,不需要添加厂商前缀。

另外:这里也解释了第5点中“对于高度确定的元素,它的文本的行数不确定的情况下,怎么让文本垂直居中呢?”的问题,使用这里提到的 css3 弹性布局方式。对付元素使用 display: flex; justify-content: center; align-items: center; 来解决。

注意:

1. 如果不添加 justify-content: center; 子元素不会水平居中;

2. 如果不添加 align-items: center; 子元素会铺满父元素的高度,而不是我们希望的只有包含住文本的高度!

记忆方法:

我们知道:text-align: justify; 能将文本按照两端对其的方式对文本进行布局,这个处理的是水平方向上的问题。联想记忆,justify-content 也是处理水平方向上的事情,所以 justify-contnet: center; 就是让元素水平居中了。


扩展:

需求:我们经常做分页时,需要将分页的列表项置于水平居中的位置,就像下面的 dom 一样:

<ul class="pagination"><li><a href="#">&laquo;</a></li><li><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li><li><a href="#">&raquo;</a></li>
</ul>      

解决方法:

可以为父元素 ul 添加 text-align: center; 同时给子元素 li 添加 display: inline-block;

完整的代码:

<ul class="pagination"><li><a href="#">&laquo;</a></li><li><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li><li><a href="#">&raquo;</a></li>
</ul>

ul.pagination {margin-top: 20px;text-align: center;font-size: 0; /* 设置 font-size 的大小为 0,目的是让显示方式为 inline-block 的子元素去除外边距(外边距是由于 html 的空格所导致的) */
}
ul.pagination li { display: inline-block; }
ul.pagination li a {display: inline-block;padding: 7px 14px;border-width: 1px 0 1px 1px;border-style: solid;border-color: #f1f2f3;font-size: 15px;  /* 这里一定要设置 font-size,别指望去继承了,因为如果不设置,将会继承 ul 的大小 0 */transition: all .3s ease 0;
}
ul.pagination li:first-child a {border-top-left-radius: 5px;border-bottom-left-radius: 5px;
}
ul.pagination li:last-child a {border-right: 1px solid #f1f2f3;border-top-right-radius: 5px;border-bottom-right-radius: 5px;
}
ul.pagination li a:hover {background-color: #fe5454;color: #fff;border-color: #fe5454;
}

标签: css, display, 居中

转载于:https://www.cnblogs.com/libin-1/p/5918477.html

css中元素居中总结相关推荐

  1. css中关于居中的那点事儿

    css中关于居中的那点事儿 关于居中,无论是水平居中,还是垂直居中都有很多方式,下面我来介绍一些常用的. 第一部分:水平居中 1.实现行内元素的居中.方法:在行内元素外面的块元素的样式中添加:text ...

  2. CSS实现元素居中原理解析

    原文:CSS实现元素居中原理解析 在 CSS 中要设置元素水平垂直居中是一个非常常见的需求了.但就是这样一个从理论上来看似乎实现起来极其简单的,在实践中,它往往难住了很多人. 让元素水平居中相对比较简 ...

  3. 10 个CSS实现元素居中的方法汇总

    英文 | https://javascript.plainenglish.io/10-css-tricks-you-should-know-for-centering-elements-61092d3 ...

  4. 【CSS】元素居中总结-水平居中、垂直居中、水平垂直居中

    [CSS]元素居中 一. 水平居中 1.行内元素水平居中 (1)text-align 2.块级元素水平居中 2.1 margin (1)margin 2.2布局 (1)flex+ justify-co ...

  5. css 伪元素居中对齐

    css 伪元素居中对齐 .demo {display: flex;padding: 40px;}.demo::after {content: '';display: inline-block;back ...

  6. css中如何居中一个浮动元素,css浮动元素的居中的例子

    居中对于大家来说指的是页面了,此时我们通常使用提50%来实现了,下面来看一个css浮动元素的居中的例子,希望例子对各位有用. 关于浮动元素的居中,其实不太常用,但为了应付某些奇葩的面试官,也要知道怎么 ...

  7. CSS中各种各样居中方法的总结

    在开发前端页面的时候,元素的居中是一个永远都绕不开的问题.看似简单的居中二字,其实蕴含着许许多多的情况,对应着很多的处理方法,本文就试图对页面布局中的居中问题进行总结~~ 居中问题分为水平居中和竖直居 ...

  8. html中元素居中的五种方法

    在网页开发中,经常会有嵌套元素中将子元素居中的要求.下边将五种常用的居中方法进行总结. 1:原始图(父子元素无border,无padding): 2:实现居中效果: 一:使用margin进行固定长度的 ...

  9. css中div居中显示的四种方法

    css中设置div元素居中显示的四种方法 一.先确定div的基本样式 二.具体实现方法 第一种:利用子绝父相和margin: auto实现 第二种:利用子绝父相和过渡动画tranform实现 第三种: ...

最新文章

  1. memcached 缓存服务器
  2. LineageOS编译
  3. BZOJ3294 CQOI2011放棋子(动态规划)
  4. 百词斩和扇贝打卡测试与评估
  5. 云通讯短信验证码实例
  6. 产品经理思维模型:从品牌营销的角度看用户
  7. 计算机专业毕业论文指导记录详细,毕业论文指导记录怎么写 了解一下
  8. 外设驱动库开发笔记31:S-Modlue远红外气体传感器驱动
  9. 团队协作新趋势:语雀——助力更多:个人、企业、博客、协作、托管、画布设计、资源托管(立体化趋势)
  10. placeholder和assign速度对比
  11. Django:数据进行md5加密
  12. 谷粒商城:14.全文检索ElasticSearch
  13. Linux keypad 设备树,beaglebone black通过devicetree配置矩阵键盘matrix-keypad
  14. SCADA和三大工业控制系统PLC、DCS、FCS
  15. 程序员如何实现“财务自由”?反正不是靠天天加班就能的!
  16. 台架控制器-AVL_ISAC学习
  17. bzoj3207 花神的嘲讽计划Ⅰ
  18. mysql 重置密码_mysql忘记密码如何重置密码,以及修改root密码的三种方法
  19. vlan局域网对战平台
  20. 南海滩饮食法——不饿的情况下快速减肥的法宝

热门文章

  1. Java谜题:等于,还是不等于?
  2. 在2011年QCon北京大会上的主题分享内容——Keynote
  3. 开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试
  4. springmvc 使用fastjson 处理 json 数据时中文乱码
  5. GoJS超详细入门(插件使用无非:引包、初始化、配参数(json)、引数据(json)四步)...
  6. CentOS Linux解决Device eth0 does not seem to be present
  7. 跨域cookie传递
  8. 如何确定一个IAR工程所使用的IAR版本
  9. DzzOffice增加应用对扩展名文件的支持设置,将会在Beta中提供。
  10. oh-my-zsh 功能收集