温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS怎么设置未知大小图片在已知大小容器水平垂直居中

发布时间:2021-07-27 22:05:06 来源:亿速云 阅读:149 作者:chen 栏目:web开发

本篇内容主要讲解“CSS怎么设置未知大小图片在已知大小容器水平垂直居中”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“CSS怎么设置未知大小图片在已知大小容器水平垂直居中”吧!


代码如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cn" >
<head>
<title>未知大小图片在已知大小容器水平/垂直居中</title>
<style type="text/css">
.box {
/*非IE的主流浏览器识别的垂直居中的方法*/
display: table-cell;
vertical-align:middle;
/*设置水平居中*/
text-align:center;
/* 针对IE的Hack */
*display: block;
*font-size: 175px;/*约为高度的0.873,200*0.873 约为175*/
*font-family:Arial;/*防止非utf-8引起的hack失效问题,如gbk编码*/
width:200px;
height:200px;
border: 1px solid #EEE;
float:left;
}
.box img {
/*设置图片垂直居中*/
vertical-align:middle;
}
</style>
<body onload="int()">
<div class="box">
<img src="https://cache.yisu.com/upload/information/20210311/295/14159.gif" />
</div>
<div class="box">
<img src="http://bbs.blueidea.com/images/default/logo.gif" />
</div>
<div class="box">
<img src="https://cache.yisu.com/upload/information/20210311/295/14159.gif" />
</div>
<div class="box">
<img src="http://bbs.blueidea.com/images/default/logo.gif" />
</div>
<div class="box">
<img src="https://cache.yisu.com/upload/information/20210311/295/14159.gif" />
</div>
<div class="box">
<img src="http://bbs.blueidea.com/images/default/logo.gif" />
</div>
<div class="box">
<img src="https://cache.yisu.com/upload/information/20210311/295/14159.gif" />
</div>
<div class="box">
<img src="http://bbs.blueidea.com/images/default/logo.gif" />
</div>
<div class="box">
<img src="https://cache.yisu.com/upload/information/20210311/295/14159.gif" />
</div>
<div class="box">
<img src="http://bbs.blueidea.com/images/default/logo.gif" />
</div>
<script language="javascript">
<!--
function int(){
obj=document.getElementsByTagName("img");
for(var i=0;i<obj.length;i++){
imgnum=obj[i];
size(imgnum);
}
}
function size(_imgnum){
w=parseInt(_imgnum.offsetWidth);
h=parseInt(_imgnum.offsetHeight);
s=w/h;
if(w>200){
w=200;
h=200/s;
_imgnum.style.width=w+"px";
_imgnum.style.height=h+"px";
}
if(h>200){
h=200;
w=200*s;
_imgnum.style.width=w+"px";
_imgnum.style.height=h+"px";
}
}
//-->
</script>
</body>
</html>

到此,相信大家对“CSS怎么设置未知大小图片在已知大小容器水平垂直居中”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI