CSS自动控制图片大小的代码

作者:袖梨 2022-06-25

Css防止图片尺寸过大

添加如下CSS:

代码如下 复制代码

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

代码中的max-限制图片的最大宽度为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事件
//示例: ;
// 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 = "点击在新窗口中查看原图";
}

相关文章

精彩推荐