Go Back   SitePoint Forums > Forum Index > Design Your Site > Web Page Design > CSS
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 18, 2009, 14:55   #1
espresso
SitePoint Evangelist
 
Join Date: Oct 2008
Posts: 527
Do not use width and height on images?

I could have sworn you should no longer specify width and height in images, best practice right?
espresso is offline   Reply With Quote
Old Nov 18, 2009, 15:15   #2
EricWatson
SitePoint Wizard
 
EricWatson's Avatar
 
Join Date: Dec 2007
Location: Carlsbad, CA
Posts: 1,540
Doing so, just preserves the alotted space for the image before it appears. I've never used a height and width. But doing so is perfectly fine, and even suggested.
EricWatson is online now   Reply With Quote
Old Nov 18, 2009, 15:15   #3
ralph.m
Courage, Telemachos
bronze trophy
 
ralph.m's Avatar
 
Join Date: Mar 2009
Location: Melbourne, Australia
Posts: 1,420
This came up recently. It is probably still good practice, because when height and width are set, a space is reserved for the image even if it has not yet loaded. Without this, a user may be trying to read the text before images have loaded, but the text keeps jumping around violently as each image loads, which can be both annoying and even upsetting.
ralph.m is offline   Reply With Quote
Old Nov 18, 2009, 15:18   #4
Erik J
SitePoint Mentor
bronze trophy
 
Erik J's Avatar
 
Join Date: May 2007
Location: Countryside, Sweden
Posts: 3,085
Best practice would be to use the width and height attributes in the tag. Because then the browser knows what space the image will take before it is downloaded. (In IE you have also the advanced option to "Show image download placeholders" that iirc reads the size in the tag.)
Erik J is offline   Reply With Quote
Old Nov 18, 2009, 16:14   #5
RyanReese
CSS Consultant/Ninja-I'm fast
SitePoint Award Recipient
 
RyanReese's Avatar
 
Join Date: Oct 2008
Location: Whiteford, MD
Posts: 10,067
Hi, in tables in IE if you don't specify the height/width in the actual <img> via the attributes, then IE will jump the page around allocating space one image at a time, so yes you should include them .

It can be fiixed by table-layout:fixed; on the table however the easiest route is pututing them on there
RyanReese is offline   Reply With Quote
Old Nov 18, 2009, 22:25   #6
AutisticCuckoo
SitePoint Wizard
silver trophybronze trophy
 
Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 11,777
Specifying the image dimensions via CSS should be equally good for reserving space, with the arguable benefits that you don't have to specify two presentational attributes and that you may reduce markup bloat somewhat.

There may be issues with some ancient browsers, but I don't think they are worth worrying about. IIRC, old Netscape versions wouldn't load more than 4 images in parallel if the dimensions weren't hardcoded into the markup.

I almost never use the width and height attributes for images any more. I use CSS instead.
AutisticCuckoo is offline   Reply With Quote
Old Nov 18, 2009, 22:32   #7
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,805
You need to specify them in the HTML in order to reserve the right space for the image. This means the page doesn't have to be rearranged after the CSS and images load and it also handles the situation for browsers that do not understand CSS.

Unlike the textarea tag where the rows and cols are mandatory in the HTML the width and height are optional in the img tag as they can be worked out from the image once it has loaded.

Since the width and height are a part of the image it is semantically correct to include them in the HTML.
felgall is online now   Reply With Quote
Old Nov 18, 2009, 23:45   #8
AutisticCuckoo
SitePoint Wizard
silver trophybronze trophy
 
Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 11,777
Quote:
Originally Posted by felgall View Post
You need to specify them in the HTML in order to reserve the right space for the image.
Only on the first load (before the CSS has been cached).

The HTML4 specification hints about the intended use for those attributes:
Code:
<!ATTLIST IMG
...
  height      %Length;       #IMPLIED  -- override height --
  width       %Length;       #IMPLIED  -- override width --
...
  >
