整合营销服务商

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

免费咨询热线:

CSS 水平居中方式三

CSS 水平居中方式三
<div id="parent">
<!-- 定义子级元素 -->
<div id="child">居中布局</div>
</div>

过以下CSS样式代码实现水平方向居中布局效果

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

优点:

父级元素是否脱离文档流, 不影响子集元素水平居中效果

缺点:transform属性是CSS3中新增属性, 浏览器支持情况不好


、CSS 垂直居中

1、父元素display:table-cell;vertical-align:center,里面的子元素就会实现垂直居中,不需要知道子元素的宽高

/* HTML */
<div class='father'>
  <div class='son'></div>
</div>
<style>
  .father {
	display: table-cell;
	vertical-align: middle;
	width: 300px;
	height: 300px;
	border: 3px solid red;
  }
  .son {
	width: 50px;
	height: 50px;
	background-color: aqua;
  }
</style>
复制代码
  • 效果展示


2、absolute+margin:auto,定位为 absolute 的元素垂直居中,不需要知道该元素的宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	position: relative;
	width: 300px;
	height: 300px;
	border: 3px solid red;
  }
  .son {
	position: absolute;
	background-color: aqua;
	width: 50px;
	height: 50px;
	top: 0;
	bottom: 0;
	margin: auto;
  }
</style>
复制代码
  • 效果展示


3、absolute+负margin,定位为 absolute 的元素垂直居中,需要知道该元素的宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	position: relative;
	width: 300px;
	height: 300px;
	border: 3px solid red;
  }
  .son {
	position: absolute;
	width: 100px;
	height: 100px;
	background-color: aqua;
	top: 50%;
	/* 负margin须是高度的一半 */
	margin-top: -50px;
  }
</style>
复制代码
  • 效果展示


4、absolute+calc(css3计算属性),定位为 absolute 的元素垂直居中,需要知道该元素的宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	position: relative;
	width: 300px;
	height: 300px;
	border: 3px solid red;
  }
  .son {
	position: absolute;
	width: 100px;
	height: 100px;
	background-color: aqua;
	/* 注意"-"两边要隔开 减去的须是高度的一半*/
	top: calc(50% - 50px);
  }
</style>
复制代码
  • 效果展示


5、absolute+transform,定位为 absolute 的元素垂直居中,不需要知道元素的宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	position: relative;
	width: 300px;
	height: 300px;
	border: 3px solid red;
  }
  .son {
	position: absolute;
	width: 100px;
	height: 100px;
	background-color: aqua;
	top: 50%;
	transform: translateY(-50%);
  }
</style>
复制代码
  • 效果展示


6、line-height,父元素:line-height=height。子元素:display:inline-block。子元素垂直居中,不需要知道子元素的宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	width: 300px;
	height: 300px;
	border: 3px solid red;
	line-height: 300px;
  }
  .son {
	background-color: aqua;
	width: 100px;
	height: 100px;
	display: inline-block;
	vertical-align: middle;
  }
</style>
复制代码
  • 效果展示


7、flex,目前主流的布局方案,父元素为 flex 容器且添加 align-items: center,控制子元素的布局。不需要知道子元素的宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	width: 300px;
	height: 300px;
	border: 3px solid red;
	display: flex;
	align-items: center;
  }
  .son {
	background-color: aqua;
	width: 100px;
	height: 100px;
  }
</style>
复制代码
  • 效果展示

8、grid ,目前最强大的布局方案,使用还尚未流行。父元素为 grid,子元素添加 align-self: center。不需要知道子元素的宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	width: 300px;
	height: 300px;
	border: 3px solid red;
	display: grid;
  }
  .son {
	background-color: aqua;
	width: 100px;
	height: 100px;
	align-self: center;
  }
</style>
复制代码
  • 效果展示


9、伪元素after或before,这是我搜出来整理的。CSS 真的太神(s)奇(d)了,毫无道理。子元素垂直居中不需要知道宽高

<!-- HTMl -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	width: 300px;
	height: 300px;
	border: 3px solid red;
	display: block;
  }
  .father::after {
	content: "";
	display: inline-block;
	vertical-align: middle;
	height: 100%;
  }
  .son {
	background-color: aqua;
	width: 50px;
	height: 50px;
	display: inline-block;
	vertical-align: middle;
  }
</style>
复制代码
  • 效果展示


10、隐藏节点(盒子)实现 该原理就是使用盒子占位置,但不显示出该盒子。另外的盒子垂直居中,子盒子的宽高需由实际计算时确定

<!-- HTML -->
<div class="father">
	<div class="hide"></div>
	<div class="son"></div>
</div>
<style>
  .father {
	width: 300px;
	height: 300px;
	border: 3px solid red;
  }
  .son {
	background-color: aqua;
	width: 50%;
	height: 50%;
  }
  .hide {
	width: 50px;
	height: 25%;
 }
</style>
复制代码
  • 效果展示


11、writing-mode,这是搜索整理而来,参考资料见最后。子元素盒子 display: inline-block。子元素垂直居中,不需要知道该盒子的宽高

<!-- HTML -->
<div class="father">
	<div class="son"></div>
</div>
<style>
  .father {
	width: 300px;
	height: 300px;
	border: 3px solid red;
	writing-mode: vertical-lr;
	text-align: center;
  }
  .son {
	background-color: aqua;
	width: 100px;
	height: 100px;
	writing-mode: horizontal-tb;
	display: inline-block;
  }
</style>
复制代码
  • 效果展示


三、参考资料


作者:soloplayer
链接:https://juejin.cn/post/6904138129612439560
来源:掘金

