tags:

views:

52

answers:

5

I have a link in a div,

I want the link color change to red when the div is hovered(not just when the link itself is hovered),however I can not make it work well. This is the exmaple:

<div class="witha"><a href="">Test color without tag A</a></div>
<div class="withouta">Test color with tag A</div>

This is the css:

<style type="text/css">
.witha,.withouta {
    height: 40px;
    border: solid 1px red;
    padding: 5px;
}

.witha:hover,.withouta:hover {
    color: red;
}

a:link {
    color:black;
}

a:visited {

}

a:hover {
    color:red;
}

a:active {

}
</style>

When test the page, I found that in the div which owns a tag a, the hover will not happen unless the link is hovered,it seems that the attribute of tag 'a' override the setting of the div.

How to make it work?(I just want the link color is changed once the div is hovered)


------------Update ---------------------------------

hi, the !important does not work. And I am afraid someone misunderstand me.

Here is a example, please check and test it.

<html>
<head>
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.witha,.withouta {
    height: 40px;
    border: solid 1px black;
    padding: 5px;
}
.witha:hover,.withouta:hover {
    color: red ! important;
}
a:link {
    color: black;
}
a:visited {

}
a:hover {
    color: red
}
a:active {

}
</style>
</head>
<body>
<div class="witha"><a href="">Test color without tag A</a><br/>This is not a link, it works fine(change to red when the div.witha is hovered).But the link above should change to red also.</div>
<div class="withouta">Test color with tag A</div>
</body>
</html>
A: 

Make hover color is important attribute

.witha:hover,.withouta:hover {
    color: red !important;
}
Bang Dao
A: 

You need to look at the specificity of CSS selectors.

Either increase the :hover one, or use !important.

alex
A: 

You didn't tell us which browser you are testing in.
IE6 allows 'hover' psuedo tag only on the <a> tags.

Sujoy
I am using firefox,but it is better if it support IE :)
hguser
A: 

I think what you need is this rule, if I understand your question correctly.

a:hover, .witha:hover a{
    color: red
}

This (witha:hover a) is more specific than the following rule, which was previously governing the text colour of the link, even when the witha:hover rule was applied

    a:link {
    color: black;
}

As an aside, the use of !important in CSS rules should be restricted to when there is no alternative. If possible, use a more specific selector instead.

Make sure you read the specificity information on the link in @Alex's answer.

Mark Chorley
In a word, I want the text in a div including the tag 'a' changed to red when the div is hovered. That's to say,if I hover on the div,but the mouse not put on the link, the link should change to red also.I am reading the the link in @Alex's answer.:)
hguser
That is what the first rule in this answer does (at least in FF and Safari). It should replace the existing a:hover rule. Does it not work for you?
Mark Chorley
Yes, it does not work. And I have tried @red-X's full code, not work also.
hguser
I dont know what your doing differently but it works here http://www.jsfiddle.net/pKucU/
red-X
I am not sure why? But it does not work. This is the screen when I opent the firebug to see the css,it seems that the hover attribute of the div has no effect,since I can not see it in the css monitor:http://i.imgur.com/YDvBJ.jpg However it works in the chrome. :(
hguser
your currently viewing the css for the withouta div ;)
red-X
A: 

the trick is to select an a within a hovered .witha like this JSFiddle

.witha:hover a {
    color: red;
}

or in full:

<html>
<head>
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.witha,.withouta {
    height: 40px;
    border: solid 1px black;
    padding: 5px;
}
.witha:hover,.withouta:hover {
    color: red ! important;
}
a:link {
    color: black;
}
a:visited {

}

.witha:hover a {
    color: red;
}
a:active {

}
</style>
</head>
<body>
<div class="witha"><a href="">Test color without tag A</a><br/>This is not a link, it works fine(change to red when the div.witha is hovered).But the link above should change to red also.</div>
<div class="withouta">Test color with tag A</div>
</body>
</html>
red-X