手把手教你CSS水平、垂直居中的10种方式(小结)

所属分类: 网页制作 / CSS 阅读数: 2015
收藏 0 赞 0 分享

面试必备,工作一定会用到。emmm大家都懂的,话不多说,先直接贴一个总结和效果图。

总结

  • PC端有兼容性要求,子元素宽高固定,推荐absolute + 负margin
  • PC端有兼容要求,子元素宽高不固定,推荐absolute + transform
  • PC端无兼容性要求,推荐flex
  • 移动端推荐使用flex

在这里插入图片描述

要怎么实现上面这幅图的效果呢,下面为大家总结了10中常用的方法。首先我先创建一个公共的模板样式

<template>
  <div class="two">
    <div class="parent">
      <div class="child">123</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.parent{
  margin: 0 auto;
  width: 300px;
  height: 300px;
  border: 1px solid red;
  box-sizing: border-box;
  .child {
    height: 100px;
    width: 100px;
    background: #2f8df0;
  }
}
</style>

然后具体用到的样式单独写在方法里面,首先先介绍4个平时布局的技巧。

1.水平居中div里面的div,设置子元素的宽度。

.parent{
	width:300px;
    margin:0 auto;
}

注意:如果子元素设置了display:table-cell,那么margin:0 auto;将会失效。

2.设置文字垂直居中,设置包含文字div的高。

.child{
    text-align:center
    line-height:100px //知道子元素的高,设置和高一样的高度
}

3.两个或者多个块级元素垂直居中对齐,父元素设置height和line-height相等。

 .parent{
     line-height: 300px; //知道父元素的高,设置和高一样的高度
	 }
   .child1{
    display: inline-block;
    vertical-align: middle;
    line-height: initial;  //initial 关键字用于设置 CSS 属性为它的默认值。
   }
   .child2{
    display: inline-block;
    vertical-align: middle;
    line-height: initial;  //initial 关键字用于设置 CSS 属性为它的默认值。
   }

4.让一个元素充满当前整个容器,设置absolute

.parent{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}

OK,介绍完毕,下面开始介绍CSS实现水平垂直居中的方式。

1.不需要设置子元素的宽高,需要设置父元素的高度。使用 absolute + transform (推荐)

.parent{
   position: relative 
}
.child{
    position: absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%)
}

// 备注一下,如果只需要上下居中那就只要保留top,只要左右居中的话就保留left,translate设置
   translateY(-50%)或者translateX(-50%)

2.不需要设置子元素的宽高,不需要设置父元素的宽高。 使用flex布局(建议移动端直接使用flex
pc端看需要兼容的情况。)

.parent{
  display:flex;
  align-items:center;
  justify-content:center;
}
.child{
 
}

3.不需要设置子元素的宽高,需要设置父元素的高度。使用 lineheight
注意:这种方法需要通过text-align在子元素中将文字显示重置为想要的效果

.parent{
    line-height: 300px;  //设置和父元素的高度一样
    text-align: center;
}
.child{
    display: inline-block;
    vertical-align: middle;
    line-height: initial; //initial 关键字用于设置 CSS 属性为它的默认值。
    text-align: left;  //将文字显示重置为想要的效果
}

4.不需要设置子元素的宽高,需要设置父元素的高度。使用css-table (使用之后此元素的margin:0 auto会失效)

