allegri
allegri

Reputation: 189

CSS gradient animation degree change

I found this codepen, which i want to apply to a certain HTML element on hover. Works fine, but i want to change the movement from horizontal to vertical.

/* <- Demo Stuff Start */

body{
  font-family: 'Montserrat', sans-serif;
  margin:0;
}

.container {
  display: flex;
  align-items: center;
  align-content: center;
  flex-wrap: wrap;
  width: 80vw;
  margin: 0 auto;
  min-height: 100vh;
}
.btn {
  flex: 1 1 auto;
  margin: 10px;
  padding: 30px;
  text-align: center;
  text-transform: uppercase;
  transition: 0.5s;
  background: linear-gradient(90deg, var(--c1, #f6d365), var(--c2, #fda085) 51%, var(--c1, #f6d365)) var(--x, 0)/ 200%;
  color: white;
 /* text-shadow: 0px 0px 10px rgba(0,0,0,0.2);*/
  box-shadow: 0 0 20px #eee;
  border-radius: 10px;
 }

/* Demo Stuff End -> */

/* <- Magic Stuff Start */

.btn:hover { --x: 100%; }

.btn-1 {
  --c1: #f6d365;
  --c2: #fda085;
}

.btn-2 {
  --c1: #fbc2eb;
  --c2: #a6c1ee;
}

.btn-3 {
  --c1: #84fab0;
  --c2: #8fd3f4;
}

.btn-4 {
  --c1: #a1c4fd;
  --c2: #c2e9fb;
}

.btn-5 {
  --c1: #ffecd2;
  --c2: #fcb69f;
}

/* Magic Stuff End -> */
<div class="container">
  <a class="btn btn-1">Hover me</a>
  <a class="btn btn-2">Hover me</a>
  <a class="btn btn-3">Hover me</a> 
  <a class="btn btn-4">Hover me</a> 
  <a class="btn btn-5">Hover me</a>
</div>

Whenever i change the "90deg" in the codepen to "180deg", the animation stops working. I fiddled a lot with it but cant get it to work as intended.

What am I missing?

Any help is appreciated.

The goal is to create an animated gradient overlay on a dynamic element in WP. Similar to this:

enter image description here

Upvotes: 0

Views: 272

Answers (1)

Temani Afif
Temani Afif

Reputation: 272909

You need to change more than the angle. You need to animate the Y position and make the height 200%

/* <- Demo Stuff Start */

body{
  font-family: 'Montserrat', sans-serif;
  margin:0;
}

.container {
  display: flex;
  align-items: center;
  align-content: center;
  flex-wrap: wrap;
  width: 80vw;
  margin: 0 auto;
  min-height: 100vh;
}
.btn {
  flex: 1 1 auto;
  margin: 10px;
  padding: 30px;
  text-align: center;
  text-transform: uppercase;
  transition: 0.5s;
  background: linear-gradient(180deg, var(--c1, #f6d365), var(--c2, #fda085) 51%, var(--c1, #f6d365)) 0 var(--y, 0)/100% 200%;
  color: white;
 /* text-shadow: 0px 0px 10px rgba(0,0,0,0.2);*/
  box-shadow: 0 0 20px #eee;
  border-radius: 10px;
 }

/* Demo Stuff End -> */

/* <- Magic Stuff Start */

.btn:hover { --y: 100%; }

.btn-1 {
  --c1: #f6d365;
  --c2: #fda085;
}

.btn-2 {
  --c1: #fbc2eb;
  --c2: #a6c1ee;
}

.btn-3 {
  --c1: #84fab0;
  --c2: #8fd3f4;
}

.btn-4 {
  --c1: #a1c4fd;
  --c2: #c2e9fb;
}

.btn-5 {
  --c1: #ffecd2;
  --c2: #fcb69f;
}

/* Magic Stuff End -> */
<div class="container">
  <a class="btn btn-1">Hover me</a>
  <a class="btn btn-2">Hover me</a>
  <a class="btn btn-3">Hover me</a> 
  <a class="btn btn-4">Hover me</a> 
  <a class="btn btn-5">Hover me</a>
</div>

To make sure it works in both cases you can do like below:

/* <- Demo Stuff Start */

body{
  font-family: 'Montserrat', sans-serif;
  margin:0;
}

.container {
  display: flex;
  align-items: center;
  align-content: center;
  flex-wrap: wrap;
  width: 80vw;
  margin: 0 auto;
  min-height: 100vh;
}
.btn {
  flex: 1 1 auto;
  margin: 10px;
  padding: 30px;
  text-align: center;
  text-transform: uppercase;
  transition: 0.5s;
  background: linear-gradient(var(--a,180deg), var(--c1, #f6d365), var(--c2, #fda085) 51%, var(--c1, #f6d365)) var(--y, 0) var(--y, 0)/200% 200%;
  color: white;
 /* text-shadow: 0px 0px 10px rgba(0,0,0,0.2);*/
  box-shadow: 0 0 20px #eee;
  border-radius: 10px;
 }

/* Demo Stuff End -> */

/* <- Magic Stuff Start */

.btn:hover { --y: 100%; }

.btn-1 {
  --c1: #f6d365;
  --c2: #fda085;
}

.btn-2 {
  --c1: #fbc2eb;
  --c2: #a6c1ee;
  --a:90deg;
}

.btn-3 {
  --c1: #84fab0;
  --c2: #8fd3f4;
}

.btn-4 {
  --c1: #a1c4fd;
  --c2: #c2e9fb;
  --a:90deg;
}

.btn-5 {
  --c1: #ffecd2;
  --c2: #fcb69f;
}

/* Magic Stuff End -> */
<div class="container">
  <a class="btn btn-1">Hover me</a>
  <a class="btn btn-2">Hover me</a>
  <a class="btn btn-3">Hover me</a> 
  <a class="btn btn-4">Hover me</a> 
  <a class="btn btn-5">Hover me</a>
</div>

Related: Using percentage values with background-position on a linear-gradient

Upvotes: 1

Related Questions