CSS Transition on Hover - html

I have a problem. I actually try to do a transition at a div when I hover the mouse over an object. So basically I have a div, and when I hover my mouse the div, it should display another div at the top of it, however it should be transitioned, so the hover effect would be smoother.
How is it possible, if I have these two divs?
<div id="first">
<div id="on-hover">
<img src="example-image.png" />
</div>
</div>

You use CSS3 transition:
#on-hover {
opacity:0;
/* Firefox */
-moz-transition-property: opacity;
-moz-transition-duration: 2s;
-moz-transition-delay: 1s;
/* WebKit */
-webkit-transition-property: opacity;
-webkit-transition-duration: 2s;
-webkit-transition-delay: 1s;
/* Opera */
-o-transition-property: opacity;
-o-transition-duration: 2s;
-o-transition-delay: 1s;
/* Standard */
transition-property: opacity;
transition-duration: 2s;
transition-delay: 1s;
}
#on-hover:hover {
opacity:1;
}
The complete thing working: http://jsfiddle.net/djwave28/CuNkZ/2/
More information can be read here: http://robertnyman.com/2010/04/27/using-css3-transitions-to-create-rich-effects/
I do need to explicitly mention that this may not work in IE9 and below. IE10 seems to work according top the docs. If needed, you can simulate the effect with javascript, but the question was CSS.

#on-hover {
opacity: 0;
transition: opacity 0.5s;
-webkit-transition: opacity 0.5s;
}
#first:hover #on-hover {
opacity: 1;
}
This is only about the animation. A more detailed example here: http://jsfiddle.net/ELa6X/

i quickly mocked up something that could do it with css3 transitions demo jsfiddle
#on-hover {
transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-webkit-transition: opacity .25s ease-in-out;
opacity:0;
}

By smoother do you mean fade in? Here is an example of fading in on #on-hover. I use pointer-events:none to ignore hover on <img> and pass the event on to its parent #on-hover.
jsFiddle
#first,
#on-hover {
width:100px;
height:100px;
}
#first {
background-color:#F00;
}
#on-hover {
opacity:0;
background-color:#0F0;
-webkit-transition:opacity .5s ease;
-moz-transition:opacity .5s ease;
transition:opacity .5s ease;
}
#on-hover:hover {
opacity:1;
}
#on-hover img {
pointer-events:none;
}

Use the pseudo selector :hover.
#first:hover > #on-hover{
opacity:1;
}
#on-hover{
opacity:0;
-webkit-transition: opacity 0.5s;
}
#first{
width:300px;
height:200px;
}
http://jsfiddle.net/charlescarver/V8PK3/1/

I'd suggest using jQuery and animate(). You could do with CSS but then you'd have problems with older browsers.

Related

CSS3 : Fade in on hover and Fade out when hover is gone

I have a div. When I hover on it , it should be appeared using fadeIn effect and when I remove mouse then it should gone using fadeOut effect.
I have tried the below code.
.main_div{
width:100px;
height:100px;
border:thin black solid;
position:relative;
}
.main_div .overlay_div{
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
width:100%;
height:100%;
display:none;
background:rgba(0,0,0,0.5);
opacity: 0;
transition: opacity;
transition-timing-function: ease-out;
transition-duration: 100ms;
transition-delay: 1s;
-ms-transition: opacity;
-ms-transition-timing-function: ease-out;
-ms-transition-duration: 100ms;
-ms-transition-delay: 1s;
-moz-transition: opacity;
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 100ms;
-moz-transition-delay: 1s;
-webkit-transition: opacity;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 100ms;
-webkit-transition-delay: 1s;
}
.main_div:hover .overlay_div{
display:block;
opacity: 1;
/* Fade in */
transition: opacity;
transition-timing-function: ease-out;
transition-duration: 100ms;
-ms-transition:opacity;
-ms-transition-timing-function: ease-out;
-ms-transition-duration: 100ms;
-moz-transition:opacity;
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 100ms;
-webkit-transition: opacity;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 100ms;
}
<div class="main_div">
<div class="overlay_div">
some text
</div>
</div>
Any help would be great.
Thank You.
CSS Transitions don't work on display:block/none; You can manage to give that effect using visibility/opacity properties.
Check this example, You can manually add the transition-delay property if you really want it to add.
.main_div {
width: 100px;
height: 100px;
border: thin black solid;
position: relative;
}
.main_div .overlay_div {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
visibility: hidden;
opacity: 0;
transition: all 1s ease-out;
}
.main_div:hover .overlay_div {
visibility: visible;
opacity: 1;
transition: all 1s ease-in;
}
<div class="main_div">
<div class="overlay_div">
some text
</div>
</div>
Code work fine just change display from none to block, as transition doesn't works when changing display.
.main_div .overlay_div{
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
width:100%;
height:100%;
display:block;/*Change this*/
......
......
}

Making text show up on hover and blur effect blinking fix

/* CSS used here will be applied after bootstrap.css */
body{
background-color:yellow;
}
img{
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease;
}
img:hover{
-webkit-filter:blur(5px);
}
<img src="http://i.stack.imgur.com/K0jNI.png">
When you hover over the image the borders of the image flash for a bit before settling.. Is there a way to fix that?
And how do i make a text show up on the middle of the image when i hover over it?
EDIT: This now looks great in Chrome
I don't think it's entirely possible to get a super clean transition when using webkit blur. I've had a lot of rendering issues and glitches when using it before. It's a resource hog too when used on a lot of elements. My advice to change your easing to linear and target only the blur. That should tighten it up a little bit.
img{
-webkit-transition: -webkit-filter 0.5s linear;
-moz-transition: -webkit-filter 0.5s linear;
-o-transition: -webkit-filter 0.5s linear;
-ms-transition: -webkit-filter 0.5s linear;
transition: -webkit-filter 0.5s linear;
}
As for the text fade in. You'll need to add in an element that is initially opacity:0; but then changed to opacity:1; when the parent block is hovered. Initial HTML changed to this:
<div class='block'>
<img src="https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4">
<span>Hey there</span>
</div>
And the new CSS
/* CSS used here will be applied after bootstrap.css */
body {
background-color: yellow;
}
img {
-webkit-transition: -webkit-filter 0.5s linear;
transition: -webkit-filter 0.5s linear;
}
.block {
position: relative;
width: 400px;
}
.block img {
width: 100%;
}
.block span {
opacity: 0;
-webkit-transition: all .3s;
transition: all .3s;
color: white;
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
text-align: center;
left: 0;
right: 0;
}
.block:hover > span {
opacity: 1;
}
img:hover {
-webkit-filter: blur(4px);
}
Example here
http://codepen.io/jcoulterdesign/pen/58d613e80e4a768cc9e54aa1e7aaa0af

Link over image hover

I have 2 images that are changing when I hover on the first one.
On the second one, I have some text because I want a link there.
My problem is now that I want the hover image to remain when I hover over the link.
Here is my code:
#blur {
border: 1px solid #bebfc1;
position:relative;
height:450px;
width:300px;
margin:0 auto;
-webkit-transition: border 1s ease-in-out;
-moz-transition: border 1s ease-in-out;
-o-transition: border 1s ease-in-out;
transition: border 1s ease-in-out;
}
#blur img {
position:absolute;
left:0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#blur:hover {
z-index:2;
border: 1px solid #000000;
}
#blur img.top:hover {
opacity:0;
}
#blur .text {
position:absolute;
color:#bebfc1;
opacity:0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
font-family:"Segoe UI Light";
font-size:13px;
}
#blur:hover .text {
opacity:1;
}
<div id="blur">
<img class="bottom" src="http://s28.postimg.org/do5izc4gd/image.png" />
<img class="top" src="http://s28.postimg.org/a5tj2y3kd/image.png" />
<p class="text" style="bottom:6px; left:180px;">link</p>
</div>
When I hover over the link I want the red image to remain the same.
How can this be done?
Thanks !!!
DEMO here http://jsfiddle.net/VYR9q/
Okay, I fixed it in the fiddle line you provided :)
Here you are what I fixed:
#blur:hover .top {
opacity:0;
}
Instead of:
#blur img.top:hover {
opacity:0;
}
Edit from: #biziclop:
You can make it:
#blur:hover img.top
I think you should simply move :hover from the img to the common parent #blur
#blur img.top:hover {
to
#blur:hover img.top {
Live example: http://jsfiddle.net/VYR9q/2/
Change your css to :
#blur:hover img.top {
//your css
}

CSS - tooltip only with css and html doesn't work

I am trying to do simple tooltip only with css3 and html, but the transition doesn't work. What am I doing wrong?
HTML
<p>
This has tooltip
</p>
<div class="tooltip">Tooltip content</div>
CSS
p {
width: 200px;
background-color: aqua;
padding: 10px;
margin-top: 50px;
}
div.tooltip {
position: absolute;
width: auto;
padding: 10px;
background-color: rgba(0,0,0, 0.5);
top: 0px;
display: none;
opacity: 0.0;
transition: opacity 1s;
-webkit-transition: opacity 1s;
-o-transition: opacity 1s;
-moz-transition: opacity 1s;
}
p:hover + div.tooltip {
display: block;
opacity: 1.0;
transition: opacity 1s;
-webkit-transition: opacity 1s;
-o-transition: opacity 1s;
-moz-transition: opacity 1s;
}
http://jsfiddle.net/MCDg4/
Update / Alternate solution
For a modern browser CSS3 solution you could use pseudo elements..
<span data-tooltip="I am the tooltip">This has a tooltip</span>
and
[data-tooltip]{
position:relative;
}
[data-tooltip]:before{
content:attr(data-tooltip);
position:absolute;
bottom:110%;
padding:10px;
background:#666;
opacity:0;
color:white;
font-size:smaller;
-webkit-transition:opacity 1s ease;
-o-transition:opacity 1s ease;
transition:opacity 1s ease;
pointer-events:none;
}
[data-tooltip]:hover:before{
opacity:1;
}
Demo at http://jsfiddle.net/BJ2tr/
(this could be done without pseudo-elements by nesting the tooltip inside the elements that it refers to, and adjusting the css accordingly)
Unfortunately when you change display from none to something else, you cannot have transitions.
Instead of display:none you could just offset it outside of the window (with top:-9999px) and bring it to position when showing it.
div.tooltip {
position: absolute;
width: auto;
padding: 10px;
background-color: rgba(0,0,0, 0.5);
top: -999px; /*CHANGED THIS AND REMOVED display:none*/
display: none;
opacity: 0.0;
transition: opacity 1s;
-webkit-transition: opacity 1s;
-o-transition: opacity 1s;
-moz-transition: opacity 1s;
}
p:hover + div.tooltip {
opacity: 1.0;
top: 0px; /*ADDED THIS AND REMOVED display:block*/
transition: opacity 1s;
-webkit-transition: opacity 1s;
-o-transition: opacity 1s;
-moz-transition: opacity 1s;
}
This will, however, not fade out (only in) since it moves it away on mouseout (so it actually does fade but you do not see it because it is outside the viewport)..
Explanation
You put transition only on opacity, while when changing to display:block; it is shown as a block with opacity:1; by default.
Solution
(JSFiddle)
Delete the display:none; and display:block on your tooltip element.

Image Hover in Reverse using CSS

I have a css image hover effect, in which the image in the div turns to 0 opacity and then the background image is displayed. This means as your hover, one image fades out and the other appears.
This effect uses CSS and webkit.
But the issue is that when you hover over the image the effect takes place but not in reverse, meaning it does not fade in when you leave the image. But that is the effect I want.
This is the HTML markup...
<div id="info"><img src="infow1.png" width="800" height="800" /></div>
This is the CSS markup...
#info {
background: url(infow2.png) 0 0 no-repeat;
position: fixed;
top: 50%;
left: 50%;
margin-top: -400px;
margin-left: -400px;
width: 800px;
height: 800px;
z-index:100;
opacity: 1;
-webkit-transition: opacity;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 500ms;
}
#info img:hover{
opacity:0;
-webkit-transition: opacity;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 500ms;
}
So overall I want the infow.png to fade out, when hover over the image then fade back in when you leave.
Is this what you are looking for?
HTML:
<img src="infow1.png" class="fadeOut">
CSS3:
.fadeOut {
opacity: 1;
transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-webkit-transition: opacity .25s ease-in-out;
}
.fadeOut:hover {
opacity: 0;
}
JS Fiddle: http://jsfiddle.net/AXQW4/1/
EDIT: If you want to display a different image when hovering, you can add it as a background-image behind the original: http://jsfiddle.net/8qzcY/
You've created no rules that apply to the unhovered-image, so if you create some rules, as such:
#info img {
opacity: 1;
-webkit-transition: opacity;
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 500ms;
}
JS Fiddle demo.
It should work as you wish.
For contrast, the same demo without the unhovered #info img rules: JS Fiddle demo, which seems to match your description.