千家信息网

如何使用纯CSS实现图片水平垂直居中于DIV

发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,这篇文章主要为大家展示了"如何使用纯CSS实现图片水平垂直居中于DIV",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用纯CSS实现图片水平垂直居中于
千家信息网最后更新 2024年10月22日如何使用纯CSS实现图片水平垂直居中于DIV

这篇文章主要为大家展示了"如何使用纯CSS实现图片水平垂直居中于DIV",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用纯CSS实现图片水平垂直居中于DIV"这篇文章吧。

HTML代码:

CSS代码:

/*For Firefox Chrome*/

.demo{border:1px #ddd solid;width:208px;height:148px;overflow:hidden;text-align:center;display:table;float:left;margin:50px;position:relative;}

.demo a{display:table-cell;vertical-align:middle;width:200px;height:140px;}

.demo a img{border:1px #ddd solid;margin:0 auto;max-width:200px;max-height:140px;}

/*For IE7*/

*+html .demo a{position:absolute;top:50%;width:100%;text-align:center;height:auto;}

*+html .demo a img{position:relative;top:-50%;left:-50%;}

/*For IE6*/

*html .demo a{position:absolute;top:51%;width:100%;text-align:center;height:auto;display:block;}

*html .demo a img{position:relative;top:-50%;left:-50%;width:_expression(this.width>200?"200px":"auto");height:_expression(this.height>140?"140px":"auto");}

其中For IE6中的css有这么一段:

width:_expression(this.width>200?"200px":"auto");height:_expression(this.height>140?"140px":"auto");

这是限制IE6下图片的最大宽和最大高,就像非IE6下的:

max-width:200px;max-height:140px;

是一个道理。

以上是"如何使用纯CSS实现图片水平垂直居中于DIV"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0