-1

I have browsed all the how-tos about CSS sprites and although some of them are helpful, absolutely none explain how exactly to use the CSS class to display an image in a HTML page.

This is my CSS code, it has two classes.

.sprites{background-image:url('bookie_logos/0csssprites.png');background-color:transparent;background-repeat:no-repeat;}
#3_png{height:16px;width:75px;background-position:0 0;}
#5_png{height:16px;width:75px;background-position:-75px 0;}
...and so on

Which code do I use to display the image I want, not as a link, not as a list, not as a background, just a stand-alone image with width 75 and height 16? I suppose I should be using a DIV and assign two classes for it like this: class="sprites 3_png" but it doesn't work.

UPDATE: There were several problems with the code. The first one was that 3_png should be used as an id, not as a class. Second, the code only worked when naming convention png_N was used instead of N_png, it won't work when it starts with a number. After finding a working solution, I encountered another problem with the images creating a line break, solved it using display:inline. This is the CSS and PHP code which produces the wanted output:

CSS:

.sprites {background-image:url('bookie_logos/0csssprites.png');background-color:transparent;background-repeat:no-repeat;display:inline;}
#png3  {height:16px;width:75px;background-position:0px 0;}
#png5  {height:16px;width:75px;background-position:-75px 0;}

PHP:

$classy = "png" . $db_field['bookieid'];
echo "<td>" , "<img src='transparent.gif' class='sprites' id='$classy' alt='bookie' align='absmiddle'/>" , "</td>";

$classy is a variable which is choosing between 160+ images in the spritesheet. transparent.gif is a 1px transparent gif, as I opted to use IMG tag instead of DIV, and I need to use that 1px gif in order for the IMG tag to actually display the sprite image.

I'd like to thank all the contributors, most of you saw that first part of the problem, where ID should be used instead of class. You all guided me towards the solution, and unfortunately I can only choose one answer, and I'm choosing the one which mentioned display:block as it made me use display:inline later.

Thanks everyone, and I hope this question and answer will help someone else who's having the same problem with CSS sprites.

Dan Horvat
  • 800
  • 3
  • 14
  • 27
  • 6
    If you are trying to show an image as an image then you should probably be using an `` tag, not sprites – My Head Hurts Feb 21 '12 at 14:34
  • 2
    `not as a link, not as a list, not as a background`. CSS sprites are backgrounds.. – Curtis Feb 21 '12 at 14:47
  • 1
    @DanHorvat Dan, can you add your HTML? Your original question does have some contradiction in it, as Curt points out. Thanks! – jmbertucci Feb 21 '12 at 16:24
  • Update: It works now, but only if using class naming convention png3 instead of 3png. Thanks everyone! The new problem is that after the image is displayed, text (which is supposed to go to the right of the image) is displayed beneath the image. You can see it here (dozens of small logos in the middle column are our CSS sprites): http://www.oddsnavigator.eu/match/Soccer/England%203%20League%201/Scunthorpe-Walsall/1x2-ord-juice_a-1025943-0-1-1 – Dan Horvat Feb 21 '12 at 16:37
  • display:inline solved that problem. – Dan Horvat Feb 21 '12 at 16:49

6 Answers6

3

Your error is using 3_png as a class, it's an ID, also css grammar spec says that ids and classes have to start with an - , _ or letter a-z, so 3_png is invalid, but png_3 isn't.

to get it to work you should use:

 <div id="png_3" class="sprites"></div>

and change your css to reflect the name change.

JKirchartz
  • 17,612
  • 7
  • 60
  • 88
2

If you are applying 3_png as a class it won't work.

You will have to set the div's id as 3_png or change the css to make 3_png a class

Option one:

html:

<div id="3_png" class="sprites"></div>

css stays the same.

Option two:

html:

<div class="3_png sprites" > </div>

css:

.sprites{background-image:url('bookie_logos/0csssprites.png');background-color:transparent;background-repeat:no-repeat;}
.3_png{height:16px;width:75px;background-position:0 0;}
Caleb Doucet
  • 1,751
  • 2
  • 14
  • 29
2

If you want to use 1 sprite for displaying multiple images, I think you should try this:

CSS

.sprite {
  background-image:url('bookie_logos/0csssprites.png');
  background-color:transparent;
  background-repeat:no-repeat;
  display:block;
}

#img_1 {
  height:16px;width:75px;background-position:0px 0;
}

#img_2 {
  height:16px;width:75px;background-position:-75px 0;
}

HTML

<div class="sprite" id="img_1">
</div>

<div class="sprite" id="img_2">
</div>
skywlkr
  • 1,185
  • 11
  • 22
  • Your solution is showing some signs of life. While testing it, I've noticed that ID can't be starting with a number. If I use ID 3png it doesn't work, and if I use png3 it works. I need to test it further, just wanted to say that this was the error in my code. – Dan Horvat Feb 21 '12 at 16:12
  • 1
    I chose your answer because it is the only one which tackled all three different problems I have encountered. Thank you! – Dan Horvat Feb 21 '12 at 17:07
1

You don't. That's what the <img> tag is for.

