Should image size be defined in the img tag height/width attributes or in CSS?

img标签的 widthheight属性中定义图像大小是否是更好的编码实践?

<img src="images/academia_vs_business.png" width="740" height="382" alt="" />

或者在 CSS 样式的宽度/高度?

<img src="images/academia_vs_business.png" style="width:740px; height:382px;" alt="" />

还是两者都有?

<img src="images/academia_vs_business.png" width="740" height="382" style="width:740px; height:382px" alt="" />
167231 次浏览

肯定不是两者兼有。除此之外,我不得不说这是个人偏好。如果我有许多同样大小的图片,我会使用 css 来减少代码。

.my_images img {width: 20px; height:20px}

从长远来看,CSS 可能会胜出,因为 HTML 属性不再适用,而且更有可能是因为 SVG 等矢量图像格式的增长,在这些格式中,使用% 或 em 等非像素单位来缩放图像实际上是有意义的。

<img id="uxcMyImageId" src"myImage" width="100" height="100" />

specifying width and height in the image tag is a good practice..this way when the page loads there is space allocated for the image and the layout does not suffer any jerks even if the image takes a long time to load.

虽然可以使用内联样式,但是在页面上包含一个外部 CSS 文件可能会更好地满足您的目的。这样你就可以定义一个图片类(比如“缩略图”、“照片”、“大图”等等)并给它分配一个固定的大小。这将有助于当您最终的图像需要相同的位置跨多个页面。

像这样:

In your header:
<link type="text/css" rel="stylesheet" href="css/style.css" />


Your HTML:
<img class="thumbnail" src="images/academia_vs_business.png" alt="" />


In css/style.css:
img.thumbnail {
width: 75px;
height: 75px;
}

但是,如果您想使用内联样式,最好使用 style 属性来设置宽度和高度,以保证可读性。

在标签中定义高度/宽度的历史原因是,这样浏览器甚至可以在加载 CSS 和/或图像资源之前调整页面中的实际 <img>元素的大小。如果不显式提供高度和宽度,则 <img>元素将呈现为0x0,直到浏览器可以根据文件调整其大小。当这种情况发生时,一旦图像加载,就会导致页面的可视化重流,如果页面上有多个图像,则会复合。通过高度/宽度调整 <img>的大小可以在页面流中创建正确大小的物理占位符,使您的内容可以异步加载,而不会影响用户体验。

另外,如果您正在进行移动响应设计(这是目前的最佳实践) ,则通常指定宽度(或最大宽度) 只有并将高度定义为 auto。这样,当你为不同的屏幕宽度定义媒体查询(例如 CSS)时,你可以简单地调整图片宽度,让浏览器来处理保持图片高度/长宽比正确的问题。这是一种中间立场的方法,因为你可能会得到一些回流,但它允许你支持广泛的屏幕尺寸,所以利大于弊。

最后,有时候您可能不知道图像的大小(图像 src可能是动态加载的,或者可以通过脚本在页面的生命周期中进行更改) ,在这种情况下,使用 CSS 只是有意义的。

底线是,你需要理解这些权衡,并决定哪种策略对你想要实现的目标最有意义。

选项 A。 简单直接的 fwd。你看到的是什么,你得到容易进行计算。

Option b. Too messy to do this inline unless you want to have a site that can stretch. IE if you used the with:86em however modern browsers seem to handle this functionally adequately for my purposes.. . Personally the only time that i would use something like this is if i were to create a thumbnails catalogue.

/*css*/
ul.myThumbs{}
ul.myThumbs li {float:left; width:50px;}
ul.myThumbs li img{width:50px; height:50px;border:0;}


<!--html-->
<ul><li>
<img src="~/img/products/thumbs/productid.jpg" alt="" />
</li></ul>

选项 c 太混乱而无法维护。

我将在这里反其道而行之,指出将内容与布局分离的原则(这将证明建议使用 CSS 的答案是正确的)并不总是适用于图像的高度和宽度。

每个图像都有一个固有的、原始的高度和宽度,这些高度和宽度可以从图像数据中推导出来。在内容与布局的框架中,我认为这个派生的高度和宽度信息是内容,而不是布局,因此应该作为元素属性呈现为 HTML。

这很像 alt文本,也可以说是从图像派生。这也支持任意用户代理(例如语音浏览器)应该拥有这些信息以便将其与用户联系起来的想法。至少,长宽比可以证明是有用的(“图像的宽度为15,高度为200”)。这样的用户代理不一定处理任何 CSS。

The spec says that the width and height attributes can also be used to override the height and width conveyed in the actual image file. I am not suggesting they be used for this. To override height and width, I believe CSS (inline, embedded or external) is the best approach.

So depending on what you want to do, you would specify one and/or the other. I think ideally, the original height and width would always be specified as HTML element attributes, while styling information should optionally be conveyed in CSS.

我使用 contentEditable来允许在我的应用程序富文本编辑。我不知道它是如何滑过去的,但是当一个图像被插入,然后调整大小(通过在它的一边拖动锚) ,它会生成这样的东西:

  <img style="width:55px;height:55px" width="100" height="100" src="pic.gif" border=0/>

(随后的测试显示插入的图像不包含这种“流氓”样式 attr + param)。

当通过浏览器(IE7)呈现时,样式中的宽度和高度覆盖了 img width/height 参数(这样图像就像我想要的那样显示。.缩小为55像素 x 55像素。看起来一切都很顺利。

当我通过设置 mime 类型 application/mword 或将浏览器渲染粘贴到 mword 文档来将页面输出到 ms-word 文档时,所有图像都恢复到默认大小。我最终发现 mword 放弃了样式,使用了 img width 和 height 标记(它具有原始图像大小的值)。

我花了点时间才发现。无论如何... 我已经编写了一个 javascript 函数来遍历所有的标签,并将 img style.width 和 style.height 值“传输”到 img.width 和 img.height 中,然后在将这段 html/richtext 数据保存到数据库之前清除 style 中的这两个值。

干杯。

我的回答是... 不。把这两个属性直接放在 img 下面,而不是 style。