css 实现水平居中的方法总结

css 实现水平居中,垂直居中,水平垂直居中,是css 入门的必修课题,也是代码实践,笔试面试中经常遇到的场景。这次的内容主要围绕着几种场景下的,多种水平居中方法的实现

实现场景:蓝色方块需要在父元素内部水平居中

image.png

1. 居中元素为块级元素

基础代码

<div class="container">
    <div class="box">牛</div>
</div>
.container{
  width: 100px;
  border:1px solid red;
  height: 100px;
}
.container .box{
  font-size: 20px;
  line-height: 50px;
  background-color: blue;
  color: white;
}

块级元素的特点:可设置元素的宽度,在不设置宽度时, 元素的宽度会自动填满其父元素宽度,并独占一行

1.1 margin: auto

居中元素设置宽度,并且设置 margin: auto 可实现水平居中

.container .box{
  height: 50px;
  width: 50px; /* 设置宽度 */
  margin: 0 auto; /* 设置宽度 */
}

问题: margin: auto 实现水平居中的前提是, 元素的宽度已知,当宽度值未知时,见2.2

2. 居中元素为行内元素

基础代码

<div class="container">
    <span class="box">牛</span>
</div>
.container{
  width: 100px;
  border:1px solid red;
  height: 100px;
}
.container .box{
  font-size: 20px;
  background-color: blue;
  color: white;
}

行内元素的特点: 行内元素的宽度为元素自身撑起的宽度,和其他元素在同一行,高度,宽度,内边距等都是不可控的,即设置padding-left, padding-right, margin-left, margin-right, height, width 都无法生效

2.1 text-align: center

利用行内元素的特点,设置 text-align:center 使元素居中

.container{
  text-align: center;
}

2.2 display:inline-block 改变模型

利用 行内元素设置 text-align 实现水平居中的方法,可以针对居中元素为块级元素,但宽度不确定时 (即1.1 问题)的场景

<div class="container">
    <div class="box">牛</div>
  </div>
.container{
  text-align: center;
}
.container .box{
  display: inline-block;
  *display: inline; /*兼容性代码*/
  *zoom: 1; /*兼容代码*/
}

问题: 需要解决 display: inline-block 在IE 下的兼容性问题

3. 利用定位实现水平居中(已知宽度)

相对定位: 相对定位就是将元素偏离元素的默认位置,但是并没有脱离文档流,只是视觉上发生的偏移,不会改变元素的display 属性

绝对定位: 相对于设置了 相对定位的上层元素或者顶级元素的相对位置,无论设置的元素为块级元素还是行内元素,position 设置成absolute 之后 , 元素display 表现为 block, 元素脱离文档流,父元素无法知道该元素的高度

因此,从以上的定义我们可以得到一种新的水平居中的方式,父级元素设为相对定位,居中元素设置为绝对定位,设置居中元素的宽度,并相对于父元素设置位置

.container{
  position: relative;
}
.container .box{
  position: absolute;
  width: 50px; /*设置宽度*/
  left: 50%; /*左移50%*/
  margin-left: -25px; /*修正位置*/
}

缺点: 缺点是需要知道元素的宽度

4 利用定位,translate 实现水平居中(未知宽度)

对于已知元素的宽度,我们可以使用3中的 margin-left: -25px; /*修正位置*/ 进行精确的定位,而对于不知道宽度的元素,我们可以使用 translate 来精准定位,只需要将 margin-left: -25px; /*修正位置*/ 替换成 transform: translate(-50%, 0) 即可

.container .box{
  font-size: 20px;
  background-color: blue;
  color: white;
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  width: 50px;
}

5. flex 布局实现

Flex是Flexible Box的缩写,意为”弹性布局”,父元素设置成flex 布局,可以将子元素设置为水平居中, (但是flex布局部分浏览器无法使用)

.container{
  display: flex;
  justify-content: center
}

6. tabel-cell布局

实际项目中并不常见,故暂不提及。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,680评论 1 92
  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 12,530评论 3 30
  • 1. 前言 前端圈有个“梗”:在面试时,问个css的position属性能刷掉一半人,其中不乏工作四五年的同学。在...
    YjWorld阅读 10,091评论 5 15
  • 学会使用CSS选择器熟记CSS样式和外观属性熟练掌握CSS各种选择器熟练掌握CSS各种选择器熟练掌握CSS三种显示...
    七彩小鹿阅读 11,424评论 2 66
  • 第三卷: (三十八)明利害之情,忘利害之忧。 原文: 议事者,身在事外,宜悉利害之情;任事者,身居事中,当忘利害之...
    爱文字的小药师阅读 6,169评论 7 16