Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight div box on hover

Say I have a div with the following attributes:

.box {
  width: 300px;
  height: 67px;
  margin-bottom: 15px;
}

How would I make it so that if someone hovers their mouse over this area, it changes the background to a slightly darker colour and makes it a clickable area?

like image 673
ShadyPotato Avatar asked Mar 25 '13 20:03

ShadyPotato


People also ask

How do you highlight a div in mouse over?

The :hover selector is used to select elements when you mouse over them.

How do you highlight a box in CSS?

How Do I Highlight Text In CSS? To Highlight text in HTML you have to use an inline element such as the <span> element and apply a specific background style on it. This will create the highlighting effect, which you can tweak in many different ways to create different looks.

Can we use hover with Div?

You can apply :hover styles to any renderable element on a page. IE6 only supports that pseudo-class on links though.

How do I highlight a selected DIV?

Here you could use highlight . . highlight { background: blue; } makes more sense than . red { background: blue; } .


3 Answers

CSS Only:

.box:hover{
background: blue; /* make this whatever you want */
}

To make it a 'clickable' area, you're going to want to put a <a></a> tag inside the div, and you may want to use jQuery to set the href attribute.

jQuery Solution

$('.box').hover(function(){
$(this).css("background", "blue");
$(this).find("a").attr("href", "www.google.com");
});

A third solution: You could change the cursor, and also give it a click event using jQuery:

$('.box').click(function(){
// do stuff
});

Use the above along with the following CSS:

.box{
background: blue;
cursor: pointer;
}
like image 158
What have you tried Avatar answered Oct 21 '22 04:10

What have you tried


.box:hover {
    background: #999;
    cursor: pointer;
}

When you hover the background changes to the color you want and cursor becomes pointer. You can trigger an event with jQuery like so:

$('.box').click(customFunction);
like image 35
kapantzak Avatar answered Oct 21 '22 03:10

kapantzak


Linking a div has worked for me simply by wrapping it in an a tag. Here is an example below with Bootstrap classes:

<a href="#">
<div class="col-md-4">

<span class="glyphicon glyphicon-send headericon"></span>

<p class="headerlabel">SEND</p>
<p class="headerdescription">Send candidate survey</p>    

</div> 
</a>

To change your div colour on hover add:

div:hover {
background-color: rgba(255,255,255,0.5);
}

to your CSS :)

like image 20
ariebear Avatar answered Oct 21 '22 03:10

ariebear