CSS自動控製圖片大小的代碼

來源:互聯網
上載者:User

Css防止圖片尺寸過大

添加如下CSS:

 代碼如下 複製代碼

img { 
max-width: 800px; 
height: auto; 

代碼中的max-width:800px限制圖片的最大寬度為800像素,而下面的hight:auto很關鍵,可以保證圖片有正確的長寬比,不至於因為被調整寬度而變形。

實用例子

WordPress自動調整圖片大小

1、開啟你的“樣式表 (style.css)”檔案,然後在 p img{ 或類似的地方添加下列代碼(可以將所有550改成你想要的寬度)

 代碼如下 複製代碼

p img{
max-width: 550px;
width: expression(this.width > 550 ? "550px" : true);
height: auto;
}

2、清空緩衝就可以啦! 
3、同樣對於某些老版本IE不支援。

如果你想兼職所有瀏覽器jq或js是最好的辦法

 代碼如下 複製代碼

//  方法:setSelectReadOnly  用於設定極select控制項ReadOnly,
//這個一個類比唯讀不是真的唯讀
//使用了onbeforeactivate,onfocus,onmouseover,onmouseout事件
//樣本:< img src='img.jpg' onload='ImgAutoSize(ImgD,FitWidth,FitHeight)' > ;
//  create by sl 
// ---------------------------------------------------
function ImgAutoSize(imgD,FitWidth,FitHeight) 
{
var image1=new Image(); 
image1.onload = function ()
{
if(this.width>0 && this.height>0) 

if(this.width/this.height>= FitWidth/FitHeight) 

if(this.width>FitWidth) 

imgD.width=FitWidth; 
imgD.height=(this.height*FitWidth)/this.width; 

else 

imgD.width=this.width; 
imgD.height=this.height; 


else 

if(this.height>FitHeight) 

imgD.height=FitHeight; 
imgD.width=(this.width*FitHeight)/this.height; 

else 

imgD.width=this.width; 
imgD.height=this.height; 


}
image1 = null;
}

image1.src=imgD.src; 
imgD.style.cursor = 'hand';
imgD.onclick= function(){openWin(this.src,'imgphoto',600,400)};
imgD.title = "點擊在新視窗中查看原圖";
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.