CSS实现水平居中和垂直居中
在前端开发中,元素的居中是经常会遇到的问题,网上也有许多的例子实现,这里总结一下,归纳出我常用的几种方法。
水平居中的几种方式
1、text-align 方式
text-align是前端常用的属性,text-align 属性规定元素中的文本的水平对齐方式,它居中的是文本或者图片对象,其实对块状元素并没有影响。
<body>
<div style="width:500px;height:100px;background: #d5e2e8;text-align: center;">
<div style="height: 50px;width:300px;background: #e0c99d;">居中</div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;text-align: center;">
<span>居中</span>
<span>居中</span>
</div>
</body>
页面效果:
2、margin auto 方式
当父元素宽度固定,通过设置子元素的margin属性也可以实现元素的居中。**注意:**该设置只对块状元素生效,对于块状元素,如果不设置宽度,块状元素将撑满整行。
<body>
<div style="width:500px;height:100px;background: #d5e2e8;">
<div style="height: 30px;width:300px;background: #e0c99d;margin: 0 auto;"></div>
<br>
<div style="height: 30px;background: #e0c99d;margin: 0 auto;"></div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;">
<span style="margin: 0 auto;display: block;width:100px;background: #e0c99d">居中</span>
<span style="margin: 0 auto;background: #e0c99d">居中</span>
</div>
</body>
页面效果:
3、position+transform 方式
采用position+transform 方式,对块状元素和非块状元素都能达到居中的效果
<div style="width:500px;height:100px;background: #d5e2e8;position: relative;">
<div
style="height: 30px;width:300px;background: #e0c99d;position: absolute;left: 50%;transform: translate(-50%);">
</div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;position: relative;">
<span style="background: #e0c99d;position: absolute;left: 50%;transform: translate(-50%);">居中</span>
</div>
页面效果:
4、flex方式
flex布局是我常用的一种布局方式,实现起来也很简单,只需要将justify-content属性设置为center即可。**注意:**当子元素没有明确高度时,子元素会根据父元素的高度进行拉伸。
<div style="width:500px;height:100px;background: #d5e2e8;display: flex;justify-content: center;">
<div style="height: 30px;width:300px;background: #e0c99d;"></div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;display: inline-flex;justify-content: center;">
<span style="background: #e0c99d;">居中</span>
</div>
页面效果:
垂直居中的几种方式
1、line-height方式
line-height适用于容器内只有一行行内元素的情况,只需要将父元素的line-height属性设置成与父元素的height高度相等即可实现。
<body>
<div style="width:500px;height:100px;background: #d5e2e8;line-height: 100px;">
<span>居中</span>
</div>
</body>
2、vertical-align方式
将父元素的display属性设置为table-cell,设置父元素的vertical-align属性为middle,即可实现垂直居中。
<body>
<div style="width:500px;height:100px;background: #d5e2e8;display: table-cell;vertical-align: middle;">
<div style="height: 30px;width:50px;background: #e0c99d;"></div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;display: table-cell;vertical-align: middle;">
<span style="background: #e0c99d;">居中</span>
</div>
</body>
页面效果:
3、position+margin
position+margin这种方式又分高度已知和高度未知,这里只介绍一种方法,对两种情况均适用。需要注意的是,当子元素为行内元素时,如果不设置子元素的高度,子元素将会拉伸到与父元素等高的高度。
<div style="width:500px;height:100px;background: #d5e2e8;position: relative;">
<div style="height: 30px;width:50px;background: #e0c99d;position: absolute;top: 0;bottom: 0;margin: auto;">
</div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;position: relative;">
<span style="height: 30px;background: #e0c99d;position: absolute;top: 0;bottom: 0;margin: auto;">居中</span>
</div>
页面效果:
4、position+transform
这种方式与之前介绍的position+transform设置水平居中原理是相同的。
<body>
<div style="width:500px;height:100px;background: #d5e2e8;position: relative;">
<div
style="height: 30px;width:50px;background: #e0c99d;position: absolute;top: 50%;left:0;transform: translate(0,-50%);">
</div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;position: relative;">
<span style="background: #e0c99d;position: absolute;top: 50%;left:0;transform: translate(0,-50%);">居中</span>
</div>
</body>
页面效果:
5、flex方式
flex布局很简单也很使用,不了解的可以去学习一下。只需要将父元素的display设置为flex,再设置align-items为center即可**注意:**当子元素没有明确高度时,子元素会根据父元素的高度进行拉伸。
<body>
<div style="width:500px;height:100px;background: #d5e2e8;display: flex;align-items: center;">
<div style="height: 30px;width:50px;background: #e0c99d;"></div>
</div>
<br>
<div style="width:500px;height:100px;background: #d5e2e8;display: flex;align-items: center;">
<span style="height:30px;background: #e0c99d;align-items: center;">居中</span>
</div>
</body>
页面效果: