位置高度垂直居中问题

方法一:

该方法是将外部容器的显示模式设置成display:table,img标签外部再嵌套一个span标签,并设置span的显示模式为display:table-cell,这样就可以很方便的使用vertical-align象表格元素那样对齐了,当然这只是在标准浏览器下,IE6/IE7还得使用定位。
HTML代码

<div id="box">

<span><img src="images/demo.jpg" alt="" /></span>

</div>

CSS代码

<style type="text/css">

#box{ width:500px;height:400px; display:table; text-align:center; border:1px solid #d3d3d3;background:#fff; } #box span{ display:table-cell; vertical-align:middle; }

#box img{ border:1px solid #ccc; } </style>

 

<!--[if lte IE 7]> <style type="text/css"> #box{ position:relative; overflow:hidden; } #box span{ position:absolute; left:50%;top:50%; } #box img{ position:relative; left:-50%;top:-50%; } </style> <![endif]-->

 

方法二:

方法二和方法一的实现的原理大同小异,结构也是相同的,方法一用的是条件注释,方法二就用的CSS Hack。
CSS代码

<style type="text/css">
#box{
    width:500px;height:400px;
    overflow:hidden;
    position:relative;
    display:table-cell;
    text-align:center;
    vertical-align:middle;
    border:1px solid #d3d3d3;background:#fff;
}
#box span{
    position:static;
    *position:absolute; /*针对IE6/7的Hack*/
    top:50%; /*针对IE6/7的Hack*/
}
#box img {
    position:static;
    *position:relative; /*针对IE6/7的Hack*/
    top:-50%;left:-50%; /*针对IE6/7的Hack*/
    border:1px solid #ccc;
}
</style>

该方法有个弊端,在标准浏览器下由于外部容器#box的显示模式为display:table-cell,所以导致#box无法使用margin属性,并且在IE8下设置边框也无效。

方法三:

标准浏览器还是将外部容器#box的显示模式设置为display:table-cell,IE6/IE7是利用在img标签的前面插入一对空标签的办法。

HTML代码

<div id="box">

    <i></i><img src="images/demo.jpg" alt="" />

</div>

CSS代码

<style type="text/css">

#box{

width:500px;height:400px;

display:table-cell;

text-align:center;

vertical-align:middle;

border:1px solid #d3d3d3;background:#fff;

}

#box img{

border:1px solid #ccc;

}

</style>

<!--[if IE]>

<style type="text/css">

#box i {

    display:inline-block;

    height:100%;

    vertical-align:middle

    }

#box img {

    vertical-align:middle

    }

</style>

<![endif]-->

方法四:

在img标签外包裹一个p标签,标准浏览器利用p标签的伪类属性:before来实现,IE6/IE7使用了CSS表达式来实现兼容。
HTML代码

<div id="box">
    <p><img src="images/demo.jpg" alt="" /></p>
</div>

CSS代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#box{

    width:500px;height:400px;

    text-align:center;

    border:1px solid #d3d3d3;background:#fff;

}

#box p{

    width:500px;height:400px;

    line-height:400px; /* 行高等于高度 */

}

  

/* 兼容标准浏览器 */

#box p:before{

    content:"."; /* <a href="http://casinogreece.gr/">????????????</a> 具体的值与垂直居中无关,尽可能的节省字符 */

    margin-left:-5px; font-size:10px; /* 修复居中的小BUG */

    visibility:hidden; /*设置成隐藏元素*/

}

  

#box p img{

    *margin-top:expression((400 - this.height )/2); /* CSS表达式用来兼容IE6/IE7 */

    vertical-align:middle;

    border:1px solid #ccc;

}

使用:beforr这个方法对于标准浏览器来说比较给力,也没发现有副作用,但是对于IE6/IE7,如果对性能要求较高的话CSS表达式的方法要慎用。

方法五:

该方法针对IE6/IE7,将图片外部容器的字体大小设置成高度的0.873倍就可以实现居中,标准浏览器还是使用上面的方法来实现兼容,并且结构也是比较优雅。
HTML代码

<div id="box">
    <img src="images/demo.jpg" alt="" />
</div>

CSS代码

复制代码

#box{
    width:500px;height:400px;
    text-align:center;
    border:1px solid #d3d3d3;background:#fff;
    
    /* 兼容标准浏览器 */
    display: table-cell;
 vertical-align:middle;
    
    /* 兼容IE6/IE7 */
    *display:block;
    *font-size:349px; /* 字体大小约为容器高度的0.873倍 400*0.873 = 349 */
    *font-family:Arial; /* 防止非utf-8引起的hack失效问题,如gbk编码 */
}
 
#box img{
    vertical-align:middle;
}

复制代码

设置字体大小的方法感觉比较怪异,也没有看到一个合理的解释,只知道图片元素有一些不同于其他元素的特性,但是对于IE6/IE7来说,这个方法还是比较给力的。
思考:很多方法都是依赖于将外部容器的显示模式设置成table才能实现垂直居中,也就是div来模拟table。

CSS3实现:

方法1:table-cell

html结构:

<div class="box box1">		<span>垂直居中</span></div>

css:

.box1{	display: table-cell;	vertical-align: middle;	text-align: center;			}

方法2:display:flex

.box2{	display: flex;	justify-content:center;	align-items:Center;}

方法3:绝对定位和负边距

.box3{position:relative;}
.box3 span{            position: absolute;            width:100px;            height: 50px;            top:50%;            left:50%;            margin-left:-50px;            margin-top:-25px;            text-align: center;        }

方法4:绝对定位和0

.box4 span{  width: 50%;    height: 50%;    background: #000;  overflow: auto;    margin: auto;    position: absolute;    top: 0; left: 0; bottom: 0; right: 0;  }

这种方法跟上面的有些类似,但是这里是通过margin:auto和top,left,right,bottom都设置为0实现居中,很神奇吧。不过这里得确定内部元素的高度,可以用百分比,比较适合移动端。

方法5:translate

.box6 span{			position: absolute;			top:50%;			left:50%;			width:100%;			transform:translate(-50%,-50%);			text-align: center;		}

这实际上是方法3的变形,移位是通过translate来实现的。

方法6:display:inline-block

.box7{  text-align:center;   font-size:0;}.box7 span{  vertical-align:middle;   display:inline-block;   font-size:16px;}.box7:after{  content:'';  width:0;  height:100%;  display:inline-block;  vertical-align:middle;}

这种方法确实巧妙...通过:after来占位。

方法7:display:flex和margin:auto

.box8{	display: flex;	text-align: center;}.box8 span{margin: auto;}

方法8:display:-webkit-box

.box9{    display: -webkit-box;    -webkit-box-pack:center;    -webkit-box-align:center;    -webkit-box-orient: vertical;    text-align: center}

css3博大精深,可以实现很多创造性的效果,需要好好研究下。

转载于:https://my.oschina.net/u/2962717/blog/779324

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值