Hello,guys!I want to make the text of the navbar link another color when active,could you help me?
<nav><ul>
<li><a href="index.html">HOME</a></li>
<li><a href="pagina2.html">YOUTH</a></li>
<li><a href="#">APPLE</a></li>
<li><a href="#">LEAVING APPLE</a></li>
<li><a href="#">BACK TO APPLE</a></li>
<li><a href="#">LEGACY</a></li>
</nav></ul>
</header>
First, you give the appropriate link a class=âactiveâ on each individual page.
Then you apply a style to .active in your css file.
Thatâs it.
1 Like
It does`n work,it just changes the color of the text even if the link is not active
html;
<nav><ul>
<li><a href="index.html" class="active">HOME</a></li>
css:
.mainheader nav ul li .active {
color: black;
}
By âactiveâ, do you mean the moment at which the link is activated, or do you mean the link for the current page?
for example when i am on the home page i want the text to be another color,so you can see that you are on the homepage
Then as Dr_John said, the simplest way is to add a class to the appropriate link on each page, and apply styles to that. So on your âhomeâ page, you add the class to the âhomeâ link, on your âyouthâ page, you add it to the âyouthâ link, etc.
Dr_John
November 6, 2017, 10:14pm
7
For example, see this site, and work through the menu, checking the source code each time.
http://manorgaragewantage.co.uk/index.htm
It uses current instead of active, but itâs just a name.
ronpat
November 7, 2017, 5:15am
8
You might be able to use the âmatching pairsâ technique. A couple of old examples should help:
[font=verdana]The other way to style the link to the current page â if you arenât able to put a class/ID on the relevant <li>, maybe because youâre including the menu so canât change it page-by-page â is to use âmatching pairsâ.
To do this, you give each <li> a different class name, and then you put the matching name as an ID on the body tag.
So, for example, the menu would be
<ul>
<li class="home"><a href="index.html">Home</a></li>
<li class="about"><a href="about.html">About Us</a></li>
<liâŚ
mslee,
I believe that the following two examples address your issue. And I am guessing that the reason the pages you were referred to didnât help is that you do not quite appreciate specificity (thus, I have included TWO examples).
Note that classnames were added to all of the list items AND the <body> tag has been given an id.
The first example uses your code and demonstrates how to override the link coloring on each page with the !important property. However, it is usually unnecessary.
TâŚ
1 Like
Iâve used the above matching pairs method successfully when the menu was added by javascript, but I canât remember who it was for so canât point at a live page to view. you just have to remember to update the css file as well when adding a new page.
SamA74
November 7, 2017, 10:39am
10
raulstefan98:
</nav></ul>
Off Topic Sort out the nesting of your elements, the closing </nav>
tag should not be inside the <ul>
.
Gandalf
November 7, 2017, 10:59am
11
If you are not using the active class elsewhere you would be better off just setting styles using
.active
rather than using all the preceding styles and tags as well.
1 Like
system
Closed
February 6, 2018, 5:59pm
12
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.