How to show current page using CSS? .current_link not working - css

I'm trying to show the current page link in a different color. I've found other answers that will do this, but its still not working. I'm using a class of current_link on the respective links of each page. I also found an answer that said to apply the !important tag to the color rule but that didn't do anything. I'm thinking I have something small wrong or that I'm not aware of. Maybe some kind of ordering rule.
Here's the CSS rules relative to my links. As you can see I have .current_link at the top (I figured this would get rid of any ordering/over riding issues). The relative HTML naming will follow.
.current_link {
color: #00AD26;
}
#main_nav a:link, a:visited {
text-decoration:none;
color: #00A3E6;
}
#main_nav a:hover {
text-decoration: none;
color: #A8EDFF;
}
#main_nav a:active {
text-decoration: none;
color: #00B7FF;
}
a:link, a:visited {
text-decoration:none;
color: #00A3E6;
}
a:hover, a:active {
text-decoration: none;
color: #00B7FF;
}
Relative HTML from one of the pages.
<ul id="main_nav" class="grid_5 prefix_9">
<li id="home" class="current_link">Portfolio</li>
<li id="about">About</li>
<li id="contact">Contact</li>
</ul>

Your .current_link matches the <li>.
The <a> inside the <li> overrides the color it inherits from its parent element.
You need to apply the color to the <a> itself, either by moving the class or by changing the selector to select <a> elements inside the <li>.
Also, lower rules override earlier ones (if they have the same specificity).

Try this:
.current_link a {
color: #00AD26 !important;
}

You should use:
#main_nav li.current_link a {
color: #00AD26;
}
This will overrule the other selectors and avoids using !important.

Related

CSS styling a link within a div class

I'm trying to stop a link from turning purple when you click it. My link is within a div with the class name "header". My code doesn't seem to work and the link just stays purple.
.header a:visited {
color: black;
}
The :visited selector is used to select visited links. If your HTML looks the same as below it should be working, Possibly you might want to use :focus or :active?
.header a:visited {
color: black;
}
.header a:focus {
color: pink;
}
<div class="header">
sdfsd
</div>
a:visited {
color: black;
}
<div>
Link
</div>
try ctrl + shift + r, might just be a browser cache thing.

CSS selector giving strange results

I'm getting some strange results from one of my selectors.
After a reset, I have some base settings - this being one:
a:not([class]) {
text-decoration:underline;
&:link, &:visited, &:hover, &:active {
color:#primaryColor;
}
&:hover {
text-decoration:none;
}
}
It does the job - partly.
This anchor with no href works
<a class="link-more mt24">Learn more</a>
However this anchor with an href doesn't work.
<a class="link-more mt24" href="https://www.bbc.co.uk">Learn more</a>
By work I mean that the first link correctly gets ignored, the second link isn't ignored even though it has a class.
For completeness, this is what Less is pushing out:
a:not([class]) {
text-decoration: underline;
}
a:not([class]):link,
a:not([class]):visited,
a:not([class]):hover,
a:not([class]):active {
color: #03a9f4;
}
a:not([class]):hover {
text-decoration: none;
}
Any ideas?
The behavior is as expected. a:not([class]) would select and style a elements that don't have the class attribute. So the third a in the below snippet is underlined as it doesn't have class attribute.
The first a doesn't get the underline because a elements without href attribute assigned to it won't get the underline by default. This is because the text-decoration: underline is normally set using a selector like a:-webkit-any-link (WebKit specific, but other UA's will have similar ones).
The second a has the underline because of the default styling (indicated above) that is applied by the UA for a tags. The a:not([class]) does not have any effect on it (that is, it is not the reason for the underline) because the selector won't even point to that element.
If you want all the a elements with class to not have underline then use a[class] and remove the underline.
a[class] { /* if you remove this selector, the second link will be underlined */
text-decoration: none;
}
a:not([class]) {
text-decoration: underline;
}
a:not([class]):link,
a:not([class]):visited,
a:not([class]):hover,
a:not([class]):active {
color: #ff0000;
}
a:not([class]):hover {
text-decoration: none;
}
<a class="link-more mt24">Learn more</a>
<a class="link-more mt24" href="https://www.bbc.co.uk">Learn more</a>
Learn more

Difference in syntax for anchors with IDs

Is there a difference between
ul#mainNav li a:active {
color: #0312a4;}
and
ul#mainNav li.active a {
color: #0312a4;}
? From what I can tell, they do the same thing.
Yes difference is there
ul#mainNav li a:active {
color: #0312a4;}
in this, active is a pseudo class. It will apply color to a when event happen like click and hold (Eg: link,visited,hover,active etc.. are pseudo classes and always starts with :)
and
ul#mainNav li.active a {
color: #0312a4;}
in this, active is a class.(color will apply to anchor tag when the parent element li tag with active class only.)
Yes, both are completely different. Do check below snippet that will show you difference. Click to first text so it will show green color to you.
a:active means when anchor tag is active and li.active a class "active" is set to li tag.
ul#mainNav li.active a {
color: red;
}
ul#mainNav li a:active {
color: green;
}
<ul id="mainNav">
<li><a>Click Here</a></li>
<li class="active"><a>Click Here</a></li>
</ul>

Styling :active state on a link