平居中

1) 若是行内元素, 给其父元素设置 text-align:center,即可实现行内元素水平居中.

2) 若是块级元素, 该元素设置 margin:0 auto即可.

3) 若子元素包含 float:left 属性, 为了让子元素水平居中, 则可让父元素宽度设置为fit-content,并且配合margin, 作如下设置:

.parent{ 
width: -moz-fit-content; 
width: -webkit-fit-content; 
width:fit-content; 
margin:0 auto;
}

fit-content是CSS3中给width属性新加的一个属性值,它配合margin可以轻松实现水平居中, 目前只支持Chrome 和 Firefox浏览器.

4) 使用flex 2012年版本布局, 可以轻松的实现水平居中, 子元素设置如下:

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

5) 使用flex 2009年版本, 父元素display: box;box-pack: center;如下设置:

.parent { 
display: -webkit-box; 
-webkit-box-orient: horizontal; 
-webkit-box-pack: center; 
display: -moz-box; 
-moz-box-orient: horizontal; 
-moz-box-pack: center; 
display: -o-box; 
-o-box-orient: horizontal; 
-o-box-pack: center; 
display: -ms-box; 
-ms-box-orient: horizontal; 
-ms-box-pack: center; 
display: box; 
box-orient: horizontal; 
box-pack: center;
}

6) 使用CSS3中新增的transform属性, 子元素设置如下:

.son{ 
position:absolute; 
left:50%; 
transform:translate(-50%,0);
}

7) 使用绝对定位方式, 以及负值的margin-left, 子元素设置如下:

.son{ 
position:absolute; 
width:固定; 
left:50%; 
margin-left:-0.5宽度;
}

8) 使用绝对定位方式, 以及left:0;right:0;margin:0 auto; 子元素设置如下:

.son{
 position:absolute; 
 width:固定; 
 left:0; 
 right:0; 
 margin:0 auto;
 }

垂直居中

单行文本

1) 若元素是单行文本, 则可设置 line-height 等于父元素高度

行内块级元素

2) 若元素是行内块级元素, 基本思想是使用display: inline-block, vertical-align: middle和一个伪元素让内容块处于容器中央.

.parent::after, .son{ 
display:inline-block; 
vertical-align:middle;
}

这是一种很流行的方法, 也适应IE7.

元素高度不定

3) 可用 vertical-align 属性, 而vertical-align只有在父层为 td 或者 th 时, 才会生效, 对于其他块级元素, 例如 div、p 等, 默认情况是不支持的. 为了使用vertical-align, 我们需要设置父元素display:table, 子元素 display:table-cell;vertical-align:middle;

优点

元素高度可以动态改变, 不需再CSS中定义, 如果父元素没有足够空间时, 该元素内容也不会被截断.

缺点

IE6~7, 甚至IE8 beta中无效.

4) 可用 Flex 2012版, 这是CSS布局未来的趋势. Flexbox是CSS3新增属性, 设计初衷是为了解决像垂直居中这样的常见布局问题. 相关的文章如《弹性盒模型Flex指南》

父元素做如下设置即可保证子元素垂直居中:

.parent { display: flex; align-items: center;

优点

  • 内容块的宽高任意, 优雅的溢出.

  • 可用于更复杂高级的布局技术中.

缺点

  • IE8/IE9不支持

  • 需要浏览器厂商前缀

  • 渲染上可能会有一些问题

5) 使用flex 2009版.

.parent { 
display: box; 
box-orient: vertical; 
box-pack: center;
}

优点

实现简单, 扩展性强

缺点

兼容性差, 不支持IE

6) 可用 transform , 设置父元素相对定位(position:relative), 子元素如下css样式:

.son{ 
position:absolute; 
top:50%; 
-webkit-transform: 
translate(-50%,-50%);
}

优点

代码量少

缺点

IE8不支持, 属性需要追加浏览器厂商前缀, 可能干扰其他 transform 效果, 某些情形下会出现文本或元素边界渲染模糊的现象.

元素高度固定

7) 设置父元素相对定位(position:relative), 子元素如下css样式:

.son{ 
position:absolute; 
top:50%; 
height:固定; 
margin-top:-0.5高度;
}

优点

适用于所有浏览器.

缺点

父元素空间不够时, 子元素可能不可见(当浏览器窗口缩小时,滚动条不出现时).如果子元素设置了overflow:auto, 则高度不够时, 会出现滚动条.

8) 设置父元素相对定位(position:relative), 子元素如下css样式:

.son{ 
position:absolute;
height:固定; 
top:0; 
bottom:0; 
margin:auto 0;
}

优点

简单

缺点

没有足够空间时, 子元素会被截断, 但不会有滚动条.

总结

水平居中较为简单, 共提供了8种方法, 一般情况下 text-align:center,marin:0 auto; 足矣

  • ① text-align:center;

  • ② margin:0 auto;

  • ③ width:fit-content;

  • ④ flex

  • ⑤ 盒模型

  • ⑥ transform

  • ⑦ ⑧ 两种不同的绝对定位方法

垂直居中, 共提供了8种方法.

  • ① 单行文本, line-height

  • ② 行内块级元素, 使用 display: inline-block, vertical-align: middle; 加上伪元素辅助实现

  • ③ vertical-align

  • ④ flex

  • ⑤ 盒模型

  • ⑥ transform

  • ⑦ ⑧ 两种不同的绝对定位方法

我们发现, flex, 盒模型, transform, 绝对定位, 这几种方法同时适用于水平居中和垂直居中.

希望对大家有所帮助.