用CSS如何控制网页中图片自适应大小

 

<script language=JavaScript src="/ad_js.php?n=contentad" mce_src="ad_js.php?n=contentad"></script> <script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript mce_src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script>window.google_render_ad();</script>

图片自动适应大小是一个非常常用的功能,在进行制作的时候为了防止图片撑开容器而对图片的尺寸进行必要的控制,我们可不可以用CSS控制图片使它自适应大小呢?

我们想到了一个比较简单的解决方法,虽然不是非常的完美,如果您的要求不是非常高,已经可以满足你的需要了。我们看下面的代码:

 

 

以下为引用的内容:

div img {
 max-width:600px;
 width:600px;
 width:expression(document.body.clientWidth>600?"600px":"auto");
 overflow:hidden;
}

 ◎ max-width:600px; 在IE7、FF等其他非IE浏览器下最大宽度为600px。但在IE6中无效。


 ◎ width:600px; 在所有浏览器中图片的大小为600px;

 


 ◎ 当图片大小大于600px,自动缩小为600px。在IE6中有效。


 ◎ overflow:hidden; 超出的部分隐藏,避免控制图片大小失败而引起的撑开变形。

 

2.

     放入head里面  
  <script   language="JavaScript">  
  <!--  
  var   flag=false;  
  function   DrawImage(ImgD){  
        var   image=new   Image();  
        image.src=ImgD.src;  
        if(image.width>0   &&   image.height>0){  
          flag=true;  
          if(image.width/image.height>=   164/112){  
            if(image.width>164){      
            ImgD.width=164;  
            ImgD.height=(image.height*164)/image.width;  
            }else{  
            ImgD.width=image.width;      
            ImgD.height=image.height;  
            }  
            ImgD.alt=image.width+"×"+image.height;  
            }  
          else{  
            if(image.height>112){      
            ImgD.height=112;  
            ImgD.width=(image.width*112)/image.height;            
            }else{  
            ImgD.width=image.width;      
            ImgD.height=image.height;  
            }  
            ImgD.alt=image.width+"×"+image.height;  
            }  
          }  
  }    
  //-->  
  </script>  
   
  图片调用  
  <a   href="<%=trim(rs("picurl"))%>"   target="_blank"><img   src="<%=trim(rs("imgurl"))%>"   border="0"   width="164"   height="112"   οnlοad="javascript:DrawImage(this);"></a>  
   
  这样可以实现等比例缩放!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值