整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

CSS 中几种最常用的水平垂直居中的方法

SS 是前端里面的基础之一,也是非常重要的一部分,它往往决定了你所做出来的网页页面是否美观。在设计网页页面的过程中,总会有将元素或者文字进行水平垂直居中的要求。下面w3cschool编程狮就为大家介绍 CSS 中几种常用到的水平垂直居中的方法。


一、使用 margin:auto

当元素有给定的高度以及宽度的时候,使用 margin: auto; 元素仅会水平居中,并不会进行垂直居中。此时就需要设置元素的 position 为 absolute,父级元素的 position 为 relative,同时元素的上下左右都需要设置为 0。

HTML 代码

<div class="box">
  <div class="center1"></div>
</div>

CSS 代码

.box{
  width: 200px;
  height: 200px;
  background-color: #eee;
  position: relative;
  margin-top: 20px;
}
.center1{
  width: 50px;
  height: 50px;
  background-color: #00ACED;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

效果展示:



二、使用 position:absolute

当已经知道了要进行水平垂直居中的元素的宽高时,就可以通过设置 position: absolute 来实现。但是,使用的同时还需要结合其他属性才完整实现。因为,单是设置 absolute,上左距离均为一半,就会出现下面这种情况。很显然可以看到,元素并不是完全居中,仅只有左上角的位置在中心点

概念图:

因此想要实现元素完全水平垂直居中,在设置了 absolute 定位后,可以设置 margin 值为负,或者使用 calc 来计算,上左距离在 50% 的基础上还要减去元素本身一半的宽高。

margin 值为负或者 calc 计算均是在已知元素宽高的情况下,假设不知道元素的宽高,那么怎么实现水平垂直居中呢?这里就可以使用 transform 属性,通过坐标位移来实现居中。

CSS 代码

/* 结合 margin */
.center2{
  width: 50px;
  height: 50px;
  background-color: #7FFFD4;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -25px;
  margin-top: -25px;
}
/* 结合 calc 计算*/
.center2{
  width: 50px;
  height: 50px;
  background-color: #7FFFD4;
  position: absolute;
  left: calc(50% - 25px)
  top: calc(50% - 25px);
}
/* 结合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

效果展示



03

PART

三、使用弹性布局

可以通过弹性布局来设置水平垂直居中,这里需要设置父级元素 display:flex; 还需要设置两个属性,水平布局 justify-content 以及垂直布局 align-items。

HTML代码

<div class="box2">
  <div class="center4"></div>
</div>

CSS代码:

.box2{
  background-color: #eee;
  width: 200px;
  height: 200px;
  position: relative;
  margin-top: 20px ;
  display: flex;
  justify-content: center;
  align-items: center;
}
.center4{
  width: 50px;
  height: 50px;
  background-color: #B39873;
}

效果展示:


四、文本水平对齐和行高

前面介绍的是元素如何实现水平垂直居中,下面介绍的是如何将文字进行水平垂直居中。这第一个方法也是最经常用的,使用文本水平对齐 text-align 和行高 line-height 来实现的。

HTML 代码

<div class="box3">
  <div class="center5">文字居中</div>
</div>

CSS 代码

.box3{
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
}
.center5{
  text-align: center;
  line-height: 200px;
}

效果展示


05

PART

五、使用网格布局

第二个方法可以通过网格布局 grid 来实现。而这里通过 grid 有两种方式实现,一种对元素本身属性进行设置,另一种在元素的父级元素中设置。两者看上去内容似乎差不多,不同的是在元素中设置的是 align-self 还要多了一个 margin,父级元素中是 align-items。

相关代码:

/* grid 元素中设置 */
.box4{
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
  display: grid;
}
.center6{
  align-self: center;
  justify-content: center;
  margin: auto;
}
/* grid 父级元素中设置 */
.box5{
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
  display: grid;
  align-items: center;
  justify-content: center;
}

效果展示:


六、总结

以上就是关于 CSS 如何将元素或者文字进行水平垂直居中的几种常用方法,大家还其他关于 CSS 实现水平垂直居中的方法吗?请在评论区留下你的想法。

关注w3cschool编程狮订阅更多IT资讯、技术干货~

哥最近调整自己的一个网站时,觉得在发布文章时,手动把图片居中,实在有些麻烦。能不能让文章内容中的图片自动居中呢?是否可以通过样式控制表来实现?

网上找了下答案,似乎有些过于复杂了。自己动手实践吧。

首先要调整的,是让图片独占一行空间。

补充CSS知识点:

图像(<img>标签)默认是行内样式(inline style),行内样式的大体意思是,它前边或后边的HTML标签元素会紧挨着排在它的前后(如影随形),不会另换一行;而像DIV标签则默认是块级元素,块级元素前后的标签元素会换行。因此需要把文章内容中的图片样式由行内样式更换为block块级元素,要不就易和文字内容纠缠在一起。

简单点:

  • block元素将显示为块级元素,此元素前后会带有换行符。

  • inline默认。元素会被显示为内联元素,元素前后没有换行符。

在原有的控制图像的样式中增加:display: block。

.entry img {

max-width: 100%;

height: auto;

display: block;

}

图像设为块级元素后,需要让图像居中。居中用的同样是网页布局时整体居中的代码,即margin:0 auto,0可以改为任何数据,像素或EM;重要的是AUTO属性,因为它控制元素左右的距离,AUTO则是用于居中的属性。

实例:

.entry img {

max-width: 100%;

height: auto;

display: block;

margin:0 auto;

}

经以上设置后,图像已能自动在文章内容中居中。但在实践中,发现一些旧的网页文章,在发布时其图像有行内样式,如:<img style=”margin:0;”>这样的。由于行内样式的优先(层叠)级别高于外部样式表中的图像自动居中的定义,因此刚才定义的:margin:0 auto就失效了。

这也不是问题,给这个居中用!important指定为最优先级别。最样使用的样式如下。

.entry img {

max-width: 100%;

height: auto;

display: block;

margin: 0 auto !important;

}

好记性不如烂笔头,记下来方便再次用。

实例应用可查看:起点通首页点任一文章内容页,或肖运华个人博客文章内容页。

. 元素高度声明的情况下在父容器中居中:绝对居中法

<div class="parent">
 <div class="absolute-center"></div>
</div>
.parent {
 position: relative;
}
.absolute-center {
 position: absolute;
 margin: auto;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 height: 70%;
 width: 70%;
}
优点:
1.跨浏览器,包括 IE8-10
2.无需其他冗余标记,CSS 代码量少
3.完美支持图片居中
4.宽度高度可变,可用百分比
缺点:
1.必须声明高度

2. 负外边距:当元素宽度高度为固定值时。设置 margin-top/margin-left 为宽度高度一 半的相反数,top:50%;left:50%

<div class="parent">
 <div class="negative-margin-center"></div>
</div>
.parent {
 position: relative;
}
.negative-margin-center {
 position: absolute;
 left: 50%;
 top: 50%;
 margin-left: -150px;
 margin-top: -150px;
 height: 300px;
 width: 300px;
}
优点:
良好的跨浏览器特性,兼容 IE6-7
代码量少
缺点:
不能自适应,不支持百分比尺寸和 min-/max-属性设置
内容可能溢出容器
边距大小域与 padding,box-sizing 有关

3. CSS3 Transform 居中:

<div class="parent">
 <div class="transform-center"></div>
</div>
.parent {
 position: relative;
}
.transform-center {
 position: absolute;
 left: 50%;
 top: 50%;
 margin: auto;
 width: 50%;
 -webkit-transform: translate(-50%, -50%);
 -moz-transform: translate(-50%, -50%);
 transform: translate(-50%, -50%);
}
优点:
内容高度可变
代码量少
缺点:
IE8 不支持
属性需要浏览器厂商前缀
可能干扰其他 transform 效果

4. table-cell 居中: