CSS中高度不确定如何让元素垂直居中
Admin 2022-06-08 群英技术资讯 1212 次浏览
这篇文章给大家分享的是CSS中高度不确定如何让元素垂直居中。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。本文主要介绍了CSS未知高度垂直居中的实现,分享给大家,具体如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=8" http-equiv="X-UA-Compatible"/>
<title> CSS垂直居中</title>
<style type="text/css">
.container{
width:500px;/*装饰*/
height:500px;
background:#B9D6FF;
border: 1px solid #CCC;
}
</style>
</head>
<body>
<h1>垂直居中(table)</h1>
<div class='container'>
<table width="100%" height="100%">
<tr>
<td align="center" valign="middle">
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
</td>
</tr>
</table>
</div>
</body>
</html>
好了,我们看其CSS实现。凡是table能做到的,CSS都能做的,但各浏览器在CSS的差异比较大,因此要兼容它们难度很大。这涉及许多细节,各种流啊,display的表现效果与CSS hack,IE早些年搞了大堆的私有属性,这也有待我们深一步挖掘。我们先看最简单的实现,背景图片法
背景图片法

<!doctype html>
<html>
<head>
<title> CSS垂直居中</title>
<style type="text/css">
.container {
width:500px;
height:500px;
line-height:500px;
background:#B9D6FF url(http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg) no-repeat center center;
border:1px solid #f00;
text-align: center;
}
</style>
</head>
<body>
<h1>垂直居中</h1>
<div class="container">
</div>
</body>
</html>
CSS表达式法
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=8" http-equiv="X-UA-Compatible"/>
<title>司徒正美 CSS垂直居中</title>
<style type="text/css">
.container{
/*IE8与标准游览器垂直对齐*/
display: table-cell;
vertical-align:middle;
width:500px;/*装饰*/
height:500px;
background:#B9D6FF;
border: 1px solid #CCC;
}
.container img{
display:block;/*让其具备盒子模型*/
margin:0 auto;
text-align:center;
margin-top:expression((500 - this.height )/2);/*让IE567垂直对齐 */
}
</style>
</head>
<body>
<h1>垂直居中(CSS表达式)</h1>
<div class="container">
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
</div>
</body>
</html>
绝对定位法
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=8" http-equiv="X-UA-Compatible"/>
<title>司徒正美 CSS垂直居中</title>
<style type="text/css">
div {
/*IE8与标准游览器垂直对齐*/
display:table-cell;
vertical-align:middle;
overflow:hidden;
position:relative;
text-align:center;
width:500px;/*装饰*/
height:500px;
border:1px solid #ccc;
background:#B9D6FF;
}
div p {
+position:absolute;
top:50%
}
img {
+position:relative;
top:-50%;
left:-50%;
}
</style>
</head>
<body>
<h1>垂直居中(绝对定位)</h1>
<div class="container">
<p>
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
</p>
</div>
</body>
</html>
display:inline-block法
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=8" http-equiv="X-UA-Compatible"/>
<title>司徒正美 CSS垂直居中</title>
<style type="text/css">
div {
display:table-cell;
vertical-align:middle;
text-align:center;
width:500px;
height:500px;
background:#B9D6FF;
border: 1px solid #CCC;
}
</style>
<!--[if IE]>
<style type="text/css">
i {
display:inline-block;
height:100%;
vertical-align:middle
}
img {
vertical-align:middle
}
</style>
<![endif]-->
</head>
<body>
<h1>垂直居中(inline-block法)</h1>
<div class="container">
<i></i>
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
</div>
</body>
</html>
writing-mode法
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=8" http-equiv="X-UA-Compatible"/>
<title> CSS垂直居中</title>
<style type="text/css">
div{
width:500px;
height:500px;
line-height:500px;
text-align:center;
background:#B9D6FF;
border:1px solid #f00;
}
div span{
height:100%\9;
writing-mode:tb-rl\9;
}
div img{
vertical-align:middle
}
</style>
</head>
<body>
<h1>垂直居中(writing-mode法)</h1>
<div class="container">
<span>
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
</span>
</div>
</body>
</html>

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了css模块化方案,css的模块化方案可能和js的一样多,下面简单介绍几种主要的模块方案,非常具有实用价值,需要的朋友可以参考下
HTML中form表单标签的简单使用是怎样?今天来学习下关于HTML中的form表单标签的相关内容,在HTML标记语言中经常用到,如果你是小白的话,小编接下来分享的内容你一定不要错过了。
本文主要介绍了使用canvas仿Echarts实现金字塔图的实例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
HTML中相对位置与绝对位置是怎样,怎么使用?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
table表格设置边框后的重叠问题table{ border-spacing: 0; border-collapse: collapse;}table td { border: 1px solid #000; padding: 20px 30px;}在table表格中
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008