Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MouseOver and MouseOut In CSS

Tags:

html

css

For using mouse into one element we use the :hover CSS attribute. How about for mouse out of the element?

I added a transition effect on the element to change the color. The hover effect works fine, but what CSS attribute should I use for mouse out to apply the effect? I'm looking for a CSS solution, not a JavaScript or JQuery solution.

like image 360
Moslem7026 Avatar asked Aug 28 '13 16:08

Moslem7026


People also ask

What is mouseover and Mouseout?

The mouseover event occurs when a mouse pointer comes over an element, and mouseout – when it leaves. These events are special, because they have property relatedTarget . This property complements target . When a mouse leaves one element for another, one of them becomes target , and the other one – relatedTarget .

How do I use Onmouseout in CSS?

CSS itself does not support a mousein or mouseout selector. The :hover selector will apply to the element while the mouse is over it, adding the style when the mouse enters and removing the style when the mouse leaves.

What is mouseover CSS?

Definition and UsageThe :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links.


2 Answers

Here is the best solution, i think.

CSS onomouseout:

div:not( :hover ){ ... }

CSS onmouseover:

div:hover{ ... }

It's better, because if you need to set some styles ONLY onmouseout and trying to do this in this way

div { ... }

you will set your styles and for onmouseover too.

like image 69
dr.anon Avatar answered Oct 06 '22 02:10

dr.anon


CSS itself does not support a mousein or mouseout selector.

The :hover selector will apply to the element while the mouse is over it, adding the style when the mouse enters and removing the style when the mouse leaves.

The nearest approach is to define the styles which you would place in mouseout within your default (non-hover) styles. When you mouse-over the element the styles within hover will take effect, emulating a mousein, and when you move your mouse off the element the default styles will take effect again, emulating mouseout.

Here is an example, taken from here:

div {
    background: #2e9ec7;
    color: #fff;
    margin: 0 auto;
    padding: 100px 0; 
    -webkit-transition: -webkit-border-radius 0.5s ease-in;
    -moz-transition: -moz-border-radius 0.5s ease-in;
    -o-transition: border-radius 0.5s ease-in;
    -ms-transition: border-radius 0.5s ease-in;
    transition: border-radius 0.5s ease-in;
    text-align: center;
    width: 200px;
}


div:hover {
    background: #2fa832;
    -webkit-border-radius: 100px;
    -moz-border-radius: 100px;
    border-radius: 100px;
    -webkit-transition: all 1s ease;
    -moz-transition: all 1s ease;
    -o-transition: all 1s ease;
    -ms-transition: all 1s ease;
    transition: all 1s ease;
    -webkit-transform: rotate(720deg);
    -moz-transform: rotate(720deg);
    -o-transform: rotate(720deg);
    -ms-transform: rotate(720deg);
    transform: rotate(720deg);
}

The transitions defined for the div:hover style will take effect when the mouse enters (and hover is applied). The transitions for the div style will take effect when the mouse leaves (and hover is removed). This results in the mousein and mouseout transitions being different.

like image 37
STW Avatar answered Oct 06 '22 01:10

STW