CSS Sprites into HTML code

janesta

New Member
Hello all, I was hoping someone would be kind enough to give me some advice.

I am trying to learn how to create websites and am picking bits and bobs up along the way. I have so far been creating my design in Photoshop, then slicing and exporting the HTML and images. I know that this only gives me a basic HTML, and I appreciate that it is stopping me from learning the code, but I have found that it has given me a good place to start as watching tutorials of folk just typing code into a blank page was very daunting. I spent ages reading posts and struggled to make sense of it, but starting with something on the page has meant that I can change, add and start to learn, but it does mean I get stuck at the basics. As I am learning bits and watching/reading more tutorials I am beginning to write my own code, but its early days.

I have been trying to learn how to use CSS sprites, and although I understand the concept and how to make the actual sprite, I am struggling to get this to work within the HTML code. I have read numerous tutorials, but they all focus on the CSS and not how to implement it into the HTML. I realise that this is very basic, but as I said I am new and just learning so please be gentle - I am trying my best.

I have created a simple 600px x 600px image and used a sprite generator (just for quickness, I do understand how its made), and this generator also spits out the lines for the HTML. But its not working - none of the images load, and I don't know what I am doing wrong. I have pasted in both the CSS and HTML below and would really appreciate it if someone could point me in the right direction. I am sure to you experienced guys it will be very obvious.....

CSS

}

.sprites {
background-image : (images/world.png);
background-color : transparent;
background-repeat : no-repeat;
}

#img1_jpg {
height : 300px;
width : 300px;
background-position : -0px -0px;
}

#img2_jpg {
height : 300px;
width : 300px;
background-position : -300px -0px;
}

#img3_jpg {
height : 300px;
width : 300px;
background-position : -600px -0px;
}

#img4_jpg {
height : 300px;
width : 300px;
background-position : -900px -0px;
}


HTML

<html>
<head>
<title>Untitled-10</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="images/style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table id="Table_01" width="600" height="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<div class="sprites" id="img1_jpg"></div> </td>
<td>
<div class="sprites" id="img2_jpg"></div></td>
</tr>
<tr>
<td>
<div class="sprites" id="img3_jpg"></div></td>
<td>
<div class="sprites" id="img4_jpg"></div></td>
</tr>
</table>
</body>
</html>
 

CaldwellYSR

Member
in your .sprites class in the first block of CSS you should have...

Code:
.sprites {
    background-image: [B]url[/B]("http://www.webdesignforum.com/images/world.png");
}

Now I know you didn't want this lecture but I feel obliged to say this. Slicing PSD's and exporting them is a really bad way to make websites. Besides using all flash it's probably the absolute worst way to do it. I know you're learning and I appreciate how much easier it might make it to learn but what you're learning are really bad habits. Tables haven't been used for layouts since the late 90's because they're not used for layout. You should be working with html5 elements and learning them. This will keep you in better habits.
 

janesta

New Member
Hi Mathew,

Thank you for the answer and the lecture - although I have seen much harsher criticism on forum!! ;o) So cheers for keeping it tame.

I know its not the right way to be doing it and I am trying to learn - its just I find code to be a bit of a minefield to be honest. Not because the code is difficult, but because every tutorial I watch or read seems to do things differently, and you can never find one that does everything you need so you end up all over the place trying to paste together bits here and there.

It really just started after struggling to make even a simple layout in Dreamweaver and then I watched a video about slicing and within 2 minutes I had a website! But I do want to learn it properly and am trying.

This is a serious question, but can you tell me other than not actually learning how to write the code, why is it so bad?
 

CaldwellYSR

Member
Because you're making a website that's based off images and images are inherently inflexible. Across different browsers, different operating systems, and different browser sizes the website will not be consistant. There are good tutorials online, this one is my favorite. It is long so you'll have to bear with it a bit but it's thorough. If you have aim you could add me and I'd be willing to help you out anytime you needed it.
 

Edge

Member
I think people learn in different ways and some do it by diving in, making a bit of a mess, and then sorting it out later. It's probably a personality thing. I'd advocate multiple methods of learning so do a bit of what you are doing in exporting from Photoshop, if anything you'll eventually learn its limitations and also have a little go at hand coding.

One thing ultimately to bear in mind is that HTML consists of a bunch of different types of elements and each have a different purpose. E.g. a heading element (<h1></h1>) indicates the main heading for content on a page. Now you can make something look big using all kinds of non standard techniques but the only thing that will tell something like Google that your heading is a heading is a heading element! This of course has implications for SEO. That's just one example. When you splice and dice from Photoshop it doesn't get things like that right. It also chucks in a ton of code that just doesn't need to be there. Later on down the line, you want to make a simple change and you're working with a pile of crap code that's a total pain in the butt to modify.
 

janesta

New Member
Thank you guys. That makes a lot of sense. To be honest I am already finding it very limiting and changing stuff is a complete pain. I am glad I found out about slices though as it gave me a chance to learn about style sheets and things without having to learn everything all in one go. But I will go through that tutorial you suggested Matthew and I have ordered HTML for dummies, so hopefully it won't be long before I am coding all by myself!!

Matthew, I used to have aim but not anymore, but I would download it again if you honestly would not mind me picking your brain from time to time. I promise not to harass you with thousands of questions...... maximum 50 a day! ;)

While I am here, can I ask another two little questions -

1. I seem to be reading conflicting views about sprites. Some people say only to use them for buttons and logos while others say to use ALL images including a background image. What are your thoughts?

2. I have made a couple of websites using some HTML5, a banner at the top, etc. I did start out learning Flash and learned to do basic stuff, but I don't like flash for obvious reasons and wanted to create any flash like elements in HTML5 instead. I watched a presentation from Adobe about their next version of Flash which would export to HTML5, but after trying to dig around I couldn't really find out how to create an animation using HTML5 without the use of a program to do it. I found Hype on the Mac app store which I downloaded and have been using, but is this something else I should be learning another way?

Again, thank you for your time, I do really appreciate it. I should of signed up to this site long ago and maybe I would be doing stuff right by now.....
 

Phreaddee

Super Moderator
Staff member
I only ever use sprites on buttons. that is if I havent already decided to make it entirely with css...
 

CaldwellYSR

Member
I wouldn't have any problems with you adding me.Click the little aim dude under my name.

1. I agree with Phreaddee about the sprites. I don't use them too often. I've done a couple buttons, arrows for navigation, little detail things that I can slap together and seem to make sense together. I definitely don't recommend doing it with all your images... too much work.

2. Flash is never the correct answer for web design. JQuery (a javascript library found here) will do almost everything people use flash for and it will do it better. As for html5... the canvas tag along with different api's is what would be used but I haven't gotten around to messing with that yet. I know it's pretty powerful but I've never found myself in a position where I needed it yet.
 

Lizyd Design

New Member
This may be a little off topic, try looking at the source code of other websites, not sure if you have been doing that already, but it taught me a ton at least. :)
 
Top