transition effect not working on hover with linear-gradient to bottom - css

I have two boxes, each have :hover selector which transition linear-gradient background image.
linear gradient to top works perfectly fine with transition (code below).
.box7:hover{
color: #FFF;
background-image: linear-gradient(to top, #000 50%, #fff 50%);
background-position: 0% 100%;
background-size: 100% 200%;
transition: all 1s ease;
border: none;
}
but I am having problems with linear gradient to bottom, transition effect doesn't seems to work (code below).
.box2:hover{
color: #FFF;
background-image: linear-gradient(to bottom, #000 50%, #fff 50%);
background-position: 100% 0%;
background-size: 100% 200%;
transition: all 1s ease;
border: none;
}
Any help would be appreciated.
Below is the link to check (hover on all boxes)
https://conrad93.github.io/linear-gradient-sliding-effect/boxbox.html

The problem is that your styles are not applied when you aren't hovering over the element. The solution is therefore to apply these styles to the default state of the element as well. Something like this:
// html
<div class="container">
<div class="box box-1"></div>
<div class="box box-2"></div>
<div class="box box-3"></div>
<div class="box box-4"></div>
</div>
// CSS
.container {
display: flex;
}
.box {
width: 100px;
height: 100px;
border: solid 1px red;
margin: 5px;
transition: all .4s ease;
background-color: lightgoldenrodyellow;
}
.box-1 {
background-image: linear-gradient(to top, crimson 50%, lightgoldenrodyellow 50%);
background-position: 0% 0%;
background-size: 100% 200%;
}
.box-1:hover {
background-position: 0% 100%;
}
.box-2 {
background-image: linear-gradient(to bottom, crimson 50%, lightgoldenrodyellow 50%);
background-position: 0% -100%;
background-size: 100% 200%;
}
.box-2:hover {
background-position: 0% -200%;
}
.box-3 {
background-color: crimson;
background-image: linear-gradient(to bottom left, crimson 50%, lightgoldenrodyellow 50%);
background-position: 0% 100%;
background-size: 200% 200%;
background-repeat: no-repeat;
}
.box-3:hover {
background-position: 0% -100%;
}
.box-4 {
background-image: linear-gradient(to top right, crimson 50%, lightgoldenrodyellow 50%);
background-position: 0% -100%;
background-size: 200% 200%;
background-repeat: no-repeat;
}
.box-4:hover {
background-position: 0% 100%;
}
Within the declaration block for the hover state, you only need to declare those properties that are changing, in this case background-position.
Here is a working example on JsFiddle.
You probably also want the diagonal gradient to animate up from the bottom left of the element. This is a little tricky to do, but you can see it working in the example. To understand why it works, you need to understand what the x and y values in the background-position property mean. MDN docs are a good place to learn.

Related

Css animation: fill a div with a color from left to right

I'm trying to create an animated background fill effect (still learning animation) but the fill color jumps quickly before it reaches the end of the div. What's the issue and how to fix it? Thanks in advance.
.outer {
margin: 50px;
}
.button {
border: 1px solid black;
border-radius: 3px;
width: 100px;
height: 30px;
display: block;
background: linear-gradient(to right, black 50%, transparent 50%);
background-size: 200% 100%;
background-position: right bottom;
animation: makeItfadeIn 3s 1s;
animation-fill-mode:forwards;
}
#-webkit-keyframes makeItfadeIn {
100% {
background-position: left bottom;
background: linear-gradient(to right, black 100%, black 0%);
}
}
#keyframes makeItfadeIn {
100% {
background-position: left bottom;
background: linear-gradient(to right, black 100%, black 0%);
}
}
<div class="outer">
<div class="button">
</div>
</div>
Background inside the animation is the culprit. You simply need to animate the position from right to left:
.outer {
margin: 50px;
}
.button {
border: 1px solid black;
border-radius: 3px;
width: 100px;
height: 30px;
display: block;
background: linear-gradient(to right, black 50%, transparent 0);
background-size: 200% 100%;
background-position: right;
animation: makeItfadeIn 3s 1s forwards;
}
#keyframes makeItfadeIn {
100% {
background-position: left;
}
}
<div class="outer">
<div class="button">
</div>
</div>
Related to get more details: Using percentage values with background-position on a linear-gradient

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

I am working on creating a Facebook content placeholder with the shimmer effect. I just want to animate the background property (or applying the linear gradient from top left to bottom right,) from the top left and end to the bottom right.
.Box {
height: 100px;
width: 100px;
margin: 16px;
background: #f6f7f8;
border-radius: 50%;
}
.Shine {
display: inline-block;
background: linear-gradient(to right, #eeeeee 8%, #dddddd 18%, #eeeeee 33%);
background-repeat: no-repeat;
animation-duration: 1s;
animation-fill-mode: forwards;
animation-iteration-count: infinite;
animation-name: placeholderShimmer;
animation-timing-function: linear;
}
#keyframes placeholderShimmer {
0% {
background-position: -1000px 0;
}
100% {
background-position: 10px 0;
}
}
<div class="Shine">
<div class="Box"> </div>
</div>
Now it's growing linearly from left to right.
Youn need to adjust the gradient then consider percentage value to have a better effect:
.Box {
height: 100px;
width: 100px;
margin: 16px;
background: #f6f7f8;
border-radius: 50%;
}
.Shine {
display: inline-block;
background:
linear-gradient(to bottom right, #eeeeee 40%, #dddddd 50%, #eeeeee 60%);
background-size:200% 200%;
background-repeat: no-repeat;
animation:placeholderShimmer 2s infinite linear;
}
#keyframes placeholderShimmer {
0% {
background-position:100% 100%; /*OR bottom right*/
}
100% {
background-position:0 0; /*OR top left*/
}
}
<div class="Shine">
<div class="Box"></div>
</div>
The trick is that the background will be twice as big as the container (200%x200%) with a diagonal direction and we make the coloration in the middle (around 50%). Then we simply slide this big background from top left to bottom right.
Related question for more details: Using percentage values with background-position on a linear-gradient

CSS transition background-image jump hover in Safari

I have an issue with a transition effect in Safari and could use some help. My CSS hover swaps background images, as it should, but the hover image 'jumps' from its position, up and to the left (which I don't want to happen). Thanks.
Structure:
.sidebar .widget {
height: 276px;
width: 326px;
background:url('/wp-content/themes/a-theme/svg/polygon-image.svg') no-repeat center center;
background-size: 100%;
}
.sidebar .widget:hover {
background:url('/wp-content/themes/a-theme/svg/polygon-image-hover.svg') no-repeat center center;
background-size: 100%;
transition: 0.5s ease-in-out;
}
<div class="sidebar">
<aside id="black-studio-tinymce-18" class="widget widget_black_studio_tinymce">
<div class="textwidget">
<h3 style="text-align: center;">Sample Text<br></h3>
</div>
</aside>
</div>
avoid browsers bugs and clean:
.sidebar .widget {
height: 276px;
width: 326px;
background-repeat:no-repeat;
background-position: center center;
background-image:url(/wp-content/themes/a-theme/svg/polygon-image.svg);
background-size: 100%;
transition:background-image 0.5s ease-in-out;
}
.sidebar .widget:hover {
background-image:url(/wp-content/themes/a-theme/svg/polygon-image-hover.svg);
}
From your help, I modified my CSS to get exactly what I needed. I had a background image of a polygon shape and a graphic in the middle of it. I kept the graphic and used a CSS polygon for the shape, instead, and now my hover effect works in Firefox, Chrome, and Safari.
See my CSS below and thanks again for the replies.
sidebar .widget {
background: url('/wp-content/uploads/2017/08/image.png') no-repeat center center;
background-color: rgba(255, 165, 0, 0.85);
background-size: 70%;
-webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
height: 276px;
width: 326px;
display: flex;
justify-content: center;
align-items: center;
}
.sidebar .widget:hover {
background: url('/wp-content/uploads/2017/08/image.png') no-repeat center center;
background-color: rgba(108, 218, 244, 0.8);
background-size: 80%;
-webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
height: 276px;
width: 326px;
transition: 0.5s ease-in-out;
}

How to create a responsive div in cone shape

does anyone knows a way to create a div with responsive width in shape of an inverted cone (see attached code snippet) only using css. Also this div needs to have a repeated background image (pattern).
I tried to use clipPath:
#div {
height: 100%;
width: 100%;
-webkit-clip-path: polygon(50% 90px, 100% 0%, 100% 100%, 0 100%, 0 0);
clip-path: polygon(50% 25%, 100% 0, 100% 100%, 0 100%, 0 0);
background: blue;
padding-top: 160px;
}
<div id="div"></div>
This works fine in Safari and Chrome but won't work in Mozilla, Opera or IE.
Is there a way to achieve for all relevant browsers?
Any help would be appreciated.
Use linear-gradient with side or corner values instead of fixed angles. You can make that shape with transforms too, but that'll require JS to make it responsive.
Fiddle
body {
background-color: blue;
margin: 0;
padding: 0;
}
div {
height: 150px;
width: 100%;
position: relative;
}
div:after, div:before {
content:"";
position: absolute;
height: inherit;
width: 50%;
}
div:before {
left: 0;
background: -webkit-linear-gradient(to bottom left, white 50%, transparent 50%);
background: -moz-linear-gradient(to bottom left, white 50%, transparent 50%);
background: -o-linear-gradient(to bottom left, white 50%, transparent 50%);
background: linear-gradient(to bottom left, white 50%, transparent 50%);
}
div:after {
right: 0;
background: -webkit-linear-gradient(to bottom right, white 50%, transparent 50%);
background: -moz-linear-gradient(to bottom right, white 50%, transparent 50%);
background: -o-linear-gradient(to bottom right, white 50%, transparent 50%);
background: linear-gradient(to bottom right, white 50%, transparent 50%);
}
<div></div>
You can set the div to have overflow hidden, and then set 2 pseudo elements with skew, one for each half
.test {
width: 400px;
height: 300px;
overflow: hidden;
position: relative;
}
.test:after, .test:before {
content: "";
position: absolute;
top: 0px;
width: 50%;
height: 100%;
}
.test:before {
left: 0px;
transform: skewY(15deg);
transform-origin: top left;
background: repeating-linear-gradient(-15deg, white 0px, lightblue 40px);
}
.test:after {
right: 0px;
transform: skewY(-15deg);
transform-origin: top right;
background: repeating-linear-gradient(15deg, white 0px, lightblue 40px);
}
<div class="test"></div>

Transition background-color via slide animation

I am trying to change the background-color of an element on hover by using CSS transitions. I want to do this by having it scroll up from the bottom. I can fade the background in using this, but I would like it to slide up:
-webkit-transition: background-color 0.5s linear;
-moz-transition: background-color 0.5s linear;
-o-transition: background-color 0.5s linear;
transition: background-color 0.5s linear;
One other thought, would it be better to scroll up a separate element with the background applied to it?
In order to slide the background color up you would need to use a background image, or a gradient of some sort, while gradually adjusting the background-position:
.box {
width: 200px; height: 100px;
background-size: 100% 200%;
background-image: linear-gradient(to bottom, red 50%, black 50%);
-webkit-transition: background-position 1s;
-moz-transition: background-position 1s;
transition: background-position 1s;
}
.box:hover {
background-position: 0 -100%;
}
<div class="box"></div>
An extension to #Sampson answer where I will consider more friendly values easier to understand:
.box {
width: 100px;
height: 100px;
display:inline-block;
background-size: 200% 200%;
transition: background-position 1s;
}
.to-top{
background-image: linear-gradient(to top, red 50%, black 0);
background-position: top;
}
.to-top:hover {
background-position: bottom;
}
.to-bottom{
background-image: linear-gradient(to bottom, red 50%, black 0);
background-position: bottom;
}
.to-bottom:hover {
background-position: top;
}
.to-left{
background-image: linear-gradient(to left, red 50%, black 0);
background-position: left;
}
.to-left:hover {
background-position: right;
}
.to-right{
background-image: linear-gradient(to right, red 50%, black 0);
background-position: right;
}
.to-right:hover {
background-position: left;
}
<div class="box to-top"></div>
<div class="box to-bottom"></div>
<div class="box to-left"></div>
<div class="box to-right"></div>
Here is more fancy transitions:
.box {
width: 100px;
height: 100px;
display:inline-block;
transition: 1s;
}
.to-center{
background:
linear-gradient(black,black) no-repeat,
red;
background-size: 100% 100%;
background-position:center;
}
.to-center:hover {
background-size: 0% 100%; /* Or 100% 0% */
}
.from-center{
background:
linear-gradient(red,red) no-repeat,
black;
background-size: 0% 100%; /* Or 100% 0% */
background-position:center;
}
.from-center:hover {
background-size: 100% 100%;
}
.diagonal-right{
background-image:linear-gradient(to bottom right,red 49.5%,black 50%);
background-size: 200% 200%;
background-position:bottom right;
}
.diagonal-right:hover {
background-position:top left;
}
.diagonal-left{
background-image:linear-gradient(to bottom left,red 49.5%,black 50%);
background-size: 200% 200%;
background-position:bottom left;
}
.diagonal-left:hover {
background-position:top right;
}
<div class="box to-center"></div>
<div class="box from-center"></div>
<div class="box diagonal-right"></div>
<div class="box diagonal-left"></div>
Related question to get more details about how background-position works combined with background-size: Using percentage values with background-position on a linear-gradient
Other ideas using circular shapes:
.box {
width: 100px;
height: 100px;
display:inline-block;
transition: 1s;
}
.to-center{
background:
radial-gradient(farthest-side,black 98%,transparent) no-repeat,
red;
background-size: 150% 150%;
background-position:center;
}
.to-center:hover {
background-size: 0% 0%;
}
.from-center{
background:
radial-gradient(farthest-side,red 98%,transparent) no-repeat,
black;
background-size: 0% 0%;
background-position:center;
}
.from-center:hover {
background-size: 150% 150%;
}
.diagonal-right{
background:radial-gradient(farthest-side,red 48%,transparent 50%) no-repeat,
black;
background-size: 300% 300%;
background-position:bottom right;
}
.diagonal-right:hover {
background-position:center;
}
.to-left{
background:radial-gradient(farthest-side,red 48%,transparent 50%) no-repeat,
black;
background-size: 400% 400%;
background-position:left;
}
.to-left:hover {
background-position:center;
}
<div class="box to-center"></div>
<div class="box from-center"></div>
<div class="box diagonal-right"></div>
<div class="box to-left"></div>
Related: How to animate a radial-gradient using CSS?

Resources