How to create a link corresponding to the current URL?

I would like to add CSS background color to any link in the navigation bar (#subnav) that exactly matches the current URL.

I tried div#subnav a:active { background: #f1f2f2; } div#subnav a:active { background: #f1f2f2; } , but that didnโ€™t work.

Any help would be appreciated!

UPDATE: The code for subnav is as follows:

 <!-- START ALL PRODUCTS SUBNAV --> {% if collection.handle == 'all' %} <div id="back_link"><a style="background: none; color: #fff;" href="/collections/all">.</a></div> <div id="subnav" style="margin-top: -33px;">{% if linklists.sub-navigation.links.size > 0 %}{% for link in linklists.sub-navigation.links %} <li class="main_category_link"><a href="{{ link.url }}"{% if link.url == collection.url %} class="active"{% endif %}>{{ link.title | escape }}</a></li>{% unless forloop.last %} {% endunless %}{% endfor %}{% else %} {% endif %} </div> {% endif %} <!-- END ALL PRODUCTS SUBNAV --> <!-- START HOME SUBNAV --> {% if collection.handle == 'home' %} <div id="back_link"><a href="#" onclick="history.go(-1);return false;">&larr; BACK</a></div> <div id="subnav"> <div class="main_category_link"><a href="/collections/home">HOME</a></div> {% if linklists.sub-navigation-home.links.size > 0 %} {% for link in linklists.sub-navigation-home.links %} <li><a href="{{ link.url }}"{% if link.url == collection.url %} class="active"{% endif %}>{{ link.title | escape }}</a></li>{% unless forloop.last %} {% endunless %}{% endfor %}{% else %} {% endif %} <div class="main_category_link"><a href="/collections/children">CHILDREN</a></div> <div class="main_category_link"><a href="/collections/women">WOMEN</a></div> <div class="main_category_link"><a href="/collections/pets">PETS</a></div> <div class="main_category_link"><a href="/collections/gifts">GIFTS</a></div> <div class="main_category_link"><a style="margin-bottom: 0;"href="/collections/sale">SALE</a></div> </div> {% endif %} <!-- END HOME SUBNAV --> <!-- START CHILDREN SUBNAV --> {% if collection.handle == 'children' %} <div id="back_link"><a href="#" onclick="history.go(-1);return false;">&larr; BACK</a></div> <div id="subnav"> <div class="main_category_link"><a href="/collections/home">HOME</a></div> <div class="main_category_link"><a href="/collections/children">CHILDREN</a></div> {% if linklists.sub-navigation-children.links.size > 0 %}{% for link in linklists.sub-navigation-children.links %} <li><a href="{{ link.url }}"{% if link.url == collection.url %} class="active"{% endif %}>{{ link.title | escape }}</a></li>{% unless forloop.last %} {% endunless %}{% endfor %}{% else %} {% endif %} <div class="main_category_link"><a href="/collections/women">WOMEN</a></div> <div class="main_category_link"><a href="/collections/pets">PETS</a></div> <div class="main_category_link"><a href="/collections/gifts">GIFTS</a></div> <div class="main_category_link"><a style="margin-bottom: 0;" href="/collections/sale">SALE</a></div> </div> {% endif %} <!-- END CHILDREN SUBNAV --> <!-- START WOMEN SUBNAV --> {% if collection.handle == 'women' %} <div id="back_link"><a href="#" onclick="history.go(-1);return false;">&larr; BACK</a></div> <div id="subnav"> <div class="main_category_link"><a href="/collections/home">HOME</a></div> <div class="main_category_link"><a href="/collections/children">CHILDREN</a></div> <div class="main_category_link"><a href="/collections/women">WOMEN</a></div> {% if linklists.sub-navigation-women.links.size > 0 %}{% for link in linklists.sub-navigation-women.links %} <li><a href="{{ link.url }}"{% if link.url == collection.url %} class="active"{% endif %}>{{ link.title | escape }}</a></li>{% unless forloop.last %} {% endunless %}{% endfor %}{% else %} {% endif %} <div class="main_category_link"><a href="/collections/pets">PETS</a></div> <div class="main_category_link"><a href="/collections/gifts">GIFTS</a></div> <div class="main_category_link"><a style="margin-bottom: 0;" href="/collections/sale">SALE</a></div> </div> {% endif %} <!-- END WOMEN SUBNAV --> <!-- START PETS SUBNAV --> {% if collection.handle == 'pets' %} <div id="back_link"><a href="#" onclick="history.go(-1);return false;">&larr; BACK</a></div> <div id="subnav"> <div class="main_category_link"><a href="/collections/home">HOME</a></div> <div class="main_category_link"><a href="/collections/children">CHILDREN</a></div> <div class="main_category_link"><a href="/collections/women">WOMEN</a></div> <div class="main_category_link"><a href="/collections/pets">PETS</a></div> {% if linklists.sub-navigation-pets.links.size > 0 %}{% for link in linklists.sub-navigation-pets.links %} <li><a href="{{ link.url }}"{% if link.url == collection.url %} class="active"{% endif %}>{{ link.title | escape }}</a></li>{% unless forloop.last %} {% endunless %}{% endfor %}{% else %} {% endif %} <div class="main_category_link"><a href="/collections/gifts">GIFTS</a></div> <div class="main_category_link"><a style="margin-bottom: 0;" href="/collections/sale">SALE</a></div> </div> {% endif %} <!-- END PETS SUBNAV --> <!-- START GIFTS SUBNAV --> {% if collection.handle == 'gifts' %} <div id="back_link"><a href="#" onclick="history.go(-1);return false;">&larr; BACK</a></div> <div id="subnav"> <div class="main_category_link"><a href="/collections/home">HOME</a></div> <div class="main_category_link"><a href="/collections/children">CHILDREN</a></div> <div class="main_category_link"><a href="/collections/women">WOMEN</a></div> <div class="main_category_link"><a href="/collections/pets">PETS</a></div> <div class="main_category_link"><a href="/collections/gifts">GIFTS</a></div> {% if linklists.sub-navigation-gifts.links.size > 0 %}{% for link in linklists.sub-navigation-gifts.links %} <li><a href="{{ link.url }}"{% if link.url == collection.url %} class="active"{% endif %}>{{ link.title | escape }}</a></li>{% unless forloop.last %} {% endunless %}{% endfor %}{% else %} {% endif %} <div class="main_category_link"><a style="margin-bottom: 0;" href="/collections/sale">SALE</a></div> </div> {% endif %} <!-- END GIFTS SUBNAV --> <!-- START SALE SUBNAV --> {% if collection.handle == 'sale' %} <div id="back_link"><a href="#" onclick="history.go(-1);return false;">&larr; BACK</a></div> <div id="subnav"> <div class="main_category_link"><a href="/collections/home">HOME</a></div> <div class="main_category_link"><a href="/collections/children">CHILDREN</a></div> <div class="main_category_link"><a href="/collections/women">WOMEN</a></div> <div class="main_category_link"><a href="/collections/pets">PETS</a></div> <div class="main_category_link"><a href="/collections/gifts">GIFTS</a></div> <div class="main_category_link"><a style="margin-bottom: 0;" href="/collections/sale">SALE</a></div> {% if linklists.sub-navigation-sale.links.size > 0 %}{% for link in linklists.sub-navigation-sale.links %} <li><a href="{{ link.url }}"{% if link.url == collection.url %} class="active"{% endif %}>{{ link.title | escape }}</a></li>{% unless forloop.last %} {% endunless %}{% endfor %}{% else %} {% endif %} </div> {% endif %} <!-- END SALE SUBNAV --> 
+7
source share
5 answers

An active property is not what you think. In the CSS world, a link becomes active after clicking on it. Of course, this will lead you to the page with which it is connected, so in fact the link is active for a very short period of time.

There are several approaches you can take to do what you want. Adding a class to the link that you want to mark as active is the most obvious:

Your HTML:

 <a href="link.html">Link1</a> <a href="link.html" class="active">Link2</a> <a href="link.html">Link3</a> 

Your CSS:

 .active { font-weight: bold; } 

Thus, the active link becomes bold. Of course, this should be done on the server side.

Check here for other approaches, including with JavaScript

+3
source

Hope I interpreted your question correctly.

You may need to use some javascript to do this. The current URL can be obtained using:

  var url = document.URL; 

Then you can compare this with the value in each link in the loop across all links. When you find the same value with the URL, you add CSS to change the color.

eg.

  thelink.style.backgroundColor = "#F1F2F2"; 

And looping the elements can be done something like this (there was a time since I did it without jQuery, so I donโ€™t know how this works, but this is the beginning.):

  var links = document.getElementById('subnav').elements; for(var i = 0; i < links.length; i++) { if(links[i].getAttribute('href') === url){ links[i].style.backgroundColor = "#F1F2F2"; } } 

So, assuming you have a bunch of <a> elements in the subnav div, this will go through them and compare them with the URL of the current page and change the color accordingly. This can be placed in the function associated with the onload event on the page.

Better yet, jQuery is to simplify the code and make it more secure in multiple browsers.


Given your comment below, what about this jQuery solution:

  $(document).ready(function(){ $("#subnav a").each(function(){ if($(this).attr('href') == document.URL){ $(this).css("background-Color", "red"); } }); }); 

So you can install jQuery and add this to the top of your page. It basically looks at every <a> element in the subnav div and compares the href attribute with the url of the current page and makes changes to css if they match. You can change what changes are made.

+2
source

You can use JavaScript ( gist ) to add .current to the bindings that match the current URL. a.href gives the full URL, even if the [href] attribute is relative.

 //gist.github.com/ryanve/6153436 (function(anchors, url, i, a) { while ((a = anchors[i++]) && a.classList) a.href === url && a.classList.add('current'); }(document.getElementsByTagName('a'), location.href, 0)); 

Then you can create the style via CSS: .current { color:orange }

+2
source

You have a small typo - instead of a:active you should have a.active

 div#subnav a.active {background: #f1f2f2;} 
+1
source

I did not think that the Active pseudo class does this, although the Active is the current selected link, so if you hover over the link and click without releasing the mouse button, the Active style will be applied.

I made a fiddle demonstrating this here:

http://jsfiddle.net/39DuU/

If you hover over a link, it will become lime; if you click on the link but donโ€™t release the mouse button, it will turn red.

An alternative would be to use Javascript / JQuery to achieve this goal, which I demonstrated here:

http://jsfiddle.net/39DuU/1/

0
source

All Articles