Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 28th September 2004, 1:30 PM   #1
SouthernMunk Thread Starter
Member
 
SouthernMunk's Avatar
 
Join Date: Dec 2002
Posts: 669
Default Different Images & CSS

I'm wanting to use different images on my website depending on what style the user has selected. For example, the default and sabretoothe styles used on OCAU use different images.

I was wondering how this could be done with CSS. At the moment I'm using:

Code:
XHTML:
<div class="ca"></div>

CSS:
div.ca
    {
    width:100px;height:100px;
    background-image:url('image.gif');
    background-position:center center;
    background-repeat:no-repeat;
    }
I know there'll be a better way to do this than using empty divisions, but I don't know how. I was thinking perhaps reading the image list from a text file, e.g.:
Code:
arrow:<img src="images/arrow.gif" alt="Arrow" />
msg_unread:<img src="msg_unread.gif" alt="Read Message" />
Should I use empty divisions, read values from a file, or use something else? Thanks.
__________________
Grammar Joke:
Q: What do you call Santa's helpers?
A: Subordinate Clauses
System:
Foxconn NF4UK8AA-8EKRS | AMD Athlon 64 3000+ | 2x256MB DDR400 Corsair RAM | Xpertvision 6600GT (PCIe) | Antec SLK2650BQE | Samsung TS-H552U | Samsung 80GB SATA
SouthernMunk is offline   Reply With Quote

Join OCAU to remove this ad!
Old 28th September 2004, 3:12 PM   #2
GreenBeret
Member
 
GreenBeret's Avatar
 
Join Date: Dec 2001
Location: Melbourne
Posts: 16,897
Default

CSS doesn't support conditional statements so it can't be purely CSS. You should either look at a template system (most preferable) or use a style switcher: http://www.alistapart.com/articles/n4switch/
__________________
Doctore: Crixus! What is beneath your feet?
Crixus: Sacred ground, Doctore. Watered with tears and blood.
GreenBeret is online now   Reply With Quote
Old 28th September 2004, 6:58 PM   #3
Shinglor
Member
 
Shinglor's Avatar
 
Join Date: Jun 2001
Location: Canberra
Posts: 2,388
Default Re: Different Images & CSS

Quote:
Originally posted by SouthernMunk
I'm wanting to use different images on my website depending on what style the user has selected. For example, the default and sabretoothe styles used on OCAU use different images.

Should I use empty divisions, read values from a file, or use something else? Thanks.
If you're using a server-side scripting language I suggest putting your CSS theme and images in the same directory eg.

Code:
<link rel="stylesheet" src="themes/$theme/style.css" media="screen, projection" />
<img src="themes/$theme/arrow.gif" alt="" />
<img src="themes/$theme/msg_unread.gif" alt="There are unread messages" />
Note the alt text, arrow is not a useful fallback. I just guessed on the other one. The CSS method you are using is acceptable (it's called Farner Image Replacement) although it's usually a span instead of a div. Just make sure you provide alternate text within the span or div.

This forum uses non-standard inaccessable presentational HTML, it hardly uses CSS at all. Not a good place to look for best practices.

Last edited by Shinglor; 28th September 2004 at 6:58 PM.
Shinglor is offline   Reply With Quote
Old 28th September 2004, 8:14 PM   #4
SouthernMunk Thread Starter
Member
 
SouthernMunk's Avatar
 
Join Date: Dec 2002
Posts: 669
Default

Thanks, Shinglor. You're replies are always helpful.

However, I've come across a new problem. In some styles I only want to use images as the main navigational links. In others I want to use images and text, and in others I want only text.

I know I can implement the image and text solution with the :before and :after pseudo-elements (or are they pseudo-classes?), but this is currently not supported by some browsers, inc. IE. What should I do in this case?
__________________
Grammar Joke:
Q: What do you call Santa's helpers?
A: Subordinate Clauses
System:
Foxconn NF4UK8AA-8EKRS | AMD Athlon 64 3000+ | 2x256MB DDR400 Corsair RAM | Xpertvision 6600GT (PCIe) | Antec SLK2650BQE | Samsung TS-H552U | Samsung 80GB SATA
SouthernMunk is offline   Reply With Quote
Old 28th September 2004, 9:31 PM   #5
Shinglor
Member
 
Shinglor's Avatar
 
Join Date: Jun 2001
Location: Canberra
Posts: 2,388
Default

Quote:
Originally posted by SouthernMunk
However, I've come across a new problem. In some styles I only want to use images as the main navigational links. In others I want to use images and text, and in others I want only text.
Here's an example navigation list
Code:
<ul id="nav">
 <li id="home"><a href="index.php"><span>Home</span></a></li>
 <li id="help"><a href="help.php"><span>Help</span></a></li>
 <li id="about"><a href="about.php"><span>About</span></a></li>
</ul>
CSS
If you want to replace the "Home" text with home.png you could do this
Code:
li#home span {
  visibility: hidden;
  width: 100px;
  height: 30px;
}
li#home a {
  background: url('home.png') no-repeat;
  width: 100px;
  height: 30px;
}
Or if you want the text below home.png you could do this.
Code:
li#home a {
  background: url('home.png') no-repeat top;
  padding-top: 30px;
  width: 100px;
}
Is that what you meant?

EDIT: Whoops, forgot to include the image in the anchor.

Last edited by Shinglor; 28th September 2004 at 9:48 PM.
Shinglor is offline   Reply With Quote
Old 28th September 2004, 9:47 PM   #6
SouthernMunk Thread Starter
Member
 
SouthernMunk's Avatar
 
Join Date: Dec 2002
Posts: 669
Default

Tis exactly what I meant, thankyou Shinglor.

LI are block elements are they not? It's just that my navigation links are not necessarily going to be vertical, so I thought I could add the image through the anchor element's class.

Could it work like this:

Code:
CSS:
a.nav_home span{visibility:hidden}
a.nav_home {background:url('home.gif') no-repeat}

XHTML:
<a class="nav_home" href="home.php"><span>Home</span></a>
EDIT: Code -> Like you said I was unnecessarily complicating the issue.
__________________
Grammar Joke:
Q: What do you call Santa's helpers?
A: Subordinate Clauses
System:
Foxconn NF4UK8AA-8EKRS | AMD Athlon 64 3000+ | 2x256MB DDR400 Corsair RAM | Xpertvision 6600GT (PCIe) | Antec SLK2650BQE | Samsung TS-H552U | Samsung 80GB SATA

Last edited by SouthernMunk; 29th September 2004 at 11:56 AM.
SouthernMunk is offline   Reply With Quote
Old 28th September 2004, 9:52 PM   #7
Shinglor
Member
 
Shinglor's Avatar
 
Join Date: Jun 2001
Location: Canberra
Posts: 2,388
Default

Quote:
Originally posted by SouthernMunk
Tis exactly what I meant, thankyou Shinglor.

LI are block elements are they not? It's just that my navigation links are not necessarily going to be vertical, so I thought I could add the image through the anchor element's class.

Could it work like this:

Code:
CSS:
a.nav_home span.nav_home {visibility:hidden}
a.nav_home {background:url('home.gif') no-repeat}

XHTML:
<a class="nav_home" href="home.php"><span class="nav_home">Home</span></a>
You just need to add the height and width of the image to your a element. If you want a horizontal menu you can float the list items to the left.

Also you don't need to set a class on the span, using inheritance (a.nav_home span) will do the same thing. I've updated my code to make the image a part of the link so it's clickable. I haven't tested it though so there could be some mistakes.

Generally if something is going to be unique use an id instead of a class.

EDIT: I just figured out that you don't like the idea of using an unordered list for a list of links. A list is better for mobile devices, screen-readers and text browsers. Show me how you have it currently and I'll try and style it as a list.

Last edited by Shinglor; 29th September 2004 at 4:10 AM.
Shinglor is offline   Reply With Quote
Old 29th September 2004, 8:11 AM   #8
SouthernMunk Thread Starter
Member
 
SouthernMunk's Avatar
 
Join Date: Dec 2002
Posts: 669
Default