It seems as if those attributes were meant to be used for overriding (i.e., resizing) images, not for specifying their natural dimensions.
AutisticCuckoo is offline   Reply With Quote
Old Nov 19, 2009, 10:40   #9
AlexDawson
SitePoint Mentor
bronze trophy
SitePoint Award Recipient
 
AlexDawson's Avatar
 
Join Date: Feb 2009
Location: England, UK
Posts: 5,504
I tend to follow the convention of using width and height for images within the CSS as well, how I see it is if your going to follow standards, be consistent with your implementation. We don't debate or quibble with having those attributes elsewhere in the document however with images people seem to feel it's worth using them. My point of view is that width and height are stylistic elements, even when it comes to the IMG element (as it can manipulate the visual dimensions), ergo for the same reason as other elements it's better to just keep style and structure separately (even if the dimensions are fixed) for the sake of consistency.
AlexDawson is online now   Reply With Quote
Old Nov 19, 2009, 11:02   #10
Paul O'B
CSS Advisor
silver trophybronze trophy
SitePoint Award Recipient
 
Paul O'B's Avatar
 
Join Date: Jan 2003
Location: Hampshire UK
Posts: 27,430
Firefox2 and under will misplace content very badly if image height and width attributes are missing. It doesn't matter if the css has the dimensions specified and the page loads with the content and images misplaced.

Once images and css are cached this no longer happens but it was a very common problem with gecko and occasionally IE.

Modern browsers seem to behave better but we still see the odd hiccough in the forums that boils down to the attributes being missing so I always prefer to use them when possible.

It's just another choice you have to make for yourself
Paul O'B is online now   Reply With Quote
Old Nov 19, 2009, 11:06   #11
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,805
Placing the width and height in the HTML saves you from having to put them in the CSS.

Also placing the actual width and height of an image in the CSS seems inappropriate since the CSS is supposed to be defining the appearance you want the page to have and not attributes that the content has already got even before it loads.
felgall is online now   Reply With Quote
Old Nov 19, 2009, 11:08   #12
AlexDawson
SitePoint Mentor
bronze trophy
SitePoint Award Recipient
 
AlexDawson's Avatar
 
Join Date: Feb 2009
Location: England, UK
Posts: 5,504
Hmm that's interesting, do we have any statistics on usage numbers for Firefox 2? Most Firefox users tend to be passionate upgraders who don't linger on old version numbers for too long (the only usual hold-back is related to incompatible plug-ins), perhaps with some general numbers it would make the decision a bit easier.

PS: Felgall, arguably all HTML will have content which defines a set of dimensions (to hold the contents), and all of this will occur before the style is applied (manipulating such figures), what I would say is if you are going to claim that giving dimensions to an element which has predefined and existing dimensions set is unncessary then surely we should omit width and height entirely (for images) as those have already been preset by the content of that element.
AlexDawson is online now   Reply With Quote
Old Nov 19, 2009, 11:26   #13
cydewaze
SitePoint Evangelist
 
cydewaze's Avatar
 
Join Date: Jan 2006
Location: Maryland, USA
Posts: 595
What if you have your browser set to hide images, or if you have something like Adblock in FF that hides certain images? It seems like by not including the width and height attributes, the page structure could be rendered in an unexpected manner.
cydewaze is offline   Reply With Quote
Old Nov 19, 2009, 11:29   #14
RyanReese
CSS Consultant/Ninja-I'm fast
SitePoint Award Recipient
 
RyanReese's Avatar
 
Join Date: Oct 2008
Location: Whiteford, MD
Posts: 10,067
Quote:
Originally Posted by cydewaze View Post
What if you have your browser set to hide images, or if you have something like Adblock in FF that hides certain images? It seems like by not including the width and height attributes, the page structure could be rendered in an unexpected manner.
Adblock in FF generally don't hide images unless they are ads lol. So the OP should be fine in those reguards
RyanReese is offline   Reply With Quote
Old Nov 19, 2009, 12:47   #15
Paul O'B
CSS Advisor
silver trophybronze trophy
SitePoint Award Recipient
 
Paul O'B's Avatar
 
Join Date: Jan 2003
Location: Hampshire UK
Posts: 27,430
Arrow

