tags:

views:

183

answers:

4

I have a link, where I want to change the color of the text away from the color that I set for hyperlinks. My code is:

<span class="button"><%= link_to "Create new scenario", :action => "create" %></span>

And my CSS is:

a:link { 
  color:rgb(50%, 15%, 5%);
  text-decoration:none; 
} 


.button {
  -moz-border-radius-bottomleft:6px;
  -moz-border-radius-bottomright:6px;
  -moz-border-radius-topleft:6px;
  -moz-border-radius-topright:6px;
  background-color:rgb(93%, 93%, 93%);
  border:1px solid black;
  color:black !important;
  line-height:1.9;
  margin:0 3px 3px 0;
  padding:4px 8px 4px 3px;
  text-decoration:none;
}

For some reason the hyperlink text is still brown, rgb(50%, 15%, 5%).

+3  A: 

You could make a css style .button a:link {color: black;}

Ballsacian1
ITYM: .button a:link {color:#000}
Sinan Ünür
...what's 'ITYM'?
David Thomas
http://www.google.com/search?q=define%3AITYM
Sinan Ünür
Thank you! I should've remembered that Google is, indeed, my friend... =]
David Thomas
+3  A: 

Change your css to use the .button class and anchors with a parent css class of .button. as shown below:

.button,.button a:link {
  -moz-border-radius-bottomleft:6px;
  -moz-border-radius-bottomright:6px;
  -moz-border-radius-topleft:6px;
  -moz-border-radius-topright:6px;
  background-color:rgb(93%, 93%, 93%);
  border:1px solid black;
  color:black !important;
  line-height:1.9;
  margin:0 3px 3px 0;
  padding:4px 8px 4px 3px;
  text-decoration:none;
}

EDIT: Keep in mind that this causes the border to repeat and makes the hyperlink show up without an underline because of text-decoration:none. The best practice in this case is to have a separate css declarations.

.button {....}
.button a:link {.....}
Jose Basilio
+3  A: 

I think it's because of the specificity; the span (.button) is less specific to the link than the a:link so the a:link styles are being applied (correctly according to the spec: http://www.w3.org/TR/CSS2/cascade.html).

If you want to override the a:link styles for this one button (or...well, any other in the same way) add the class to the <a> tag rather than its parent element.

Though you might get away with:

.button > a:link {/* styles */}

Which should become specific since this one <a> is the descendant of the the span of class .button.

Edit:

It's worth pointing out that the '>' selector applies only to immediate descendants, so an a inside an element of class .button would be affected, however an a inside a div in turn inside an element of class .button would not be affected.

Also this selector is not supported by IE (certainly below version 7, and I don't know about version 7 -or, indeed, version 8). It might be okay to use, instead, the '*' operator:

.button * a:link {/* styles */}

bearing in mind that while this is supported -I think- in IE after version 5.x at least, it's a little broad in that it will target all as within an element of class .button, regardless of any interim elements, and will still likely be less-specific than any rule applied to a:links.

David Thomas
I think IE doesn't support child selectors.
Josef
Indeed, and this was pointed out in the 'wrong' place, since the person pointing it out originally (billyswong) is as yet unable to comment on others' answers.
David Thomas
A: 

"! important" is not for forcing child's style. It's for the user to override styles assigned by webpage author. It has no use in your case.

The proper way to do it is:

.button {
  -moz-border-radius-bottomleft:6px;
  -moz-border-radius-bottomright:6px;
  -moz-border-radius-topleft:6px;
  -moz-border-radius-topright:6px;
  background-color:rgb(93%, 93%, 93%);
  border:1px solid black;
  color:black;
  line-height:1.9;
  margin:0 3px 3px 0;
  padding:4px 8px 4px 3px;
  text-decoration:none;
}

.button a {
  color:black;
}

Remarks:

  1. ".button > a" is a good idea but it won't work in IE6. Therefore one should use ".button a" here to be safe.
  2. Putting ".button" and ".button a" together in one set of style will make the button border repeat itself.
billyswong
Thank you, that's why I first advocated using an explicit class (or id if it's a one-off) on the <a> element. It just makes more sense to me.
David Thomas
Well, it seems that the questioner is using PHP or something like that and somehow could not or hard to add explicit class onto the auto-gen'ed link.And beware that "`>`" is not for descendants in general. It is specific to immediate children.(I wish I have reputation high enough to comment on others' comments...)
billyswong
While I didn't define the 'immediate-descendant' only relationship, I did appreciate that it was so. I'll amend my post slightly to reflect that more clearly. Thanks for the feedback
David Thomas