Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I highlight a link based on the current page?

Sorry if this sounds like a really stupid question, but I need to make a link change colour when you are on the page it links to.

For example, when you are on the "Questions" page of StackOverflow, the link at the top changes colour. How do you do this?

like image 684
Click Online Design Avatar asked Oct 07 '08 23:10

Click Online Design


People also ask

How do you highlight the current link in CSS?

If you want to highlight the "current" menu entry, you have to make it discernible to CSS by making it special in the HTML code - for example by adding a class "active" or "current".

How do I change the color of a link when I click it?

To change the color of links in HTML, use the CSS property color. Use it with the style attribute. The style attribute specifies an inline style for an element. Use the style attribute with the CSS property color to change the link color.


2 Answers

It really depends on how your page is constructed. Typically, I would do this using CSS, and assign give the link an id called "active"...

<a id="active" href="thisPage.html">this page</a>

...and in the CSS...

a#active { color: yellow; }

Obviously this is a fairly simplistic example, but it illustrates the general idea.

like image 146
steve_c Avatar answered Nov 14 '22 22:11

steve_c


It's a server-side thing -- when rendering the page, add a class like "current-page" to the link. Then you can style it separately from the other links.

For example, StackOverflow renders the links with class="youarehere" when it points to the page you're already on.

like image 20
John Millikin Avatar answered Nov 14 '22 23:11

John Millikin