how to specify image size in pixels
Quick question about specifying the size of images in pixels:
<img src="/logo.jpg" alt="logo" width="200px" height="120px" />
or
<img src="/logo.jpg" alt="logo" width="200" height="120" />
I've been always开发者_开发问答 putting in the px but recently noticed that very few people do this. Am I better off leaving that out? Does it matter one way or the other?
As far as I know, you do not add the px
label to width/height attribute values on the img
tag itself, and I believe (though could be wrong) it's because pixels are used regardless. In CSS, however, one may use different units to specify width and height, therefore it is appropriate to add the label when using CSS.
I usually leave it out. As not often is the case I use <img>
elements. But, it is better practice to place the width and height in the <img>
tag so the browser doesn't have to calculate these dimensions.
Could not find any information that verifies this but I would use integers without a unit (if not percentage) in HTML and use "px" in CSS, except for zero values where it´s more correct to leave out the unit.
http://www.w3.org/TR/CSS2/visudet.html#the-width-property
Use css to consider the the size of the image:
CSS:
.logo {
width: 200px;
height: 120px;
}
HTML:
<img src="/logo.jpg" alt="logo" class="logo" />
I would continue to specify the size in pixels. You probably don't need to, but I think you're supposed to according to web standards. Specifying the size in pixels is definitely not going to hurt your code, so leave it there.
精彩评论