Quote:
Originally Posted by AlexDawson View Post
Hmm that's interesting, do we have any statistics on usage numbers for Firefox 2? Most Firefox users tend to be passionate upgraders who don't linger on old version numbers for too long (the only usual hold-back is related to incompatible plug-ins), perhaps with some general numbers it would make the decision a bit easier.
I don't suppose there are very many FF2 users at all but it does apply to all gecko versions that are equivalent to FF2 (and others based on that).

It used to be such a common problem that there is still a FAQ about this problem in the sticky thread at the top of the forum (but that was in 2005 ).
Paul O'B is online now   Reply With Quote
Old Nov 19, 2009, 13:14   #16
AlexDawson
SitePoint Mentor
bronze trophy
SitePoint Award Recipient
 
AlexDawson's Avatar
 
Join Date: Feb 2009
Location: England, UK
Posts: 5,504
I might re-evaluate my reasoning if there was enough Gecko2 users to justify the point, but as it currently stands I tend to just keep all stylistic elements separately as I tend to get a bit neurotic if I see code which "looks" out of place, and for me width and height attributes in the HTML is one of those things which tends to make me grit my teeth, though out of consideration for the reasons above, I don't tend to pick people up on it if they choose to use it (as it's technically valid).
AlexDawson is online now   Reply With Quote
Old Nov 19, 2009, 13:42   #17
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,805
Quote:
Originally Posted by AlexDawson View Post
PS: Felgall, arguably all HTML will have content which defines a set of dimensions (to hold the contents), and all of this will occur before the style is applied (manipulating such figures), what I would say is if you are going to claim that giving dimensions to an element which has predefined and existing dimensions set is unncessary then surely we should omit width and height entirely (for images) as those have already been preset by the content of that element.
that isn't true.

display:block elenemts have their default width and height calculated based on both the content and the browser viewport size.

display:inline elements do not have a width or height definable at all as they take up however much space they require.

only replaced elements such as images have a width and height that are a part of the actual content itself. If the width and height were not a part of the image itself then you would always have to specify it somewhere whereas you don't necessarily need to specify the width and height for images anywhere. If you do decide to specify the width and height then in te HTML is a more appropriate place than in the CSS since the width and height of the image are a part of the image itself and hence a part of the content.
felgall is online now   Reply With Quote
Old Nov 19, 2009, 14:48   #18
AlexDawson
SitePoint Mentor
bronze trophy
SitePoint Award Recipient
 
AlexDawson's Avatar
 
Join Date: Feb 2009
Location: England, UK
Posts: 5,504
While block and inline elements are calculated by the content (and other relevant factors) their still calculated and determined unless otherwise overridden by those said items (which is what I was trying to put across). While replaced elements like images have their width and height automatically assigned by the image once it has been loaded (unless otherwise explicitly defined) the fact of the matter remains that they are still generated with or without the width and height being assigned, your arguing semantics when the issue of width and height on images is the same as determining cells for a table and whether the browser should calculate before or after to placehold the content. Sorry to say it but I don't really see your point, whether automatically or through code, the factors are the same from my perspective.
AlexDawson is online now   Reply With Quote
Old Nov 19, 2009, 16:13   #19
ralph.m
Courage, Telemachos
bronze trophy
 
ralph.m's Avatar
 
Join Date: Mar 2009
Location: Melbourne, Australia
Posts: 1,420
I may be missing something, but it seems that adding width and/or height to the CSS would potentially mean specifying a class or id for each image, or at least writing awkward rules to target specific images. Sounds more trouble than it's worth to me.
ralph.m is offline   Reply With Quote
Old Nov 19, 2009, 18:32   #20
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,805
Quote:
Originally Posted by AlexDawson View Post
I don't really see your point, whether automatically or through code, the factors are the same from my perspective.
I agree. Whether automatic OR through the code the factors are the same but images require NEITHER of those. The width and height are a part of the image itself. Open the image file in any program and the image has the same width and height. The width and height of an image are independent of the computer.

