HTML 的 width/height 属性和 img 元素上的 CSS width/height 属性有什么区别?
HTML <img>
元素可以有 width/height 属性,也可以有 CSS width/高度属性:
The HTML <img>
element can have the width / height attribute, and it can also have the CSS width / height properties:
<img src="xxx.img" width="16" height="16"
style="width: 16px; height: 16px"></img>
HTML属性和CSS属性有什么区别,应该有同样的效果吗?
What's the difference between the HTML attribute and CSS property and should they have the same effect?
推荐答案
关于这个话题的热议可以在这里找到:图片标签与 CSS 的宽度属性
A hot debate about the subject can be found here: Width attribute for image tag versus CSS
总结一下:
声明宽度值和高度值(可能不是图像的原始物理尺寸)或css声明(如宽度:[valueX];高度:[valueY];)的好处是它有帮助加快页面的渲染速度.浏览器知道为页面的特定区域分配多少空间:它甚至会在第一次绘制、第一次解析+页面渲染时绘制图像占位符.当没有定义任何宽度和高度时,浏览器必须下载图像,然后计算其尺寸,分配空间,然后重新绘制页面.
The gain from declaring a width value and an height value (which may not be the original physical dimensions of the image) or from css declarations (like width: [valueX]; height: [valueY];) is that it helps speed up the rendering of the page. The browser knows how much space to allocate a particular area of the page: it will even draw image placeholders on a first draw, a first parsing+rendering of the page. When one does not define any width and height, then the browser has to download the image and then figure out its dimensions, space to allocate and then redraw the page.
在我看来,这似乎是最有益的效果.
This seems to be the most beneficial effect in my opinion.
相关文章