/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] CSS Inheritance on Anchor Tags for NavBar

Hello,

I am new to CSS and am trying to finish off a project for a Web Development class which I am taking. I am having a bit of an issue with the external CSS file that I am linking to all of my pages. I have attempted to create a navigation bar on the left hand side of the page and it is working but I am having an issue with styling the anchor <a> tags in the nav bar as I already have styles defined in the sheet for a a:link, a:visited a:hover and a:active so any style that I put for the <a> tags in the nav bar get ignored and it instead inherits the styles from the above mentioned tags. I tried changing the order in the CSS file but then all of the <a> tags get values from the nav bar and drop the others.

Have created the nav bar using a unordered list where the .style_nav class is applied to the <ul> tag and the .li_nav class which is applied to the <li> tag. Here is the CSS file which I am using:

[CODE]

h2
{
background-color:#000000;
font-family: tahoma, arial;
color:#008B8B;
}

h3
{
background-color:#000000;
font-family: tahoma, arial;
color:#DCDCDC;
}

body
{
background-color:#000000;
font-family: tahoma, arial;
color:#708090;
font-size:14px;

}

a {text-decoration:none;}
a:link {color:#87CEEB;}
a:visited {color:#6A5ACD;}
a:hover {color:#66CDAA;}
a:active {color:#00FA9A;}

.site_nav {
float:left;
font-size:16px;
color:#7B68EE;
display:inline;
list-style:none;
margin:0;
padding:0;
overflow:hidden;

}

.li_nav

{
float:left;
}

.footer {text-align:center; font-size:11px; font-family:arial, sans-serif; color:#F0F8FF;}

.bottom_nav {text-align:center; font-size:11px;]

[/CODE]

Now, I would like to apply this style to the <a> tag but only to the <a> tags in the nav bar; the other <a> tags in the page should use the styles from above.

[CODE]

a:link,a:visited
{
display:block;
width:120px;
font-weight:bold;
color:#FFFFFF;
background-color:#98bf21;
text-align:center;
padding:4px;
text-decoration:none;
text-transform:uppercase;
}
a:hover,a:active
{
background-color:#7A991A;
}

[/CODE]

I hope this made sense. I would be grateful for any help or advice on how to accomplish this.

Thanks,
Max

to post a comment
CSS

6 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 20.2010 — Use selector chaining, this will increase the specificity thus overriding the general anchor styles [CODE].site_nav a:link,
.site_nav a:visited
{
[/CODE]
Copy linkTweet thisAlerts:
@MaxAvionauthorNov 20.2010 — Fang, thank you so much!

That was exactly what I needed and it worked perfectly. Really appreciate it.

Cheers,

Max
Copy linkTweet thisAlerts:
@NoasITMar 02.2011 — I know this is an old thread but it pertains to my problem:

My right nav links for some reason are inheriting the general anchor styling, instead of the class definitions. It worked fine when it wasn't placed in the same div as the main content, however the content then doesn't flow around the right nav as I would prefer. To see what I mean go to http://dev.noas.com/?action=about what is really confusing to me is that I have the default wrapper definitions defined first in the css file, then specific classes defined after that, to my understanding that combined with the general vs. class specific should make the cascade lead to class styling as the dominant.

If you could please look at my code to see if you can locate my error that would be awesome as apparently I'm missing something important about the cascade effect.
Copy linkTweet thisAlerts:
@MaxAvionauthorMar 02.2011 — Hi NoasIT,

Luckily this thread still generated an email alert&#8230; I might have picked up the problem in your code.

Your right navigation HTML code is as follows:

[code=html]
<div id="rightnav">
<div class="sidenavmid">
<ul>
<li class="sidenavheading">About Us</li>
<li class="sidenavlink right"><a href="#ms" class="sidenav">Mission Statement</a></li>
<li class="sidenavlink right"><a href="#board" class="sidenav">Board of Directors</a></li>
<li class="sidenavlink right"><a href="#staff" class="sidenav">Staff</a></li>
<li class="sidenavlink right"><a href="#history" class="sidenav">History</a></li>

</ul>
</div>
</div>

[/code]


The CSS for your links (global) is:

[CODE]
#main a:link, #main a:visited {
text-decoration: none;
color: #117878;
padding-left: 1px;
padding-right: 1px;
}
#main a:hover, #main a:active {
color: teal;
font-weight: bold;
}
[/CODE]


Your CSS for the links in the navigation area are declared using the class name .sidenav as:

[CODE]
a.sidenav {
text-transform: capitalize;
color: white;
text-decoration: none;
}
a.sidenav:hover {
color: black;
}
[/CODE]


The issue here is specificity (as Fang mentioned above - I had the same issue with my navbar), which is when the highest ranking declarations take over from lower ranking ones. ID&#8217;s have a higher specificity than Classes which have a higher specificity than HTML element selectors.

As you can see your links are declared inside the #main ID and the .sidenav classes are used to specify the <a> tags for the navigation. The .sidenav is a class and therefor a lower specificity so it is getting overridden by the #main link section.

What I would so is set your sidenav links to the following which will give them the correct specificity, you are not changing anything, just giving them a higher rank by indicating the rightnav ID which they are contained in (ID&#8217;s have higher rank as mentioned above)

[CODE]#rightnav a.sidenav {

text-transform: capitalize;
color: white;
text-decoration: none;
}
#rightnav a.sidenav:hover {
color: black;
}
[/CODE]


Or you could do:

[CODE]#rightnav .sidenavlink a {
text-transform: capitalize;
color: white;
text-decoration: none;
}
#rightnav .sidenavlink a:hover {
color: black;
}
[/CODE]


Hopefully this will work for you.

Max
Copy linkTweet thisAlerts:
@NoasITMar 03.2011 — Ah Thank you so much! I did not realize ID's had a higher ranking than classes, that is definitely good to know. Thanks so much for the help it works perfectly with just that little change.
Copy linkTweet thisAlerts:
@MaxAvionauthorMar 03.2011 — No worries at all! I am glad that this was able to help you out ? It is so easy to do that with CSS...

Have fun with your site.

Max
×

Success!

Help @MaxAvion spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...