css 图片等比例缩放

CSS等比例缩小图片,实用性较强的一个CSS技巧,等比例一般都是在我们规定的高度或宽度超出范围时候,另图片以高或宽为基准按比例缩小或放大,这样保证网页布局的整齐性,这也是CSS功能的强大之处。不过本处是CSS调用了expression,也就是说用CSS调用了JavaScript,有点不爽。

<!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">
<title>css等比例缩小图片-www.zzarea.com</title>
<head>
<style type="text/css">
<!–
body {
text-align: center;
margin: 20px auto;
padding: 0px;
font-size:14px;
color:red
}
#pic{
  margin:5px auto;
  width:500px;
  padding:0;
  border:1px solid #333;
  }
#pic img{
max-width:200px;
width:expression(document.body.clientWidth > 200?"200px":"auto" );
display:block;
border:0
}

–>
</style>
</head>
<body>将宽度强制为200px后图片高度被等比例缩小
<div id="pic">
<a href="#"><img src="/jscss/demoimg/wall5.jpg" /></a>
</div>
原图:
<br />
<img src="/jscss/demoimg/wall5.jpg" />
</body>

css 图片等比例缩放  方法2:

按比例缩小或者放大到某个尺寸,对于标准浏览器(如Firefox),或者最新都IE7浏览器,
直接使用max-width,max-height;或者min-width,min-height的CSS属性即可。如:

img{max-width:100px;max-height:100px;}
img{min-width:100px;min-height:100px;}
对于IE6及其以下版本的浏览器,则可以利用其支持的expression属性,在css code中嵌入javascript code
来实现图片的缩放

.thumbImage {max-width: 100px;max-height: 100px;} /* for Firefox & IE7 */
* html .thumbImage { /* for IE6 */
width: expression(this.width > 100 && this.width > this.height ? 100 : auto);
height: expression(this.height > 100 ? 100 : auto);
}

本文链接:https://www.dnwfb.com/1006.html,转载请注明出处。
0

评论0

没有账号? 注册  忘记密码?