DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> CSS入門知識 >> CSS詳解 >> CSS設置未知大小圖片在已知大小容器水平垂直居中
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="/School/UploadFiles_7810/201603/20160324210739749.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739731.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739749.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739731.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739749.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739731.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739749.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739731.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739749.gif" />
</div>
<div class="box">
<img src="/School/UploadFiles_7810/201603/20160324210739731.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>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved