Use CSS instead of <img>?

Discussion in 'HTML & Website Design' started by cc2365, Dec 16, 2008.

  1. #1
    I have a toolbar in my web page. The code looks like:

    
    <table id="toolbar">
     <tr>
      <td>
       <a href="...">
        <img alt="" src="icon1.gif" /> Link1
       </a>
       <a href="...">
        <img alt="" src="icon2.gif" /> Link2
       </a>
       <a href="...">
        <img alt="" src="icon3.gif" /> Link3
       </a>
      </td>
     </tr>
    </table>
    
    HTML:
    But I want to be able to control which icon to show by the text in CSS, how can I do that? (I tried to use CSS+div, but then the toolbar became a mess) :confused:
     
    cc2365, Dec 16, 2008 IP
  2. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I presume you want your image to span the entire table and not just a couple cells or rows?

    You can add this into your CSS definition for "toolbar":
    background-image: url('link/to/image.jpg');
    Code (markup):
    Bearing in mind that any styles you apply to the cells may make them opaque - it's just trial and error and seeing what works.
     
    Yesideez, Dec 16, 2008 IP
  3. cc2365

    cc2365 Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    @Yesideez: Think for helping, but that's not what I want. I have a toolbar and it looks like:

    *link1 *link2 *link3

    (imagine that the *s are different icons)

    And I hope I can control which icon to use via CSS.
     
    cc2365, Dec 16, 2008 IP
  4. PixelRaider

    PixelRaider Banned

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, you can do that. You need to make a new CSS class for each icon, for example:

    CSS:
    .link1 {
       background-image: url(images/link1.gif);
       background-position: top left;
       padding-left: 15px; */ Width of image */
    }
    
    .link2 {
       background-image: url(images/link2.gif);
       background-position: top left;
       padding-left: 15px; */ Width of image */
    }
    
    .link3 {
       background-image: url(images/link3.gif);
       background-position: top left;
       padding-left: 15px; */ Width of image */
    }
    Code (markup):
    HTML:
    <a href="#"><span class="link1">Link 1</span></a>
    <a href="#"><span class="link2">Link 2</span></a>
    <a href="#"><span class="link3">Link 3</span></a>
    Code (markup):
    Now, I haven't tested this although it should work. Let us know how it goes. :)
     
    PixelRaider, Dec 16, 2008 IP
  5. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I would opt to placing class within the declaration of the anchor links rather than using span.
     
    Yesideez, Dec 16, 2008 IP
  6. SiteTalkZone

    SiteTalkZone Peon

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You should use CSS and a list <ul><li></li></ul> instead of a table for navigation, much better for accessibility etc..
    If you want me to show you how PM msn/skype address.
     
    SiteTalkZone, Dec 16, 2008 IP
  7. LindseyInteractive

    LindseyInteractive Well-Known Member

    Messages:
    2,830
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    175
    #7
    Also, another way to do this is by using the UL and LI properties.

    And then creating the CSS style for the UL, and LI property, asn the LI .a effect.

    This should help, i will attach a link here so you can better understand what I am talking about.

    http://www.alistapart.com/articles/taminglists/

    There is a nice tutorial about it, then after you set your spacing and width and alignment issues, just use normal CSS style to add the

    li .a {

    Effect to it..I hope this helps some.
     
    LindseyInteractive, Dec 16, 2008 IP
  8. cc2365

    cc2365 Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    Thanks for helping. The code PixelRaider provide works, but not totally:

    [​IMG]

    As you see, only the top of the image shown, and I can't see the rest of part. How can I fix this? My CSS:

    
    #toolbar { /* this is a <div> */
     background-color:#C3D9FF;
     height:30px;
     width:100%;
    }
    
    h2 { /* I use <h2> to wrap <a> */
     font-size:9pt;
     font-weight:normal;
     margin:0px;
     line-height:30px; /* So texts will be in middle */
    }
    
    .link1 {
     background-position:top left;
     background-repeat:no-repeat;
     padding-left:20px;
     background-image:url('link1.gif');
    }
    
    Code (markup):
     
    cc2365, Dec 17, 2008 IP
  9. LindseyInteractive

    LindseyInteractive Well-Known Member

    Messages:
    2,830
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    175
    #9
    On your:

    }

    .link1 {
    background-position:top left;
    background-repeat:no-repeat;
    padding-left:20px;
    background-image:url('link1.gif');
    }

    You need to add a HEIGHT: **px;

    Attribute, and set it to your icons heights. If your icons are bigger than 30px high then you need to go in to your

    #toolbar { /* this is a <div> */
    background-color:#C3D9FF;
    height:30px;
    width:100%;
    }

    And increase this height as well.
     
    LindseyInteractive, Dec 17, 2008 IP
  10. cc2365

    cc2365 Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #10
    Although ShawnsITSolutions' tip doesn't work, I've found out that style="font-size:20px;" works. :) Thanks for everybody trying to help!
     
    cc2365, Dec 22, 2008 IP
  11. LindseyInteractive

    LindseyInteractive Well-Known Member

    Messages:
    2,830
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    175
    #11
    hmm that is very strange as this should have worked, did you use the exact same code that i put there? or did you make changes? Because the code i gave you was your code and just showing what should have been changed.

    Or maybe i didn't explain myself right :(
     
    LindseyInteractive, Dec 22, 2008 IP
  12. cc2365

    cc2365 Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #12
    @ShawnsITSolutions:
    Well, my current code (with font-size fix):
    
     
       <style type="text/css">
       <!--
        .link_icon {
         background-position:top left;
         background-repeat:no-repeat;
         padding-left:20px;
         font-size:20px;
     
        #link1 .link_icon {
         background-image:url('link1.gif');
        }
       -->
       </style>
       ...
       <a id="link1" href="...">
        <span class="link_icon">&nbsp;</span>
        <span class="link_text">Link1</span>
       </a>
    
    HTML:
    I use <span> so I can control them easily via CSS (hide text, etc.).
     
    cc2365, Dec 23, 2008 IP