css 垂直水平居中总结

前言:



垂直居中有很多方式,我们要做的不是写出完美代码,而是在合适的情况下根据需求选择合适方式。

主要方式:

  1. line-height
  2. 绝对定位
  3. 表格 display:table-cell

主要需求:

  1. 固定宽高
  2. 不固定宽高

主要兼容:

  1. ie8+  主流浏览器
  2. ie6,7

行高



1. 利用行高与高度相同,实现单行文本居中

缺点:只能是单行文本

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5     <style type="text/css">
 6     .d1{width: 200px;height: 200px;background-color: blue;line-height: 200px;}
 7     </style>
 8 </head>
 9 <body>
10 <div class="d1">
11     fdsfdsfdsfdfsfds
12 </div>
13 </body>
14 </html>

效果预览

2.利用inline-block改进(推荐)

改变display属性,就可以是块元素了,vertical-align: middle;设置垂直属性

优点:自适应内容

兼容:>=ie8 + 主流

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5 </head>
 6 <style type="text/css">
 7     .div1{
 8         width: 500px;height: 500px;
 9         background-color: blue;
10         text-align: center;
11         line-height: 500px;
12     }
13     .div2{
14         display: inline-block;
15         vertical-align: middle;
16         width: 200px;
17         height: 200px;
18         background-color: red;
19         /*通过 line-hight 来垂直居中  此法>= ie8*/
20     }
21 </style>
22 <body>
23     <div class="div1">
24         <div class="div2">
25
26         </div>
27     </div>
28 </body>
29 </html>

效果预览

绝对定位



1.负margin

top,left 50%;margin -50%;

缺点:需固定宽高

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5 </head>
 6 <style type="text/css">
 7     .div1{
 8         width: 500px;height: 500px;
 9         background-color: blue;
10         position: relative;
11     }
12     .div2{
13         position: absolute;
14         width: 200px;
15         height: 200px;
16         background-color: red;
17         top: 50%;
18         left: 50%;
19         margin-left: -100px;   /*此处为width的一半,所以应用此法,元素必须固定宽、高*/
20         margin-top: -100px;
21
22     }
23 </style>
24 <body>
25     <div class="div1">
26         <div class="div2">
27             fdsfsdffdf
28             fdsfdsfsdff
29         </div>
30     </div>
31 </body>
32 </html>

效果预览

2.css3 translate

我们希望实现不固定宽高,在上法上改进。可以用js,动态添加宽高,但不推荐。其实可以用css3 translate属性,因为translate是唯一一个相对于自身宽度计算的属性

兼容:>=ie9 + 主流

优点:自适应内容

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5 </head>
 6 <style type="text/css">
 7     .div1{
 8         width: 500px;height: 500px;
 9         background-color: blue;
10         position: relative;
11     }
12     .div2{
13         position: absolute;
14         background-color: red;
15         top: 50%;
16         left: 50%;
17         transform:translate(-50%,-50%);
18         /*应用css3 translate属性是相对于自身的,此法>=ie9,且宽高自适应*/
19     }
20 </style>
21 <body>
22     <div class="div1">
23         <div class="div2">
24             fdsfsdffdf
25             fdsfdsfsdff
26         </div>
27     </div>
28 </body>
29 </html>

效果预览

3.绝对定位 + 相对定位(推荐)

思想与上面的方式是一样,只是这是基于ie6,7的bug,相对定位位移父元素的50%

缺点:多添加一个容器标签

优点:自适应内容

兼容:ie6,7

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5     <style type="text/css">
 6         .middle-demo-4{
 7         background-color: blue;
 8         height: 300px;
 9         width: 300px;
10         position: relative;
11         }
12         .middle-demo-4 div{
13         position: absolute;
14         top: 50%;
15         left: 50%;
16         }
17         .middle-demo-4 div div{
18             height: 200px;
19             background-color: red;
20         position: relative;
21         top: -50%;
22         left: -50%;
23         }/*ie6 ie7*/
24     </style>
25 </head>
26 <body>
27 <div class="middle-demo-4">
28     <div>
29         <div>dsfdsfdsfdsfdsfdsfdsf</div>
30     </div>
31 </div>
32
33 </body>
34 </html>

效果预览

4.margin:auto

绝对定位下,固定宽高,top:0,bottom:0,会自适应内容,多余部分会用margin填充

缺点:固定宽高