.parent{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
.child{
  display: inline-block;
}

5.设置子元素的宽高,设置父元素的高度。 使用absolute + 负margin

.parent{
   position: relative 
}
.child{
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -50px; //知道子元素的宽高
    margin-top: -50px;  //知道子元素的宽高
}

6.设置子元素的宽高,设置父元素的高度。使用 absolute + margin auto

.parent{
   position: relative 
}
.child{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}

7.设置子元素的宽高,设置父元素的高度。 使用 absolute + calc(这种方法兼容性依赖calc的兼容性)

.parent{
   position: relative 
}
.child{
    position: absolute;
    top: calc(50% - 50px);
    left: calc(50% - 50px);
}

8.使用writing-mode(使用起来比较复杂,不推荐)

 //公共的样式在最上面
 <div class="parent">
      <div class="box-child">
        <div class="child">123</div>
      </div>
    </div>
.parent{
     writing-mode: vertical-lr; //改变文字显示的方向
    text-align: center;
}
.box-child{
    writing-mode: horizontal-tb;
    display: inline-block;
    text-align: center;
    width: 100%;
}
.child{
    text-align: left; //将文字显示重置为想要的效果
    margin: 0 auto;
}       

9.不需要设置子元素的宽高,不需要设置父元素的宽高。 使用grid布局(不建议使用,目前兼容性不是很好)

.parent{
    display: grid;
}
.child{
     align-self: center;
     justify-self: center;
}

10.使用table布局(纯粹凑方法,这年头,谁还用table布局呀,哈哈哈哈)

<table>
    <tbody>
        <tr>
            <td class="parent">
                <div class="child">123</div>
            </td>
        </tr>
    </tbody>
</table>
.parent{
    text-align: center;
}
.child{
    display: inline-block;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

js 解决隐藏与显示div的相关问题

我的导航中就有一栏产品中心下面用隐藏个div然后鼠标放上去就显示出来,但是导航那一块div一直出不来,什么都没有,很是郁闷不知道是什么原因
收藏 0 赞 0 分享

关于clearBoth在GOOGLE Chrome中的问题解决方法

下面这段CSS在IE中好好的,但在GOOGLE Chrome中总是不行,我调测了无数次。问题就出在 clearBoth 这个样式上,此问题如何解决,写下来详细介绍
收藏 0 赞 0 分享

用css margin去掉横排图片之间的间距

HTM,CSS,怎样去掉横排图片之间的间距,是我们的一大头疼问题,于是本人搜集整理一下,晒出来和大家分享,希望可以帮助你们
收藏 0 赞 0 分享

关于li:hover的怎么清除浮动问题实现代码

当鼠标移动上去时,周围显示一个方框,但是后面的会向后移动,如何才能使得当鼠标移上去时后面的li不浮动
收藏 0 赞 0 分享

ie6不支持两个连续并列class类名怎么解决

在网页布局中会使用到两个连续的class,但唯独ie6不支持,很郁闷,于是搜索整理下,晒出来和大家分享
收藏 0 赞 0 分享

如何在class内写xsl标记注意事项

想要在class中写一个xsl标记,想要知道应该如何来写出正确的代码语句,请详看本文
收藏 0 赞 0 分享

ie10 css hack 条件注释等兼容方式整理

ie10已经上线一段时间了,相信已经有一部分前端潮人体验过了,截至到现在,在ie6到ie9的浏览器各种各样的古怪行为,开发人员不得不使用条件注释,有条件的类,和其他特定于IE的css hack来解决
收藏 0 赞 0 分享

IE6双倍边距 IE6浏览器会出现双倍边距解决方法

所谓的IE6双倍边距就是指当元素有float属性,又有margin属性时,在IE6下面显示的margin的值是设置值的两倍,这个问题从有css技术时就已经诞生,本文将介绍详细解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

css 网页背景图片 怎样用CSS实现大背景网页效果

在网页设计制作中经常会遇到这样的问题:用图片做背景时,由于显示器分辨率太大或者图片尺寸太小,在页面的两边或者下部了没有背景图片,使页面变的很僵硬,于是搜集整理一些,晒出来和大家分享
收藏 0 赞 0 分享

CSS字体中英文名称对照表 CSS常用中文字体英文名称对照表

在CSS文件中,我们常看到有些字体名称变成了乱码,这是由于编写者将中文字体的名字直接写成了中文,为了避免这种状况出现,在CSS文件中使用中文字体时,最好使用中文字体的英文名称,需要的朋友可以注意下
收藏 0 赞 0 分享
查看更多