tags:

views:

111

answers:

6

I've been learning (X)HTML & CSS recently, and one of the main principles is that HTML is for structure and CSS for presentation.

With that in mind, it seems to me that a fair number of images on most sites are just for presentation and as such should be in the CSS (with a div or span to hold them in the HTML) - for example logos, header images, backgrounds.

However, while the examples in my book put some images in CSS, they are still often in the HTML. (I'm just talking about 'presentational' images, not 'structural' ones which are a key part of the content, for example photos in a photo site).

Should all such images be in CSS? Or are there technical or logical reasons to keep them in the HTML?

Thanks, Grant

+2  A: 

One reason to put those images in CSS might be to serve different browsers from the same web site, just by changing the CSS: for example, if you detect a mobile/embedded/pocket browser you could give them the same HTML but with a CSS that doesn't include images.

ChrisW
You can `display: none;` on an image using CSS, though.
strager
A: 

The src property of an img tag is required according to HTML 4.01/XHTML 1.0 DTD. That is why it should always be included in the HTML.

You can specify it in the CSS for skining purposes, but most images in most cases are static and non changing so putting it in CSS is an unecessary step.

Tom Gullen
But what about leaving the img tag out altogether, and just having (for example) a span, which got filled with an image in CSS? As certain images are just for decoration, this is what seems to be suggested by the HTML/CSS split. Also this way, as ChrisW suggested, you can have a different image or no image depending on the type of viewer.
Grant Crofton
You can, but it's just re-inventing the image tag to be honest. You also (I think) can't use alternative text which can be important for accessability.
Tom Gullen
+1  A: 

I put them to CSS if possible. One reason is that I think they belong there like you mentioned and the other one is the possibility to use sprites. This can reduce the loading time of your page significantly.

aeby
+9  A: 

If an image is "content" say in a newspaper article, the editorial image, then use img tag. If it is part of your UI, theme or skin or whatever the name is, then yes put it CSS.

Suggested readings

  • Designing with Web Standards (Zeldman)
  • Bullet Proof Web Design (Dan Cederholm)
  • CSS Mastery (Andy Clark, Andy Budd, Cameron Moll)
redben
I assume that by "UI" you only mean the decorative part of the UI. If there are for example images used as links, those should be in HTML. Only images that are purely decorative and insignificant to contents should be put in CSS.
PauliL
I guess the real issue then becomes deciding what's content. For example I'm making a gift list page for a wedding which includes some pictures of items - not a core part of the content, but also arguably more than just decoration. Anyway this pretty much confirms what I originally though, and seems to be the popular suggestion, so you get the answer!
Grant Crofton
A: 

Well, it depends. For example, if you want to do some effects when the mouse is over an image, it must be in the HTML. When you put the image in the HTML you can positionate it more freely than in CSS. Also, as far as I know, CSS included images are not crawled (You can have interest in have your company's logo crawled by searchers).

If you think about accesibility, the HTML embedded images can have an alt and title information. So, for example, when you put the mouse over the logo of your company, the browser could show the motto of your company if you embed it with title="motto" attribute in the img tag. You can't do that with CSS.

Also people are used to put images in the HTML not the CSS and behaviours are a hard thing to change.

In conclussion, depending of your needs, CSS isn't flexible enough to fit your needs and you should put the images in the HTML. But if CSS fits your needs for UI images, then CSS is better idea.

Matachana
you can do roll overs with css. Actually you have more possibilities with css. with CSS sprites for example, you have one image containing the normal and the hover states, stacked. In css a:hover, you just change the position of the background image et voila.> CSS isn't flexible enough...questionable.There are plenty of good books on this, "CSS Mastery" for example
redben
Yep, we agree in the rollovers. If you want to do the rollover just on the image, you should use img:hover and the img tag must be present on the (X)HTML.If you want to put a title attribute on image, there's no way to do that on CSS, so for that task CSS isn't flexible enough. CSS is very flexible in general, but for some specific tasks isn't enough flexible.
Matachana
No what i mean with rollover is things like menu roll overs, don't use img tag. ex. <a class="item-1" href="#">item 1</a> then in css you'd have something like #item { background: url(...) top left;} a.item:hover {background-position:bottm left;}Now putting a title on an image means that the image is content, then it should be loaded form html with the content, not from css. This has nothing to do with css being unflexible for some specific tasks. At least this is what i think :)
redben
Yeah, I can see where you're coming from. I don't know all the options of either method, but like redben says it seems as though the things which are only available in HTML (e.g. alt tag, title) are things you'd only want to put on content anyway, and not images which were purely decorational.
Grant Crofton
A: 

Sometimes, loading UI images using CSS, also prevents the users from downloading your UI images to their drives, while saving a page.

But of course there are other ways to save them, but just a point to add.

And browsers tend to prioritize CSS more than HTML, so loading images through CSS might be a little faster compared to HTML.

Starx