兼容:>=ie8

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5 </head>
 6 <style type="text/css">
 7     .div1{
 8         width: 500px;height: 500px;
 9         background-color: blue;
10         position: relative;
11     }
12     .div2{
13         margin: auto;
14         position: absolute;
15         background-color: red;
16         width: 200px;
17         height: 200px;
18         left: 0;
19         right: 0;
20         top: 0;
21         bottom: 0;
22
23     }
24 </style>
25 <body>
26     <div class="div1">
27         <div class="div2">
28             fdsfsdffdf
29             fdsfdsfsdff
30         </div>
31     </div>
32 </body>
33 </html>

结果预览

表格



1.table-cell(推荐)

单元格可以设置垂直属性 vertical-align: middle;

优点:自适应内容

兼容:>=ie8 +主流

缺点:子元素为浮动、绝对定位无效(注意)

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5 </head>
 6 <style type="text/css">
 7     .div1{
 8         width: 500px;height: 500px;
 9         background-color: blue;
10         display: table-cell;
11         vertical-align: middle;
12         text-align: center;
13     }
14     .div2{
15     /*float: left;position: absolute; 子元素为浮动、绝对定位无效
16         此法>=ie8
17     */}
18 </style>
19 <body>
20     <div class="div1">
21         <div class="div2">
22             fdsfsdffdf
23             fdsfdsfsdff
24         </div>
25     </div>
26 </body>
27 </html>

结果预览

总结



根据兼容性和自适应内容来考虑         表格/行高法 + 相对定位法

如果固定宽高                                负margin法

时间: 2024-07-28 13:52:47

css 垂直水平居中总结的相关文章

CSS垂直水平居中方法总结

在布局的时候经常能用到居中,在此总结一下 html结构: <div class="wrap"> <div class="content"></div> </div> 1.水平居中:margin:auto; .wrap{ width: 400px; height: 400px; background-color: lightblue; } .content{ width: 200px; height: 200px; ba

CSS垂直水平居中

通过之前总结水平居中与垂直居中的基本方法,梳理垂直水平同时居中的方法就没有那么乱了. text-align:center + line-height 如下图,div2中用text-align+line-height实现单行文本水平垂直居中. 也可以将div2设置为inline-block实现div垂直水平居中. text-align:center + vertical-align:middle 如果想让div2在div1中居中,那么需要将父元素设置text-align:center,因为居中效果

css垂直水平居中方案

1. 水平居中 如果是inline元素:在父元素上面设置text-align:center; 如果是block元素:设置宽度和margin:0 auto; 如果是多块级元素:在父元素上面设置text-align:center;所有的子元素上面设置display:inline-block; 2. 垂直居中相对于水平居中要复杂些,结合自己的需求自己对待 不知道元素具体的高:可以根据padding-top和padding-bottom来 不知道元素具体的高:父元素设置成display:table,子元

关于css垂直水平居中的几种方法

第一种, 针对内联元素居中. <div class="box box1"> <span class="test">垂直居中</span> </div> <style> .box{ width: 200px; height: 200px; border:1px solid red; } .box1{ display: table-cell; vertical-align: middle; text-align

css实现浏览器垂直水平居中效果代码

css实现浏览器垂直水平居中效果代码:可能是由于居中相对美观一些吧,所以一些弹窗或者功能模块出现的位置都是位于网页中间,下面就介绍一端利用css实现的将div实现垂直水平居中的效果,代码如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/

css如何实现未知宽高div中图片垂直水平居中效果

css如何实现未知宽高div中图片垂直水平居中效果:在有时候可能有这样的情况,那就是想让一个图片在div中实现垂直水平居中效果,但是有时候div的尺寸是位置的,下面通过代码实例介绍一下在这种情况下如何实现图片的垂直水平居中效果.代码如下:实例一: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content=&q

css 中让盒子垂直水平居中

css中如何让div盒子垂直水平居中 整体代码如下:(仅供参考) <!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8"> <style type="text/css"> .box{ width: 200px; height: 200px; position:absolute; left:50%; top:50%; mar

css实现图片在div中垂直水平居中代码实例

css实现图片在div中垂直水平居中代码实例:让图片在一个元素中垂直水平居中这样的需求在实际应用中还是很多的,下面就通过代码实例介绍一下如何实现此效果.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" />

div+css实现未知宽高元素垂直水平居中

div+css实现未知宽高元素垂直水平居中.很多同学在面试的时候都会遇到这样的问题:怎么用div+css的方法实现一个未知宽高的弹出框垂直水平居中??如果用JS的话就好办了,但是JS的使用会对页面性能造成影响,而且能用CSS实现的干嘛用JS呢,嘿嘿 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>div+css实现未