SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Dynamically Style a Link to its "Active" Pseudo-class

    Here's my problem.

    I have strip of navigation tabs, which are marked up as a
    <ul> and styled to look like tabs with borders and all that.

    In my stylesheet I have the pseudo-classes for
    :link , :visited , :hover , :active and all of those work fine.

    But I'd like to have the browser highlight whichever link a viewer is currently on by having Javascript activate the already defined
    :active pseudo-class.

    What's the syntax for that? Is it even possible?

    I've been googling for almost an hour and havn't gotten anything.

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Sounds like you are getting active and current mixed up.

    The :active pseudoclass is the style that applies while the mouse or enter key is depressed - it has nothing to do with which is the current page. There is no :current pseudo class unfortunately.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    I know there's no :current pseudo-class. I'd like to artificially recycle what I defined for :active in my stylesheet to highlight what's current.

    Without having to use five lines of Javascript to set everything one style at a time, nor create a new class.

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    If the current one is supposed to be the one that relates to the current page, the advised way is to use a body identifier, so that you can then explicitly target that from the navigation itself.

    For example:

    Code html4strict:
    <body id="contactus">
    ...
    <ul id="nav">
        <li id="navaboutus"><a href="...">...</a></li>
        <li id="navcontactus"<a href="...">...</a></li>
        ...
    </ul>
    ...

    You can then use the following CSS to set a current status to the appropriate one:

    Code css:
    body#aboutus #navaboutus,
    body#contactus #navcontactus {
        /* current nav link style here */
    }

    This means that only on the contactus page, will the nav link for contactus have a different style, and likewise for other pages such as the aboutus page.

    Here's a video tutorial that takes you through the process.
    Current Nav Highlighting: Using PHP to Set the Body ID
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    The CMS is generating the HTML for the navigation so I'm stuck there.

    I'll figure something out though.

    Thanks!

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •