treehouse : what would you like to learn today?
Web Design Web Development iOS Development

CSS Sprite - 3 states desired - enabled, hover, and current

  • I have created a sprite for my global header. I can get the CSS to work for enabled and hover states but want to move to the current state and I can't figure out how.

    Enabled CSS - ul#menu li a.contact {
    width: 189px; background-position: -575px 0;

    Hover CSS - ul#menu li a.contact:hover, ul#awesome-menu li a.contact:focus {
    background-position: -575px -70px;
    What do I need for current state?
    Thank you,
    Laurie
  • Not sure what you meant by enabled...?
  • Enabled is the original state of button or tab. It is click-able as opposed to disabled.
  • do you mean a:active?

    so your three states would be...

    a - for any general styling when left alone and unclicked
    a:hover - for any cursor interaction
    a:active - if the button has just been clicked?

    i'm also a little confused... sorry
  • I'm the one confused trying to do this!

    1st YES - a - for any general styling when left alone and unclicked
    2nd YES - a:hover - for any cursor interaction
    3rd Maybe - a:active - if the button has just been clicked?

    I also want the active state for the current / selected state. The button would remain on "3rd" until clicking on a different link/button.

    Still clear as mud?
    Thanks,
    Laurie
  • Do you mean you want something like a bullet list?
  • I think she wants the "Enabled" to be 'currently selected'...? So a highlighted link to indicate that they're on that particular page. (different color than the rest, even when the mouse goes away).

    a:active is on mouse press and hold, so that's not really accomplishing what she wants.

    You can set a class="current" on your anchor of the page you are on, if you want to do it manually per page, but my favorite is to do jquery url string matching to set the class on the anchor, that way it's dynamic per page.

    Am I wrong? Hopefully I helped. If you want the jquery solution let me know, i'll paste it in here :)

    ~ Chris
  • Y'all are fabulous. Thanks for helping. Remember, I am using a sprite for my global header. I will see if I can use a class="current" on this site, because I only have 5 pages so far.

    BUT, my next project is one with lots of departments (two rows of tabs) so maybe the jquery thing will work. I've not learned that yet. Will I just cut and paste or will I need more education?

    Thank you,
    Laurie
  • <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script&gt;
    <script type="text/javascript">
    //Page Highlighting
    var url = window.location.toString()
    $(document).ready(function(){
    $('nav li a').each(function(){
    var myHref= $(this).attr('href');
    if( url.match( myHref )){
    $(this).addClass('current');
    }
    });
    });
    </script>

    Including the above will get you started (including the Jquery library).

    It matches the url string to the href that's in the anchor:

    http://mywebsite.com/about.html
    would match
    <a href="about.html" class="about">About</a>


    Then Just rename 'nav li a' to the id or class that you have your navigation set up as, and make sure you keep the anchor 'a' at the end, example: #mynavigation a

    Since you're using a sprite, name each anchor with a class, example: class="about", then in CSS for it's current state write: a.about.current{} to specifically target that anchor and add your sprite styles for it.

    It sounds a bit complicated, but all in all not that bad, you end up with a navigation like so:

    <nav>
    <ul>
    <li><a href="/" class="home">Home</a></li>
    <li><a href="/about.html" class="about">About</a></li>
    <li><a href="/services.html" class="services">Services</a></li>
    <li><a href="/portfolio.html" class="portfolio">Portfolio</a></li>
    <li><a href="/contact.html" class="contact">Contact</a></li>
    </ul>
    </nav>

    Also, if you don't care about supporting IE6/7/8 then using :nth-child in your CSS to select the nav item to style instead of using specific class names on each anchor is definitely better, so for About it would be: nav li:nth-child(2) a.current{}

    Hope all of this helps you! (there are other technologies that can do this too, like PHP, ColdFusion, ASP, ect, but Jquery's the easiest way to start!)

    ~ Chris
  • Wow! I will see if I can do it, and let you know.

    Thank you Chris for your time.

    Laurie
  • Hello guys!
    It's still a work in progress, but check it out if you like - LMahoney.com

    My top nav still has movement issues but I'm getting there.

    Thanks again for all your comments.
    Laurie
  • The image on the home page is nearly 1mb! Imagine somebody on their mobile trying to check out the website! Even on a super high speed connection it won't load instantly. Same thing with the Contact page - try reducing those file sizes!
  • Oh yes I know. Thanks for the reminder to fix the images. lots of stuff is thrown up without much thought.

    It's really good to know about that smush it site. I will try it out.

    Thanks for taking the time to help.
    Laurie