The width and height properties of anchors and spans cannot be set (at least, not with the browsers I am using). Because of this, the background image doesn't display correctly.

This seems to work for me, but I don't know how 'correct' it is:
Code:
CSS:
li#home {
    width:100px;height:100px;
    background-image:url('image.gif');
    background-position:center center;
    background-repeat:no-repeat;
    }
li#home span {
    visibility:hidden;
    }

XHTML:
<ul>
<a href="home.php"><li id="home"><span>Home</span></li></a>
</ul>
Because the <li> is part of the anchor, I can set the dimensions and background image properly.

Quote:
EDIT: I just figured out that you don't like the idea of using an unordered list for a list of links. A list is better for mobile devices, screen-readers and text browsers. Show me how you have it currently and I'll try and style it as a list.
What I have currently is something like this:
Code:
<a href="home.php">Home</a>
<a href="search.php">Search</a>
<a href="members.php">Members</a>
... ... ...
As you can see, I simply used whitespace (the newline character) because I wanted them on the same line. If I was going to use an unordered list, how could I make them fit on the same line without absolute positioning? (or should I use absolute positioning?)
__________________
Grammar Joke:
Q: What do you call Santa's helpers?
A: Subordinate Clauses
System:
Foxconn NF4UK8AA-8EKRS | AMD Athlon 64 3000+ | 2x256MB DDR400 Corsair RAM | Xpertvision 6600GT (PCIe) | Antec SLK2650BQE | Samsung TS-H552U | Samsung 80GB SATA
SouthernMunk is offline   Reply With Quote
Old 29th September 2004, 3:22 PM   #9
Shinglor
Member
 
Shinglor's Avatar
 
Join Date: Jun 2001
Location: Canberra
Posts: 2,388
Default

Quote:
Originally posted by SouthernMunk
The width and height properties of anchors and spans cannot be set (at least, not with the browsers I am using). Because of this, the background image doesn't display correctly.
Sorry, I forgot to set it to display: block.

Quote:
Originally posted by SouthernMunk

What I have currently is something like this:
Code:
<a href="home.php">Home</a>
<a href="search.php">Search</a>
<a href="members.php">Members</a>
... ... ...
As you can see, I simply used whitespace (the newline character) because I wanted them on the same line. If I was going to use an unordered list, how could I make them fit on the same line without absolute positioning? (or should I use absolute positioning?)
Code:
ul#nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

ul#nav li {
  display: block
  float: left;
}
I'm going to make a test page now and make sure it works.

EDIT: http://members.optusnet.com.au/shing...zontalnav.html

I used display: none instead of visibility: hidden because the text can sometimes cause the box to expand. There was a reason I used visibility: hidden but I can't remember what it was.

Last edited by Shinglor; 29th September 2004 at 3:37 PM.
Shinglor is offline   Reply With Quote
Old 30th September 2004, 6:08 PM   #10
SouthernMunk Thread Starter
Member
 
SouthernMunk's Avatar
 
Join Date: Dec 2002
Posts: 669
Default

I've ended up with this - it seems to work OK, except that in IE6 the screen jumps around (I think this is the JavaScript functions).

Code:
CSS: 
span.n_home {
    background-image:url('n_home.gif');
    background-position:center left;
    background-repeat:no-repeat;
    text-indent:25px;
    }

span.n_home span {
    display:none;
    visibility:hidden;
    }

XHTML: 
<a href="home.php"><span class="n_home"><span>Home</span></span></a>
Quote:
Generally if something is going to be unique use an id instead of a class.
I'm using the xhtml class attribute in conjunction with the id attribute because the selected style changes with the use of onmouseover and onmouseout. I'm using id to identify the element and class to change the element's className attribute.

I don't know if there is any other way to do this.
__________________
Grammar Joke:
Q: What do you call Santa's helpers?
A: Subordinate Clauses
System:
Foxconn NF4UK8AA-8EKRS | AMD Athlon 64 3000+ | 2x256MB DDR400 Corsair RAM | Xpertvision 6600GT (PCIe) | Antec SLK2650BQE | Samsung TS-H552U | Samsung 80GB SATA
SouthernMunk is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 1:29 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!