Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1000
  • Last Modified:

Using CSS to set image source

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>
0
witgrefe
Asked:
witgrefe
  • 3
  • 2
1 Solution
 
bolt81Commented:
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.
0
 
witgrefeAuthor Commented:
would this be correct?:
.currentImage {
background-image: url (location of the image);
background-repeat: no-repeat;
height: 20px;
width: 20px;
}
0
 
bolt81Commented:
if the image size is 20px then yes
0
 
witgrefeAuthor Commented:
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?
0
 
bolt81Commented:
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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now