With any other element in your web page other than replaced elements there is no inherent width and height. In all those cases the width and height are calculated automatically or provided through the code. Open those elements in a different program and the width and height can be totally different as the width and height are not a part of the actual element but are dependent on factors external to the actual element.

If you ask "how wide is xyz.jpg?" the question can be answered without reference to the web page. If you ask "how wide is the ABC data table?" the answer is unknown as it will vary depending on a whole lot of criteria not included in the question and not an inherent part of the table itself. You can'e even ask how wide a single letter is since letters of the alphabet do not have a specific width and height inherent in their mere existance the way that images do have.

If you don't specify a width and height for your images anywhere in the HTML or CSS or anywhere else the browser still does not need to calculate the size of the image because the image itself contains that information. The width and height of an image are a part of the image itself completely separate from the web page.
felgall is online now   Reply With Quote
Old Nov 19, 2009, 18:58   #21
AlexDawson
SitePoint Mentor
bronze trophy
SitePoint Award Recipient
 
AlexDawson's Avatar
 
Join Date: Feb 2009
Location: England, UK
Posts: 5,504
Agreed, though my point was that as the browser CAN calculate the width and height of the image successfully then surely it's not a necessity, granted it means that your content will bounce about as the images load however as it can be omitted I see no issue with using CSS to manipulate the visual width and height (I see the width and height applied in the image as more like the browser default style rather than as part of the content) as without CSS the height and width can be naturally given to the image from the information it receives, just like an H1 will default to a particular style (just playing devils advocate).
AlexDawson is online now   Reply With Quote
Old Nov 19, 2009, 20:39   #22
cydewaze
SitePoint Evangelist
 
cydewaze's Avatar
 
Join Date: Jan 2006
Location: Maryland, USA
Posts: 595
Quote:
Originally Posted by RyanReese View Post
Adblock in FF generally don't hide images unless they are ads lol. So the OP should be fine in those reguards
So? What if the ads are built into the page structure and they get hidden? Same result. Not that I'm going to be coding any ads into any of my pages anytime soon, but someone has to do that coding. I see lots of ads on news sites that are built into the flow of the page.
cydewaze is offline   Reply With Quote
Old Nov 20, 2009, 02:40   #23
RyanReese
CSS Consultant/Ninja-I'm fast
SitePoint Award Recipient
 
RyanReese's Avatar
 
Join Date: Oct 2008
Location: Whiteford, MD
Posts: 10,067
I don't think adblock would remove it (like display:none so the other elements would have to flow around it. I think the makers would realize that would ccause page flow issues. (I don't use adblock on my FF so this is just presumptions)
RyanReese is offline   Reply With Quote
Old Nov 20, 2009, 12:14   #24
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,805
One thing to consider that hasn't been mentioned yet is what will happen if for some reason one of the images is unable to load. If you specify the width and height in the img tag then the page will retain the intended layout and the alt text will be displayed in the area that would normally be allocated to the image. If you don't specify the width and height then the page will be laid out differently to when the image is there with the alt texy inline with the surrounding content.

Whether or not to include the width and height in the HTML probably ought to be determined by which of those two outcomes you want for when an image fails to load.
felgall is online now   Reply With Quote
Old Nov 21, 2009, 06:46   #25
Paul O'B
CSS Advisor
silver trophybronze trophy
SitePoint Award Recipient
 
Paul O'B's Avatar
 
Join Date: Jan 2003
Location: Hampshire UK
Posts: 27,430
Quote:
Originally Posted by felgall View Post
One thing to consider that hasn't been mentioned yet is what will happen if for some reason one of the images is unable to load. If you specify the width and height in the img tag then the page will retain the intended layout and the alt text will be displayed in the area that would normally be allocated to the image. If you don't specify the width and height then the page will be laid out differently to when the image is there with the alt texy inline with the surrounding content.

Whether or not to include the width and height in the HTML probably ought to be determined by which of those two outcomes you want for when an image fails to load.
That's a good point and I often see pages with the image src missing and the alt attribute text is running all over the content because the width and height have attributes been omitted from the image.
Paul O'B is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 11:22.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved