CSS - Gradient transition on hover does work both ways - html

On hover I want to fade in a gradient. I works fine, but on mouse leave there is no transition back. What is wrong, how can I improve the code?
jsfiddle
HTML
<header class="parent">
Hover here!
<div class="child"></div>
</header>
CSS
.child {
height: 100px;
position: absolute;
left: 0;
top: 0;
width: 100%;
z-index: -1;
opacity: 0;
-webkit-transition: all 1s ease-out;
-moz-transition: all 1s ease-out;
-o-transition: all 1s ease-out;
/* Opera */
transition: all 1s ease-out;
/* Standard */ }
.parent {
height: 120px;
position: absolute;
left: 0;
top: 0;
width: 100%;
z-index: 100;
opacity: 1 !important; }
.parent:hover .child {
opacity: 1;
background: -moz-linear-gradient(top, #ededed 0%, #ededed 30%, rgba(237, 237, 237, 0) 99%);
/* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ededed), color- stop(30%, #ededed), color-stop(99%, rgba(237, 237, 237, 0)));

Simply move your background out of the :hover state, onto the .child directly- seeing as you are only animating the opacity of .child this is all that is required. The reason it isnt working at present is also due to the fact you are only listing a single background state, that for hover- when you arent hovering the parent, the background is immediately removed (as no default is present) although the opacity is transitioning- so you see the 'jump'
Demo Fiddle

You have added the bg-gradient to child on hovering only.
Move the background css property to child{ /* background property */ } at default state(mouse leave)
It works good here http://jsfiddle.net/nvishnu/4paLf352/16/

Related

How to animate linear-gradient from top right to bottom left?

In my code, on hover the direction of linear-gradient animation is like counterclockwise, how can do it stright linear from top right to bottom left?
.boxstyle
{
background-color:rgba(0,69,255,1);
background-size: 0% 100%;
transition: background-color .5s;
width: 150px;
height: 150px;
}
.boxstyle:hover
{
background-image:linear-gradient(to left bottom, rgba(189,41,242,0) 0%, rgba(189,41,242,0) 40%, rgba(189,41,242, 0.9) 100%);
background-repeat:no-repeat;
background-size: 200% 100%;
transition:background-size 1s, background-color 1s;
}
<div class="boxstyle">hover it</div>
If you really want the pink to start at the top right and move to the bottom left you could use before and after pseudo elements, the before with just the color, the after also with the linear-gradient.
The after pseudo element moves diagonally across to the bottom left on hover of the main element.
.boxstyle {
width: 150px;
height: 150px;
position: relative;
overflow: hidden;
}
.boxstyle::before,
.boxstyle::after {
content: '';
position: absolute;
z-index: -1;
}
.boxstyle::before {
background-color: rgba(0, 69, 255, 1);
width: 100%;
height: 100%;
}
.boxstyle::after {
background-image: linear-gradient(to left bottom, rgba(189, 41, 242, 0) 0%, rgba(189, 41, 242, 0) 40%, rgba(189, 41, 242, 0.9) 100%);
bottom: 100%;
left: 100%;
transition: all 1s linear;
width: 200%;
height: 200%;
}
.boxstyle:hover::after {
bottom: -10%;
left: -10%;
}
<div class="boxstyle">hover it</div>
Old comment: This fits your verbal description of the effect desired, but I suspect it isn't exactly what you intended - did you want it more 'blendy' and subtle, becoming the final picture only at the very end?
UPDATE: Since seeing your images this snippet uses an after that has twice the dimensions of the main element so spreading out the colors more. It 'settles' on a hover a bit beyond the bottom left.
You can try like below. Invert the positions to get the opposite direction. More details: Using percentage values with background-position on a linear-gradient
.boxstyle {
background: linear-gradient(to top right, #0000 40%, rgb(189 41 242/0.9) 100%);
background-size: 200% 200%;
background-position: 0% 100%;
background-color: rgba(0, 69, 255, 1);
transition: .5s;
width: 150px;
height: 150px;
}
.boxstyle:hover {
background-position: 100% 0;
}
<div class="boxstyle">hover it</div>
If you prefer the pink color area to travel through the box (from top right corner to bottom left corner), it can be implemented by setting the linear-gradient so that has the pink area in the middle.
Example:
.boxstyle {
background: linear-gradient(to top right, #0000 10%, rgb(189 41 242/0.9) 40%, rgb(189 41 242/0.9) 60%, #0000 90%);
background-size: 500% 500%;
background-position: 0% 100%;
background-color: rgba(0, 69, 255, 1);
transition: .5s ease-in-out;
width: 150px;
height: 150px;
color: #fff;
}
.boxstyle:hover {
background-position: 100% 0;
}
<div class="boxstyle">hover it</div>

My imageContainer div is slightly bigger height that the elements it contains which makes it look as if there's a space between top and bottom divs [duplicate]

This question already has answers here:
Why is there an unexplainable gap between these inline-block div elements? [duplicate]
(6 answers)
Margin on child element moves parent element
(18 answers)
Closed 4 years ago.
My imageContainer div is slightly bigger in height than the elements it contains which makes it look as if there's a space between top and bottom divs. Any idea what might cause this?
.imageContainer {
text-align: center;
position: relative;
width: 100%;
}
.stickyContainer {
display: inline-block;
position: relative;
z-index: 2;
}
.blackGradient:after {
content: '';
display: block;
position: absolute;
opacity: 1;
-webkit-transition: opacity 0.5s ease-in-out;
transition: opacity 0.5s ease-in-out;
top: 0;
height: 100%;
width: 100%;
background: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 0)), color-stop(53%, rgba(120, 120, 120, 0)), to(black));
background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 0%, rgba(120, 120, 120, 0) 53%, black 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#000000', GradientType=0);
overflow: hidden;
z-index: 1;
}
.blackGradient:hover:after {
opacity: 1;
}
<div class='imageContainer'>
<div class="stickyContainer blackGradient">
<h1 class='imageTitle'></h1>
<img class='uploadedImg' src='' alt='Random image' />
<a class='specialA' href=''></a>
</div>
</div>

Hidden (opacity 0) image triggers link on hover (not wanted)

Please see: https://codepen.io/alanvkarlik/pen/BYzYoY
<div class="hover_img">
<a class="hover_link" href="x">
Title
<span>
<img src="image.jpg" width="100%"/>
</span>
</a>
</div>
and css:
.hover_img span {
z-index:-1;
opacity: 0;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transition: opacity 300ms ease-in-out;
transition: opacity 400ms ease-in-out;
}
.hover_img a:hover span {
display: inline-block;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 1;
}
basically if you go to my site, and hover below last link "Do Graphic Designers Need To Be Human?" the image will still show up because it's "hidden" underneath, moving mouse even lower triggers another image/link
I'm not sure if I can simplify the code somehow to make it work? All I want is for the image to show up when the mouse is hovering the link ONLY, not the area around it
Adding just display: none to .hover_img span should helps a bit:
.hover_img span {
display: none;
z-index:-1;
opacity: 0;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transition: opacity 300ms ease-in-out;
transition: opacity 400ms ease-in-out;
}
I tried your code, I was able to do it but animation is gone now. Hopefully you will be able to resolve it.
/* PROJECTS LINKS + BACKGROUND */
.hover_link > span { display: none; }
}
.hover_link {
transition: 0.3s;
text-decoration: none;
padding: 0 3px;
background-color: #5544ee;
color: #000;
padding: 0px 3px;
text-decoration: none;
background: -moz-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(85,68,238,0) 20%,rgba(85,68,238,1) 21%,rgba(85,68,238,1) 73%,rgba(85,68,238,0) 74%,rgba(255,255,255,0) 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(85,68,238,0) 20%,rgba(85,68,238,1) 21%,rgba(85,68,238,1) 72%,rgba(85,68,238,0) 72%,rgba(255,255,255,0) 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, rgba(255,255,255,0) 0%,rgba(85,68,238,0) 20%,rgba(85,68,238,1) 21%,rgba(85,68,238,1) 73%,rgba(85,68,238,0) 74%,rgba(255,255,255,0) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#00ffffff',GradientType=0 ); /* IE6-9 */
}

Masking a circle and moving the mask with animation

I have a circular div that is vertically and horizontally centered in div. I am trying to achieve a css animation, that it seems like it's fading in from top to bottom.
I thought of making height 0 initially and moving to 50px, however as it is centered, it starts to getting created from the center. Instead, I want to it to get positioned to the initial position and it gets created from top to bottom. Just like there is a square only masking the circle and nothing else, and it moves to down.
#circle {
width: 80px;
height: 80px;
border-radius: 50px;
}
How can I add this a square mask to achieve the below effect?
Please note that background has a gradient, so I can't put a square and assign it a color directly, thus I thought I need to mask them.
How to achieve this effect?
What I have tried:
#keyframes example {
from {height: 0}
to {height: 80px}
}
As the circle is centered, it starts to expand from the middle. This is not what I want. That's why I thought of the mask
Edited answer:
I am able to achieve this with a combination of a image background and background-position animation.
This will not work if you set the background as a CSS color like #fff. it needs to be an image or a gradient. You also need to set background-repeat to no-repeat
The animation simply starts with the background out of the div display area then pulls the the background downward into the display area.
Kindly check the examples in full-screen.
Working snippet (jpeg image as object background):
body {
background: linear-gradient(135deg, rgba(244, 226, 156, 0) 0%, rgba(59, 41, 58, 1) 100%), linear-gradient(to right, rgba(244, 226, 156, 1) 0%, rgba(130, 96, 87, 1) 100%);
margin: 0 auto;
height: 120vh;
overflow: hidden;
}
.sun {
height: 400px;
width: 400px;
border-radius: 100vw;
margin: 5em auto;
animation-name: sunrise;
animation-duration: 10s;
animation-delay: .5s;
animation-timing-function: linear;
animation-iteration-count: 1;
background: url(https://image.ibb.co/eVdQ3Q/white.jpg);
background-repeat: no-repeat;
animation-fill-mode: forwards;
opacity: 0;
}
#keyframes sunrise {
from {
opacity: 1;
background-position: 0 -700px;
}
to {
opacity: 1;
background-position: 0 0px;
}
}
<div class="sun"></div>
Working snippet (gradient as object background):
body {
background: linear-gradient(135deg, rgba(244, 226, 156, 0) 0%, rgba(59, 41, 58, 1) 100%), linear-gradient(to right, rgba(244, 226, 156, 1) 0%, rgba(130, 96, 87, 1) 100%);
margin: 0 auto;
height: 120vh;
overflow: hidden;
}
.sun {
height: 400px;
width: 400px;
border-radius: 100vw;
margin: 5em auto;
animation-name: sunrise;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: 1;
background: linear-gradient(white,white);
background-repeat: no-repeat;
animation-fill-mode: forwards;
opacity: 0;
}
#keyframes sunrise {
from {
opacity: 1;
background-position: 0 -700px;
}
to {
opacity: 1;
background-position: 0 0px;
}
}
<div class="sun"></div>

Transition doesn't work with border-image and gradient

I am using border-image with gradient and it works fine, but it seems transition is not supported for it.
Is it possible to achieve transition on hover for this example?
JsFiddle
div {
border:10px solid blue;
height:120px;
float:left;
transition:1s all;
border-image: linear-gradient(to bottom, white, blue) 1 100%;
}
div:hover {
border-image: linear-gradient(to bottom, skyblue, blue) 1 100%;
}
<div></div>
As the others already told you, it isn't possible to transition a gradient (yet). The best way to fake the effect would be to work with opacity, which can be transitioned. You don't need to add any elements however, the :before and :after pseudo elements will do just fine. have a look at the following css:
div {
height:120px;
width:10px;
padding: 0 10px;
background: salmon;
background-clip: content-box;
position: relative;
}
div:after, div:before {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
content:'';
}
div:after {
background: linear-gradient(to bottom, white 0%, blue 100%);
z-index: -1;
transition: opacity 1s;
}
div:before {
background: linear-gradient(to bottom, skyblue 0%, blue 100%);
z-index: -2;
}
div:hover:after {
opacity: 0;
}
And an example: https://jsfiddle.net/et0ffrqx/2/
Not Possible
That isn't possible yet because linear-gradient is calculated as an image, not actually colors.
Solution
Try putting the <div> within another <div> which can act as a border. Then the outer <div> can have an animated background
I've found this codepen demonstrating how this can be done with JavaScript.
My best bet for you would be to have two <div> stacked on top of each other. The bottom <div> would be the target gradient and the top being the start. Then just fade the top <div>
#start {
position:absolute;
width: 100px;
height: 100px;
z-index: 1;
opacity: 1;
background: linear-gradient(red,blue);
transition: opacity 0.5s ease;
}
#end {
position:absolute;
width: 100px;
height: 100px;
background: linear-gradient(green,orange);
z-index: -1;
}
#start:hover {
opacity: 0;
}
<div id="start">Start</div>
<div id="end">End</div>
The snippet demonstrates a simple way to fade between gradients. Not perfect but smoother and without JavaScript. Put your other stuff in side the <div> and adjust the width and height to your needs.
Also try using :before and :after to avoid having duplicate divs
No
Animations aren't supported for those properties.
You can however, think of another way to accomplish this visually.
maybe you have 2 wrappers around something, and they are 2 different gradients, and there is padding around them to simulate the look of a border... and then the elements with the gradients have opacity that fades to and from on hover.
https://jsfiddle.net/sheriffderek/5uoypaoo/
<div class="gradient-1">
<div class="gradient-2"></div>
<div class="thing"></div>
</div>
.thing {
position: relative;
width: 200px;
height: 200px;
background: white;
float: left;
}
.gradient-1 {
position: relative;
background: linear-gradient(45deg, pink, blue);
opacity: 1;
padding: 1rem;
float: left;
}
.gradient-1:hover .gradient-2 {
opacity: 1;
}
.gradient-2 {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: linear-gradient(45deg, lightgreen, orange);
opacity: 0;
transition: opacity 1s ease-in-out;
}