MetalFrog
  • 9,943
  • 1
  • 22
  • 24
  • 1
    I'm sure he knows what the `` tag is for, pretty sure he's asking how to use spritesheets and display 1 sprite from them. – anothershrubery Feb 21 '12 at 14:34
  • 2
    Why are people upvoting this?! You cannot use an `` tag for this solution! – anothershrubery Feb 21 '12 at 14:39
  • 1
    @anothershrubery From the question "Which code do I use to display the image I want, not as a link, not as a list, not as a background, just a stand-alone image with width 75 and height 16?" So if we're trying to display an image, without using CSS to manipulate the DOM, how else do you suggest we display the image? – MetalFrog Feb 21 '12 at 14:59
  • 1
    He has confused his words, but it's quite obvious he has a larger spritesheet which he wants to display one sprite from. This is not possible with an ``. The OP should be picked up on failing to describe correctly his problem, not being given false advice which people are inexplicably upvoting! – anothershrubery Feb 21 '12 at 15:50
  • 1
    @anothershrubery Possibly - but a background image posing as an in-page element sounds like a bad idea and semantically incorrect. If your assumptions about what the OP is asking are correct then this should still be done with an `` tag – My Head Hurts Feb 21 '12 at 16:23
  • @anothershrubery False advice? He's trying to do something that doesn't make sense. You're correct that it's not possible with a CSS sprite, nor _should_ it be. Save the _image_ as an _image_, put into the page as an _image_. – MetalFrog Feb 21 '12 at 16:38
  • I have 160+ individual images in that spritesheet and I need to display each one individually in a table td. All the code I was able to find online was either displaying sprites as a link, or as a list, or as a background. What I wanted to say was - I want the single image taken from the spritesheet to behave like an image. With text being around it, not on top of it (as would be the case with a background). – Dan Horvat Feb 21 '12 at 16:43
  • By the way, tag doesn't work,
    does. If I would be using tag, I would have to add src=transparent.gif (1 px) for the sprite image to appear. With
    I don't have to do that.
    – Dan Horvat Feb 21 '12 at 16:43
  • @MetalFrog I agree that the image should be saved as a new image, but technically it is possible to keep it as a sprite if you really really wanted: http://jsfiddle.net/5Sfhr/ – My Head Hurts Feb 21 '12 at 16:45
  • @DanHorvat I'm so confused by this set up. When I think CSS sprites, I think of ui elements like gradients and icons that don't matter to the actual functionality of the page. Spitting out images like this doesn't seem like a proper application of sprites to me. – MetalFrog Feb 21 '12 at 17:16
  • @DanHorvat Besides, even if you're trying to condense loading times on images, don't you reach a point where having 160 sprites in a file winds up being more of a burden than just having the individual images pulled in when you need them? – MetalFrog Feb 21 '12 at 17:19
  • @MetalFrog Perhaps it is not proper application, but it works. The way I see it, CSS sprites are used to minimize HTTP requests when you have a lot of images on the website. It doesn't say whether those images should be used for the styling or for something else. **The sprite image (the big one) is cached, so it opens very quickly.** If I'm using individual images without a sprite, you can actually see each one of them getting loaded on the website. Very slow. The only other solution I've found was using jar compression, but that concept only works in Firefox. – Dan Horvat Feb 21 '12 at 17:30
  • @DanHorvat A good rule of thumb is that sprites are used for styling (that is why they are used as background-images in the CSS) and `` is used for content (which is why it is part of the HTML). So if it is part of the content and the user needs to see it, use an `` tag. If it is something that is there to make the page look good / increase usability then you can use it as a background image (as part of a sprite). If there are large loading times, maybe you need to look at how big your images are and how they are compressed. – My Head Hurts Feb 21 '12 at 22:25
  • @MyHeadHurts If you know of a better way to minimize HTTP requests and increase the page loading speed when you have 100+ png images, I'd love to hear it. – Dan Horvat Feb 22 '12 at 00:01
  • @DanHorvat I have had a look at your bookies sprite, and you have gone the right way of doing it. The question wasn't too clear and in my opinion these logos aren't content. I understand that you have a working solution now, but if you would like to know more about another way to achieve what you did and why I believe the logos aren't content then feel free to open up a chat discussion. – My Head Hurts Feb 22 '12 at 10:29
1

Your 3_png is an id not a class .. Either make it class or use id

Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59
1

I would use CSS more like this to make it a little more straight forward:

.icon_box{
    width:75px; 
    height:16px; 
    /* default image */
    background: img('/my/image/file.png') 0 0 no-repeat;
}

.icon1{
    background-position: -75px 0;
}
.icon2{
    background-position: -150px 0;
}

Your HTML

<div class="icon_box icon1"></div>
<div class="icon_box icon2"></div>

This assumes your image sprite is 16px high and has images every 75px from left to right, like this poor ASCII art. =P

[ Img1   Img2   Img3   ... ]

You could use a element ID but if you want to have 2 icons on the same page, you'd run into issues. So, I would just make them all classes.

The idea is the default value sets up your basic "canvas" and then you simply reposition the background image you need.

UPDATE 2

Here's a JSFiddle that uses a similar code to what I wrote above: http://jsfiddle.net/VNn2D/4/

It uses this sprite image: https://i.stack.imgur.com/fuf79.gif

It comes form this article on the topic: http://www.alistapart.com/articles/sprites

Hope that helps.

Cheers!

jmbertucci
  • 8,194
  • 4
  • 50
  • 46
  • Is your image loading correctly? Does `background:url('bookie_logos/0csssprites.png') top left no-repeat;` show your image at all, before you even try to position the sprite? – jmbertucci Feb 21 '12 at 16:09
  • @DanHorvat See update 2. It's a JSFiddle of my example. If your project isn't working, there's something else going on with your HTML, CSS or possibly JavaScript. I would recommend posting more of your code so we can try and help you find the issue. – jmbertucci Feb 21 '12 at 16:22
  • The images started displaying only when I started using a class name png3 instead of 3png, it doesn't work when it starts with a number. – Dan Horvat Feb 21 '12 at 16:30
  • 1
    `In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".` [source](http://www.w3.org/TR/CSS21/syndata.html#characters) – My Head Hurts Feb 21 '12 at 16:37