How to animate an element on hover

Go To StackoverFlow.com

9

How can I make my <div> elements grow (and the content changes text size to a higher one), when hovered over? I put them in a class and tried:

size: 150%;

and

height: +30px;
width: +30px;

the first try didn't work at all, and the second code just made the div's flash and dissappear partially.

2012-04-04 19:23
by Fr0stY
It can only be done using in CSS using CSS3 that is not supported in older browsers such as everything before IE9, FF4 Safari 4 and Chrome 15. Is that what you are looking for - laymanje 2012-04-04 19:29
@laymanje well i'm only triing to increase the size of one div, while hovered, it doesn't matter if or if not this div overlaps another on my site: http://maxwf.net16.net/index/index.html (that you know what i mean by div - Fr0stY 2012-04-04 19:41


5

CSS3 solution:

div {
    background: #999;
    width: 200px;
    height: 20px; 
    transition: width 1s;
}

div:hover{
    width: 300px;
}
<div>
    <p>Im content</p>
</div>

http://jsfiddle.net/MrdvW/

2012-04-04 19:52
by laymanje
Thx this realy help - Fr0stY 2012-04-04 20:40
No Problem. Would you mind marking it as correct - laymanje 2012-04-05 00:04
how do i do that - Fr0stY 2012-04-05 06:02
k, i thing i just did. : - Fr0stY 2012-04-05 06:02


4

Using CSS you can add a hover style to the div:

div.container {
    width: 80%;
    background-color: blue;
}

div.container:hover {
    width: 100%;
    background-color: red;
}

See this jsFiddle for a demonstration.

jQuery Solution

Another option that might work for you is jQuery. It's a JavaScript library that simplifies commonly needed functionality such as this. Using jQuery, you can easily add hover effects to the elements:

//hover effect applies to any elements using the 'container' class
$(".container").hover(
    function(){ //mouse over
        $(this).width($(this).width() + 30);
    },
    function(){ //mouse out
        $(this).width($(this).width() - 30);
    }
);

See this jsFiddle for a demonstration.

2012-04-04 19:27
by James Johnson
thx, but there's one prob. with that, i can't define the size in %,because i have 3+ divs next to each other, like this:http://jsfiddle.net/epS96/ (js fiddle doesn't show the css properly) and i want each div to be able to grow... - Fr0stY 2012-04-04 19:35
btw, this is the site : http://maxwf.net16.net/index/index.htm - Fr0stY 2012-04-04 19:37
@Fr0stY, you can use any unit for setting the widths, try fiddling with his example - bfavaretto 2012-04-04 19:39
ok thx. is there something to tell it to be the existing width plus 30 px or so like width: width+30px; height: height+30px - Fr0stY 2012-04-04 19:47
Just hard code it :) if you're only changing between two sizes, a normal size and a hover size, I'd recommend using specific numbers to keep from accidentally getting strange result - Andrew 2012-04-04 19:54
ok. i guess thats the only thing to do :( but :) thx for all help :D: - Fr0stY 2012-04-04 20:02
The other alternative is to use something like jQuery to handle the resizing, or possibly a combination of both. With jQuery you would be able to expand the elements by a fixed number of pixels - James Johnson 2012-04-04 20:25
what is jquery and how do you use it is it like a script or css - Fr0stY 2012-04-04 20:31
jQuery is a JavaScript library that simplifies commonly needed functionality like what you need. I'll edit my answer and post an example - James Johnson 2012-04-04 20:56
@Fr0stY: I updated my answer with a jQuery example and Fiddle. There's also a link to the download the latest version - James Johnson 2012-04-04 21:07
what do you think of this now? http://maxwf.net16.net/index/index.html (i'm using chrome, because it displays the css very good - Fr0stY 2012-04-04 21:37
Looks pretty good to me... seems to achieve the effect you were looking for - James Johnson 2012-04-04 21:41
of course the colors etc are just for displaying purposes, and i'm not trying to copy the Metro design, but just the "fields - Fr0stY 2012-04-04 21:48


4

I did something like this for a similar problem (you can change the scale to whatever works for you):

div:hover {
 -webkit-transform: scale(1.1);
 -moz-transform:    scale(1.1);
 -o-transform:      scale(1.1);
 -ms-transform:     scale(1.1);
}

Note that this will scale both the div and its content, which I think is what you want.

2012-04-04 21:16
by tsherif
thk very much. yes it is what i was looking for xD, but ive used the answer below to do it and it looks pretty good already - Fr0stY 2012-04-04 21:32
Ads