Floy Rodd
Floy Rodd

Reputation: 111

CSS - smooth button gradient color transition on hover

I have the following button.

The CSS for the button above is this:

.cta-btn {
  display: inline-block;
  margin: 20px 0 0 20px;
  color: #fff;
  background-color: #FF8F1B;
  background-image: linear-gradient(to right, #2ab3ff, #ff2d00);
  box-shadow: 4px 5px 27px 4px rgba(220, 120, 184, 0.85);
  font-size: 21px;
  border-radius: 30px;
  padding: 12px 21px;
  font-family: Montserrat;
}
<a href="#" class="cta-btn">click me</a>

I want the button to change gradient color smoothly when I hover over it. I do not want the gradient color to just snap onto the button when I hover it. This is my attempt at a smooth gradient color transition:

a.cta-btn:hover {
  background-image: linear-gradient(to right,#FF2A67,#FF5D3A);
  color: #fff;
  box-shadow: 4px 5px 27px 4px rgba(255,45,45,0.85);
  transition: background-image .3s linear;
  transition: box-shadow .3s linear;
}

Any help is much appreciated.

Upvotes: 7

Views: 15780

Answers (5)

thr
thr

Reputation: 61

I have try all your answers, and i prefer this : It's lightly and working perfect with only background-size property for the hover and Work with Chrome IE and ff

Enjoy

.ex-button-0 {
  
    transition: all ease 0.5s;
    cursor: pointer;
    padding: 10.5px 25px;
    border: none;
    border-radius: 35px;
  
    background-image: linear-gradient(to left, black, blue, yellow, orange);
    background-size:300%;
    background-position: 0 0;
    -webkit-appearance: none !important;
    color: #000;
    text-decoration:none
}
.ex-button-0:hover {
  background-position: 100% 0;
  color:#fff;
}
<a class="ex-button-0" href="">Exemple</a>

Upvotes: 6

Barry Nervous
Barry Nervous

Reputation: 11

Probably a little late to the party, but I did manage to get a gradient transition into a solid color, which is what I needed for my project.

Here is the codepen for proof of concept.

https://codepen.io/etc-umbrella/pen/pXremq

<button class="ui-button">This is a button</button>

<h2>Creating an aninmated gradient background button using only SCSS. Worked pretty good. Didn't have to use any crazy javascript</h2>
.ui-button {
  position: relative;
  overflow: hidden;
  border-radius: 6px;
  cursor: pointer;
  padding: 12px 18px;
  border: 1px solid aqua;
  background-color: white;
  color: #ffffff;
  font-family: raleway;
  font-weight: bold;
  font-size: 16px;
  z-index: 1;
  transition: all 800ms ease-in;
  &:after{
    content: '';
    position: absolute;
    left: -200%;
    top: 0px;
    width: 400%;
    height: 100%;
    background: rgb(33,209,159);
    background: linear-gradient(45deg, rgba(33,209,159,1) 0%, rgba(34,44,64,1) 50%, rgba(21,65,153,1) 100%);
    z-index: -1;
    transition: all 800ms ease-in;
  }
  &:hover{
    color: #ffffff;
    background-color: #21d19f;
  }
  &:hover:after{
    left: 0%;
    opacity: 0;
  }
}

Upvotes: 1

michael harvey
michael harvey

Reputation: 11

@import url('https://fonts.googleapis.com/css?family=Montserrat:500');

html,body {
  font-family: 'Montserrat', sans-serif;
  margin:0;
  padding:0;
  background: linear-gradient(to right, #c9d6ff, #e2e2e2);

}

div {
  height: 100vh;
  display: flex;
  align-items:center;
  justify-content:center;
}

h1 {
  font-size: 42px;
  background-size:200%;
  padding:15px;
   border-radius:5px;
  background-image: linear-gradient(to top left, #fe87c3     0%, #D38312 50%, #A83279 100%); 
  transition: .3s ease;
  cursor: pointer;
}
h1:hover {
  background-position: 90%;
  color: #202020;
}

.home {
  background-size: 200%

}**strong text**

Here is a demo

https://codepen.io/Mikeytown19/pen/aLpNZa

Upvotes: 0

guest271314
guest271314

Reputation: 1

Though still able to see background decreasing and increasing in dimensions, this is partially possible using multiple background properties at same element, toggling background-size property.

.cta-btn {
  color: #fff;
  background: linear-gradient(to right, #2ab3ff, #ff2d00)
    , linear-gradient(to right,#FF2A67,#FF5D3A);
  background-size:100% 100%, 0% 0%;
  background-origin: border-box, border-box;
  box-shadow: 4px 5px 27px 4px rgba(220, 120, 184, 0.85);
  font-size: 21px;
  border-radius: 30px;
  padding: 12px 21px;
  font-family: Montserrat;
  transition: background .3s linear;
}
.cta-btn:hover {
  background-size:0% 0%, 100% 100%;
  box-shadow: 4px 5px 27px 4px rgba(255,45,45,0.85);
}
<a href="#" class="cta-btn">click me</a>

Upvotes: 3

rgthree
rgthree

Reputation: 7273

Short answer, you can't using just background. However, you can achieve a similar effect using other elements (or pseudo elements) inside and fading them in on hover.

The following example uses two pseudo-elements as the two background states. On hover, we simply fade-in the new background giving a similar transition effect that would happen if gradients were transition-able.

NOTE: Not all browsers support transitions on pseudo elements, so you may need to add empty elements to achieve the same effect on older/unsupported browsers.

.cta-btn {
  position: relative;
  display: inline-block;
  margin: 20px 0 0 20px;
  color: #fff;
  box-shadow: 4px 5px 27px 4px rgba(220, 120, 184, 0.85);
  font-size: 21px;
  border-radius: 30px;
  overflow: hidden;
  padding: 12px 21px;
  font-family: Montserrat;
  transition: box-shadow.3s ease-in-out;
  text-decoration: none;
}

/* These are the two backgrounds, absolutely positioned to cover. */
.cta-btn::before,
.cta-btn::after {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background-image: linear-gradient(to right, #2ab3ff, #ff2d00);
  border-radius: 30px;
  z-index: -1;
}

.cta-btn::after {
  opacity: 0;
  background-image: linear-gradient(to right,#FF2A67,#FF5D3A);
  transition: opacity.3s ease-in-out;
}

/* On hover, transtiion the shadow of the anchor, and fade in the after element to show the new background. */
.cta-btn:hover {
  box-shadow: 4px 5px 27px 4px rgba(255,45,45,0.85);
}
.cta-btn:hover::after {
  opacity: 1;
}
<a href="#" class="cta-btn">click me</a>

Upvotes: 8

Related Questions