<img src="images/pic.png" width="100" height="100" />
或者:
<img src="images/pic.png" style="width:100pxheight:100px" />
当页面中图片非常多,且要求每张图片的大小依据其父容器来固定怎么办?可以使用max-weight:
img {max-weight:100%}
这样图片会自动缩放到和其父容器等宽。
<div><img src="1.jpg" width="146px" height="145">
<img src="2.jpg" width="246px" height="311">
<img src="3.jpg" width="346px" height="345">
<img src="4.jpg" width="266px" height="215">
...
</div>
<script>
window.onload=function(){
var imgs=document.getElementsByTagName("img")
for(var i=0i<imgs.lengthi++){
imgs[i].width=300
imgs[i].removeAttribute("height")
}
}
</script>