I'm just trying to create a simple menu. So when you click a link, it directs you to another page and shows you that you selected that link by changing colour.
The problem I have is when I click a link within the list, the page loads and for some reason the list elements all enter the a:visited field. Setting them to red. I have the JSFIDDLE Here. I know its simple but it's getting on my nerves.
CSS Code:
ul.nav_style{list-style: none;}
ul.nav_style li {padding-left: 1em; text-indent: -.7em;}
ul.nav_style li:before {
content: "• ";
color: #C0C0C0; /* or whatever color you prefer */
}
ul.nav_style li a:link { color: #C0C0C0; text-decoration: none;}
ul.nav_style li a:visited { color: #FF0000; text-decoration: none;}
ul.nav_style li a:hover { color: #58595B; text-decoration: none;}
ul.nav_style li a:active { color: #E6BD13; text-decoration: none;}​
HTML Code:
<ul class="nav_style">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
Thanks in advance.
Chris
The explanation is simple: all the links have the same href. Assuming you will change the hrefs, this will not be an issue on your final site. :)
See my fiddle. All I did was to change the hrefs. Does it work as you expect now? http://jsfiddle.net/TheNix/sR3Ub/3/
EDIT:
Now, if you simply wish to make sure the link looks the same regardless if it's been visited or not, all you have to do is to apply the same styling to the :link and :visited states, like so:
ul.nav_style li a:link,
ul.nav_style li a:visited { color: #C0C0C0; text-decoration: none;}
ul.nav_style li a:hover { color: #58595B; text-decoration: none;}
ul.nav_style li a:active { color: #E6BD13; text-decoration: none;}
Now the link should be grey when idle (even if it's been visited), darker grey when you hover over it, and gold when you click it. See fiddle: http://jsfiddle.net/TheNix/sR3Ub/5/
EDIT v2
It seems you are a little bit confused about the states of a link, so here's the low-down:
:link is the standard idle link (can also be targeted simply as a)
:hover is the act of hovering over the element with the cursor.
:visited is applied to links which have already been visited.
:active is the state when the link is active in the user interface — e.g. when you are hovering over it, and pressing left mouse button (onmousedown), or when using the TAB to highlight the link. When you release the mouse button (onmouseup) or tab out, it will revert back to it's previous state.
:active therefore have nothing to do with determining if it is the active page, it only applies when you hold the mouse button down.
What you are trying to do, is unfortunately not possible with CSS alone. Here is a simple example of how it can be achieved with jQuery. http://jsfiddle.net/TheNix/sR3Ub/6/
This may be a reason of security of browsers, http://dbaron.org/mozilla/visited-privacy
Mozilla http://blog.mozilla.org/security/2010/03/31/plugging-the-css-history-leak/ also informed about this. Most of latest browsers has taken this action to protect the users.
you must put target url in href or in data-href or etc,and after click in link and load new page you must check window location and set link color to red:
$(function(){
var url = window.location.href;
$("ul.nav_style li").each(function(i,e){
var $this = $(e);
if ($this.attr("href") == url){
$this.css("color","red");
}
};
});
Problem solved, I've decided to just use PHP to generate my lists using arrays and an if statement to give the current page its css class.
PHP CODE:
if($title == "Enter"){
$enterArrayPages = array("competition.php","full_detail.php", "judges.php", "get_involved.php", "employers.php");
$enterArrayText = array("The Competition","Full Detail","The Judges","Get Involved", "Employers");
echo '<ul class="nav_style">';
for($i=0; $i<sizeof($enterArrayPages); $i++){
if($proper_title == $enterArrayText[$i]){
echo '<li><a class="linkChange" href="'.$enterArrayPages[$i].'">'.$enterArrayText[$i].'</a></li>';
}
else{
echo '<li><a class="menu_links" href="'.$enterArrayPages[$i].'">'.$enterArrayText[$i].'</a></li>';
}
}
echo '</ul>';
}
So as you can see, the code loops through each of the pages stored in an array and if the title of the page is equal to the one in the array I change its class, making the color of the link different to the others.
Here is the CSS:
a.menu_links:link { color: #58595B; text-decoration: none;}
a.menu_links:visited { color: #58595B; text-decoration: none; }
a.menu_links:active { color: #E6BD13; text-decoration: none; }
a.menu_links:hover { color: #E6BD13; text-decoration: none; }
.linkChange {
color: #E6BD13;
text-decoration: none;
}
It's really simple, I just had to think of the logic via PHP. I thought that implementing it in pure css would be good enough, but with time ticking, I resorted to PHP.
Here is an image of the code output:
Thanks for everyones comments! Much appreciated!

style css working but link css not

Is there a reason my below CSS only half works?
div.share
{
position:relative;
top: -4px;
left: 25px;
font-family:Tahoma;
background-color:#000000;
font-size:11px;
font-weight:bold;
}
/* share link css */
a.share:active
{
color: #000000;
}
a.share:hover
{
color: #FFFFFF;
background-color:#000000;
text-decoration: none;
}
The div.share CSS is all working but the CSS for the active and hover is not
CSS is valid, but make sure the link does have the "share" class, if its in the DIV, change the css to:
div.share a:active
{
color: #000000;
}
div.share a:hover
{
color: #FFFFFF;
background-color:#000000;
text-decoration: none;
}
adding your html would make this easier.
I can only guess that you have a <div> with class='share' and no <a> tag with the same.
e.g., does your html look like:
<div class='share'>
<a class='share' href='http://yoursite.com'>Your site</a>
</div>
or
<div class='share'>
</div>
...
<a class='share' href='http://yoursite.com'>Your site</a>
If it's the first, then
div.share a:hover {
...
}
would make more sense.
If it's the second, then the selector looks fine... though it might be better to choose different, but appropriate class names.
Use div.share a:active and div.share a:hover.
The way you have it right now it is looking for an <a> tag with a share class applied directly. However the share class is on the outer div.
Can you show us an HTML snippet using this CSS? Is it really the <a> tag that has the share class or is it nested inside the <div class="share">?

Resources