The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Problems overwriting link styles
CodeKing
post Nov 20 2007, 06:18 PM
Post #1


Advanced Member
****

Group: Members
Posts: 175
Joined: 12-September 06
Member No.: 118



Here's a few links:

CODE
<div id="leftNav">


  <a href="/article/1" class="">
    Home
  </a>
  

  <a href="/article/2" class="special_link_1">
    Support
  </a>
  

  <a href="/article/5" class="special_link_2">
    A Long Page
  </a>
  

  <a href="/article/7" class="special_link_3">
    Editing Test
  </a>
  


</div>


Now here's the CSS for it

CODE

#leftNav {
    position:absolute;
    left:0px;
    top:70px;
    width:150px;
}

#leftNav a:link,
#leftNav a:visited,
#leftNav a:active {
    display:block;
    color:#FFF;
    background:#FF0000;
    width:150px;
    height:50px;
    text-decoration:none;
    text-align:center;
    vertical-align:middle;
    padding-top:10px;
}

#leftNav a:hover {
    background:#C00;
}


.special_link_1 a:link,
.special_link_1 a:visited,
.special_link_1 a:active {
  background:#1A1;
}

.special_link_1 a:hover {
  background:#080;
}


When a link has a class of special_link_1, it keeps the background color of left_nav, even though it shouldn't. Why is it doing this. (And I know that special_link_2 and 3 don't exist, i haven't created them)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Nov 20 2007, 06:31 PM
Post #2


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



Check the rules for calculating a selector's specificity. The #leftNav selectors are more specific than the .special_link_1 selectors, so they take priority.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CodeKing
post Nov 20 2007, 11:23 PM
Post #3


Advanced Member
****

Group: Members
Posts: 175
Joined: 12-September 06
Member No.: 118



I changed up my code. Now here's the links:

CODE
<div id="leftNav">


  <a href="/article/1" class="special_link_default">
    Home
  </a>
  

  <a href="/article/2" class="special_link_1">
    Support
  </a>
  

  <a href="/article/5" class="special_link_2">
    A Long Page
  </a>
  

  <a href="/article/7" class="special_link_3">
    Editing Test
  </a>
  


</div>


And here's the CSS

CODE
#leftNav {
    position:absolute;
    left:0px;
    top:70px;
    width:150px;
}

#leftNav a:link,
#leftNav a:visited,
#leftNav a:active {
    display:block;
    color:#FFF;
    width:150px;
    height:50px;
    text-decoration:none;
    text-align:center;
    vertical-align:middle;
    padding-top:10px;
}

.special_link_default a:link,
.special_link_default a:visited,
.special_link_default a:active {
    background:#FF0000 ! important;
}

.special_link_default a:hover {  
    background:#C00 ! important;
}


.special_link_1 a:link,
.special_link_1 a:visited,
.special_link_1 a:active {
  background:#1A1 ! important;
}

.special_link_1 a:hover {
  background:#080 ! important;
}


According to the article adding "! important" should work, but it doesn't.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Nov 21 2007, 12:08 AM
Post #4


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



You have your selectors wrong. They match nothing.

[ADDED]
And you don't need !important.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CodeKing
post Nov 21 2007, 12:38 PM
Post #5


Advanced Member
****

Group: Members
Posts: 175
Joined: 12-September 06
Member No.: 118



QUOTE(pandy @ Nov 20 2007, 11:08 PM) *
You have your selectors wrong. They match nothing.

[ADDED]
And you don't need !important.


2 of them don't, but the rest do.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Nov 21 2007, 12:55 PM
Post #6


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Really? So it's working then? tongue.gif

You use contextual selectors like this one here.
CODE
.special_link_default a:hover {  
    background:#C00 ! important;
}

That would match A inside another element with the class 'special_link_default'. You have nothing like that in the HTML. You need a selector that matches A with the class 'special_link_default' inside an element with the id 'leftNav'.

http://www.w3.org/TR/CSS2/selector.html#descendant-selectors
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Nov 21 2007, 01:16 PM
Post #7


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



And even then, you don't need !important at all. You just need a selector that's at least as specific as your current selectors, for example:
CODE
#leftNav a.special_link_default:link { ... }
Note the addition of the ID selector. Without an ID selector, it will never be more specific than the selector that does include an ID selector.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Nov 21 2007, 01:57 PM
Post #8


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Exactly. Because you have these specific rules later in the source than the general ones the cascade takes care of it.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 23rd April 2024 - 02:07 AM