如何在<a> element change color on hover?

时间:2016-02-04 18:31:23

标签: javascript jquery html css twitter-bootstrap

My code is:

<div class="something">
    <a class="navbar-brand" href="#">
        <h1>hello</h1>
        <h2>world</h2>
        <h3>again!</h3><br/>
        <p>blah blah blah</p>
    </a>
</div>

Basically I need all the <h> elements and the <p> element to change color at the same time on hover of the <a> tag.

I've tried:

.navbar-brand *:hover{
     color:red;   
}

but this only changes color on hover of each individual element, not all of them at the same time.

I should also mention because it might be crucial, that the navbar-brand class is a bootstrap class.

3 个答案:

答案 0 :(得分:1)

这是正确的CSS:

.navbar-brand:hover {
   color:red;   
}

小提琴:https://jsfiddle.net/t4o0uffm/1/

答案 1 :(得分:0)

直接定位a.navbar-brand:hover,而不是定位子元素

答案 2 :(得分:0)

您可以像以下代码段那样执行此操作。

.navbar-brand:hover h1, 
.navbar-brand:hover h2, 
.navbar-brand:hover h3, 
.navbar-brand:hover h4, 
.navbar-brand:hover h5 {
    color: red;
}
<div class="something">
    <a class="navbar-brand" href="#">
        <h1>hello</h1>
        <h2>world</h2>
        <h3>again!</h3><br/>
        <p>blah blah blah</p>
    </a>
</div>