Using CSS to set image source

Posted on 2008-06-11
Last Modified: 2012-06-27
Instead of hardcoding the image into an img tag, I would like to have the image defined in a stylesheet and pull it from there.  How do I go about it?

<td id= 'a' ><img id='b' src ="images/image.jpg;"></td>
Question by:witgrefe
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 21757997
create css class with the following properity
background-image: url (location of the image);

use background-repeat: no-repeat; to just show it once.
you will need to set a width and hieght for the class to show the image.

Author Comment

ID: 21758072
would this be correct?:
.currentImage {
background-image: url (location of the image);
background-repeat: no-repeat;
height: 20px;
width: 20px;

Expert Comment

ID: 21758089
if the image size is 20px then yes

Author Comment

ID: 21758128
If I don't know the image size can I just leave out the width and height attributes to have the image rendered at its actual dimensions?

Accepted Solution

bolt81 earned 50 total points
ID: 22272011
sorry this isn't the case when using a backgroup image. you need to set a size and it will only show what part of the image fits into that size

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question