您现在的位置:首页 >> 软件开发 >> 内容

CSS下两种图片垂直居中方法

时间:2023-12-12 23:13:44 点击:

  核心提示:方法一:!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/x...

方法一:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>利用定位来显示垂直局中的图片</title>


<style type="text/css">
.miao {
  width:500px;
  height:220px;
  line-height:220px;
  border: 1px solid;
  text-align: center;
}
.miao img {
  vertical-align: middle;
}



</style>


</head>
<body>


<h1>固定高宽的容器中,图片垂直局中。</h1>
<p>使用的绝对定位和相对定位的方法</p>
<div class="miao">
 <span><img src="nnn.jpg" width=200px alt="Logo" /></span>你知道的太多了
</div>


 


方法二:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>利用定位来显示垂直局中的图片</title>


<style type="text/css">
.miao{border:solid 1px red;width:170px;height:100px;text-align:center;position:relative;overflow:hidden;}
.miao span{position:absolute;left:50%;top:50%; border:solid 1px green;}
.miao span img{position:relative;left:-50%;top:-50%;border:solid 1px red;}
</style>


</head>
<body>
<h1>固定高宽的容器中,图片垂直局中。</h1>
<p>绿色容器是span,目的是使自己的左上角与容器中心点对齐。红色是具体图片,再次设置负值使自己的中心点和父容器的中心点重合,最终达到垂直局中的目的。</p>



<div class="miao" style="width:400px;height:220px;">
 <span><img src="qb.jpg" alt="Google" /></span>
</div>


</body>
</html>

作者:不详 来源:网络
相关文章
  • 没有相关文章
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 陈工笔记(www.dui580.com) © 2024 版权所有 All Rights Reserved.
  • 站长:陈工 微信号:chengongbiji QQ:24498854
  • Powered by 陈工