简体   繁体   中英

Highlight the navigation tab on current page with CSS?

I'm new to html/css stuff so please bear with me. I have specific images that I want to use as a navigation menu. My html,css basically look something like this:

HTML

<div id="navigation">
 <a id="main" href="domain.com/main">main</a>
 <a id="tips" href="domain.com/tips">tips</a>
 <a id="news" href="domain.com/news">news</a>
</div>

CSS

a#main:
{
  background-image:url(main-normal.png);
  margin-right:0px;
}

a#main:hover:
{
  background-image:url(main-highlight.png);
}

a#tips:
{
  background-image:url(tips-normal.png);
  margin-right:0px;
}

a#tips:hover:
{
  background-image:url(tips-highlight.png);
}

a#news:
{
  background-image:url(news-normal.png);
  margin-right:0px;
}

a#news:hover:
{
  background-image:url(news-highlight.png);
}

#main,#news,#tips
{
 background-repeat:no-repeat;
 display:block;
 text-decoration:none;
 text-indent:-30000px;
}

When I hover over the image it does get change to highlighted image but when I click on the link the tab goes back to normal image. I want it to stay highlighted when the user is currently on that page. Can anyone help me with this? I tried "visited" but it doesn't seem to work and everything I found on Google search was a little bit different than what I'm trying to do.

Thanks in advance.

There are many ways to do this. With PHP, JavaScript or pure CSS with some extra markup.

Since you said you are new to HTML/CSS I think the CSS way would be best for you?

So you have 3 pages with exact the same menu code? Just put this on each site:

For your main page:

<div id="navigation">
 <a id="main" class="active" href="domain.com/main">main</a>
 <a id="tips" href="domain.com/tips">tips</a>
 <a id="news" href="domain.com/news">news</a>
</div>

Your tips page:

<div id="navigation">
 <a id="main" href="domain.com/main">main</a>
 <a id="tips" class="active" href="domain.com/tips">tips</a>
 <a id="news" href="domain.com/news">news</a>
</div>

Your news page:

<div id="navigation">
 <a id="main" href="domain.com/main">main</a>
 <a id="tips" href="domain.com/tips">tips</a>
 <a id="news" class="active" href="domain.com/news">news</a>
</div>

Add CSS:

#main.active {background-image:url(main-highlight.png);}
#tips.active {background-image:url(tips-highlight.png);}
#news.active {background-image:url(news-highlight.png);}

To answer the specific question, when a user clicks a link, they are taken to a new page. The links have no concept in and of themselves as to what page they exist on.

So you have a few options. The common solution is to use server-side code to send HTML with some indicator that that is the current link. You could add a class like 'current' for example. Furthermore, if that is the page you are on, there's no need for the link either, so one should not have that text linked.

But if you don't have access to the server, then you need to add this manually. You could either create a new style for .current and then on that page, apply that class to your active link.

However, perhaps you are including this menu as an include...meaning the code is the same for every single page. In that case, you will want to target the active link with your CSS. So on each page you'd have a custom bit of CSS on the page.

On the tips page, for example, you'd do this:

#tips {put your style here for active links}

To get a bit fancier/more automated, you could write some javascript that would:

  • parse the URL
  • figure out the file name of the page
  • match that filename up to something in your list of links
  • apply the class for you dynamically

PS. there is no real need to use the syntax of a#tips in your CSS. The reason is that if you are using an id, there can only be one id on the page, so the a is somewhat redundant.

This example works fine for me...

Home Page

<html>
<head>
    <title>Home</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body#home a#homenav, body#second a#secondnav {
            color: #f8f8f8;
            background: #D34;
            cursor: default;
        }
    </style>
</head>
<body id="home">
    <nav>
        <ul>
            <li><a href="nav.html" id="homenav">Home</a></li>
            <li><a href="nav2.html"  id="secondnav">Second</a></li>
        </ul>
    </nav>
</body>

Second Page

<html>
<head>
    <title>Second</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
    body#home a#homenav, body#second a#secondnav {
        color: #f8f8f8;
        background: #D34;
        cursor: default;
    }
</style>
<body id="second">
    <nav>
        <ul>
            <li><a href="nav.html" id="homenav">Home</a></li>
            <li><a href="nav2.html"  id="secondnav">Second</a></li>
        </ul>
    </nav>
</body>

A way to highlight the navigation tab on the current page with CSS is to create an active class to append to the class of the tag you are currently on. Your active css class would detail how you want it to look when it's considered active. Then, depending on what tab you are on, you insert the active as class for that tab as seen below.

`
<html>
<head>
  <title>Highlight Nav Tab of Page</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-    scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<style>
.navbar {
  background-color: purple;
  font-size: 12px !important;
  line-height: 1.42857143 !important;
}
.navbar li a, .navbar .navbar-brand {
  color: #fff !important;
}
.navbar-nav li a:hover, .navbar-nav li.active a {
  color: purple !important;
  background-color: #fff !important;
}
</style>
<body id="about.html" data-spy="scroll" data-target=".navbar" data-offset="60">
<!--navbar-->
    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav navbar-right">
                <li ><a href="#">Home</a></li>
                <li class = "active"><a href="#">About</a></li>
                <li ><a href="#">More</a></li>
                <li ><a href="#">Contact Us</a></li> 
            </ul>
        </div>
        </div>
    </nav>
</body>
</html>
`

Image shows what nav highlight looks like

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM