将高度设置为auto
,这将保持相对于宽度的比率。此外,HTML属性不需要“px”单位,这是CSS。
<img src="someimage.jpg" width="300" height="auto" alt="Some image">
<img src="someimage.jpg" width="600" height="auto" alt="Some image">
<img src="someimage.jpg" width="900" height="auto" alt="Some image">
<img src="someimage.jpg" width="1800" height="auto" alt="Some image">
您已标记此javascript。如果您想用JavaScript更改大小,则需要先使用id引用图像,然后再更改属性。
<script>
window.onload = function(){
document.getElementById('testimg').width = 500;
}
</script>
<img id="testimg" src="http://farm9.staticflickr.com/8426/7758832526_cc8f681e48.jpg" height="auto">
如果你想要的形象去放大或缩小与窗口,就像在一个弹出窗口,设置宽度为100%
并保留高度auto
。这将使图像符合其包含的元素。
类似于'document.getElementById('middle-logo')。style.width ='300px''和'document.getElementById('middle-logo')。style.height ='300px''你是什么寻找?如果是,请阅读更多细节[这里](https://developer.mozilla.org/en-US/docs/DOM/element.style) –