I have added transition to my buttons and it works fine
I have a div with width/height defined inline via props (react), the transition code looks very similar to the button so I'm unsure why it isn't wokring. For this example I just want a simple transition from a white background to red when you hover.
I have set the background color to white initially and :hover to red:
background-color: red;
transition: background-color 0.5s;
-webkit-transition: background-color 0.5s;
-o-transition: background-color 0.5s;
-moz-transition: background-color 0.5s;
However this results in the expected result.. without any transition
I have tried changing the width on hover and setting tranisition: all 1s but it works without any transition again.. Why is this happening?
Thanks
Thanks for the fast responses. My apologies for the missing details. Turns out it was because the AOS (animation on scroll) library causes issues if doing transitions on the same element. I solved this by wrapping the div in another div.
It would help if you showed more of what you have on the hover, but I'm assuming that maybe you have the code set something like this:
.box{
background-color: red;
}
.box:hover{
transition: background-color 0.5s;
-webkit-transition: background-color 0.5s;
-o-transition: background-color 0.5s;
-moz-transition: background-color 0.5s;
}
It should be like this:
.box{
transition: background-color 0.5s;
-webkit-transition: background-color 0.5s;
-o-transition: background-color 0.5s;
-moz-transition: background-color 0.5s;
}
.box:hover{
background-color: red;
}
Usually, the transition is given to the element.
Try this instead -
Css
div {
background-color: white;
transition: background-color 0.5s ease-in-out;
}
div:hover {
background-color: red;
}
Sass -
div {
background-color: white;
transition: background-color 0.5s ease-in-out;
&:hover {
background-color: red;
}
}
Try this, if set initially white to div background. Default it is white.
so, we can add transition , then hover the background change to red
CSS
.box{
transition: background-color 0.5s;
}
.box:hover{
background-color: red;
}
SCSS
.box{
transition: background-color 0.5s;
&:hover{
background-color: red;
}
}
Try this snippet.
.box{
transition: background-color 0.5s;
}
.box:hover{
background-color: red;
}
<div class="box" style="width:100vh;height:100vh;"></div>
Related
I have an some images with hover effects, for example they change the bgcolor of the body.
I want a transition bewtween the colors, but i'm too dumb.
I'm trying to use this, but it still does not show a transition.
-webkit-transition: background-color 1000ms linear;
-ms-transition: background-color 1000ms linear;
transition: background-color 1000ms linear;
You're not dumb! Just need some practice. What you have is fine I just can't see the rest of your code. But this is what you're looking for:
div {
display: block;
width: 100px;
height: 100px;
background-color: red;
transition: background-color 1000ms linear;
}
div:hover {
background-color: green;
}
<div>
</div>
You can do it this way:
.x {
width: 300px;
height: 200px;
background-color: #fa0;
transition: 1000ms linear;
}
.x:hover {
background-color: #0af;
}
<div class="x"></div>
The important thing is to put the transition parameter into the rule for the element itself, and not to write the background-color into the transition value. The hover rule only defines the second color for the transition.
I'm trying to create an image hover effect, but I'm having 1 of two 2 issues:
The text AND the background are opaque (I only want the background to be opaque. I want the text to stay normal.)
The image will hover and transition correctly, but then if I hover over the text, the whole image goes back to the original version of the image.
I've tried several different methods, but I can't get past both of those issues at the same time. So what I want to accomplish is:
-Start with an opaque image and normal color text, and then upon hover, the image goes normal and the text stays the same, and all of the image stays normal while the cursor is over the image.
Here's my code:
.outer {
opacity: 0.25;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-o-transition: all .5s ease;
-ms-transition: all .5s ease;
transition: all .5s ease;
}
.outer:hover{
opacity: 1.0;
cursor: pointer;
}
h2.image-headings {
position: absolute;
top: 40%;
width: 100%;
font-size: 60px;
color: #fff;
}
<div class="outer">
<img src="http://wasatchhospitality.com/wp-content/uploads/2017/02/wasatch-elements.jpg">
<h2 class="image-headings">Management Team</h2>
</div>
This code shows the text and image starting off opaque. Thanks
If you just want to change the opacity of the image, change your rules to change/transition opacity on the image - not the parent div.
.outer img {
opacity: 0.25;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-o-transition: all .5s ease;
-ms-transition: all .5s ease;
transition: all .5s ease;
}
.outer {
cursor: pointer;
}
.outer:hover img {
opacity: 1.0;
}
h2.image-headings {
position: absolute;
top: 40%;
width: 100%;
font-size: 60px;
color: #fff;
}
<div class="outer">
<img src="http://wasatchhospitality.com/wp-content/uploads/2017/02/wasatch-elements.jpg">
<h2 class="image-headings">Management Team</h2>
</div>
I recommend you to use a JQuery plugin to do this because they are pretty good animated and its easy to
From my understanding of CSS3 transitions you have to specify the transition only in the base element and not in the :hover element, e.g. as described in the Mozilla documentation. This should lead to a transition when the new properties from :hover are applied and reverse the transition as soon as you don't hover anymore. (Fiddle below code)
#test{
position:absolute;
height: 100px;
width: 100px;
background-color: #A8A8A8;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#test:hover{
border-bottom: 10px solid black;
}
But this leads to fading-in only. When you stop hovering the border is instantly removed.
http://jsfiddle.net/hcsamkjf/1/
Any ideas?
You also need to specify the "initial state", otherwise the browser doesn't know what to animate. It can sometimes guess, which would be why you see it half-working (but it doesn't transition at all for me).
Add border-bottom:10px solid transparent or border-bottom:0 solid transparent to your #test styles, depending on the exact effect you want.
The issue is that as you are not specifying a border-style in the initial state, when you "unhover", the animation changes from solid to the default value : none. Border-style isn't animatable so the change is sudden.
You need to specify the same border-style in the initial state (note: specify a 0 border-width too to remove default border width) so the animation only affects border-width and stays smooth in both directions.
DEMO
CSS :
#test{
position:absolute;
height: 100px;
width: 100px;
background-color: #A8A8A8;
border-style: solid;
border-width:0px;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#test:hover{
border-bottom: 10px solid black;
}
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.
On the image hover, I want to display a different small image on it with a background and opacity so that the original image is still visible. I want to use the transition to slow the hover effect.
So here's what I'm trying and not working:
HTML:
<img src="http://i50.tinypic.com/2poy3kl.jpg" class="test" />
CSS:
.test {
background: red url("http://i50.tinypic.com/10mue4k.png");
transition: background .25s ease-in-out;
-moz-transition: background .25s ease-in-out;
-webkit-transition: background .25s ease-in-out;
}
.test:hover {
opacity: 0.5;
}
JS Fiddle:
http://jsfiddle.net/AXQW4/2/
Can someone please help me with this?
You'll need an additional wrapper for this. Exactly the size of the image, and give that the background. Then, when you hover over the image, you hide it and the underlying background is revealed.
Live example
HTML
<div class="background">
<img src="http://i50.tinypic.com/2poy3kl.jpg">
</div>
CSS
* {
padding: 0;
margin: 0;
-webkit-box-sizing: border-box;
}
.background {
background: url("http://i50.tinypic.com/10mue4k.png");
display: inline-block;
font-size: 0;
}
.background img {
-webkit-transition: all .5s;
}
.background img:hover {
opacity: 0.5;
}
No need to know the exact dimensions of the image.
Is this something you were looking for: http://jsfiddle.net/AXQW4/3/
One more demo :)
http://jsfiddle.net/AXQW4/7/
Is this what you want Demo
Or this LINK
.test {
background: red url("http://i50.tinypic.com/10mue4k.png");
-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;
}
.test:hover {
opacity:.5;
}