June 16, 2010

Should all presentational images be defined in CSS?

Question by Grant Crofton

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

Answer by redben

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)

Answer by Starx

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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!