Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make entire div change color on hover using css?

Tags:

css

I have the following:

    <div id="side-menu" class="sidebar-nav span2">
        <div class="sidebar-link"><span>Link 1</span></div>
        <div class="sidebar-link"><span>Link 2</span></div>
    </div>

I'm trying to make each of the two divs change color when you hover over them - whether you hover over the text of off to the right or left of the text. Currently the color changes only if I hover over the text. Any idea how this can be done? Here's my fiddle with css:

http://jsfiddle.net/PTSkR/56/

like image 421
SB2055 Avatar asked May 19 '13 01:05

SB2055


People also ask

How do you make something change color when you hover over it CSS?

Changing link color on hover using CSS To change the color of your link on hover, use the :hover pseudo property on the link's class and give it a different color.

How do you make a hover effect a div?

You'll need a container div and at least one foreground div to cover the background (could be just an image). Then you'll want to target the parent on hover and change the foreground child. I used transform instead of animating a position property because it's more performant.


2 Answers

You have a space in the hover selector. This matters because the space is the descendant selector in CSS

div.sidebar-link :hover{
    background-color: #E3E3E3;
}

This means that only hovered descendants of .sidebar-link are affected by the rules. Remove the space.

http://jsfiddle.net/ExplosionPIlls/PTSkR/57/

like image 121
Explosion Pills Avatar answered Oct 11 '22 20:10

Explosion Pills


You can achieve this easily by this following code: Link

.cstDiv{
    padding:10px;  /* text-align:center may be used for a good look */
    width:55px;
}

div.cstDiv:hover{
    background-color:gray;    
    color:white;
    cursor:pointer;  /* you may delete this if you want */
}
   <div>
       <div class="cstDiv">Link I</div>
       <div class="cstDiv">Link II</div>
       <div class="cstDiv">Link III</div>
       <div class="cstDiv">Link IV</div>
   </div>
like image 27
eozten Avatar answered Oct 11 '22 18:10

eozten