Therefore, CSS does not support this function directly. So you need to look for alternatives and weigh compromises.
Use JavaScript and jQuery to add a class
$(function () { $('a[href="' + document.location.pathname + '"]').addClass("current"); });
Then you can create your styles around the .current selector.
a.current {background-color: white;}
Just do not put class="current" in your HTML - let everything be fine when the page loads, and javascript will add the class to the correct elements. This is good because it will work on any page and does not require dynamic code on the server. Of course, this can be done without jQuery. This may require adjusting the query string parameters, absolute URLs, and several other edge cases, but is great for a simple case.
Use server side dynamic code to put class = "current" in the appropriate elements
You could do it the same way, but it will not be as concise and expressive as the JavaScript version, because most server-side software environments do not have DOM selectors available.
source share