Bea Lovelace
Bea Lovelace

Reputation: 55

CSS: Button border gradient invert when transition on hover

I'm setting this button transition, and I found that the effect I use for other buttons in my web, doesn't work well with this one. Border colors invert on transition.

I must use gradient background, so change it isn't an option.

#button {
    background: linear-gradient(#FFF,#FFF) padding-box, linear-gradient(to right, blue 0%, green 100%) border-box;
    border: 3px solid transparent;
    color: #000;
    background-color: white;
    border-radius: 25px;
    margin-top: 60px;
    margin-bottom: 0px;
    font-weight: 600;
    font-size: 14px;
    padding: 7px 22px;
    transition: all .5s ease;
    margin-left: auto;
    margin-right: auto;
    display: block;
    }
    
#button:hover{
background: linear-gradient(to right, blue 0%, green 100%);
    border: 3px solid transparent;
    color: #FFF;
    transition: all .5s ease;
    transition-property: all;
    transition-duration: 0.5s;
    transition-timing-function: ease;
    transition-delay: 0s;
    }
<button id="button"> BUTTON </button>

Upvotes: 2

Views: 1793

Answers (2)

Temani Afif
Temani Afif

Reputation: 272723

Here is an idea of transition where you can play with background-size:

#button {
    background: 
      linear-gradient(#FFF,#FFF) padding-box center, 
      linear-gradient(to right, blue 0%, green 100%) border-box;
    background-size:100% 100%,auto;
    background-repeat:no-repeat;
    border: 3px solid transparent;
    color: #000;
    border-radius: 25px;
    margin: 60px auto 0;
    font-weight: 600;
    font-size: 14px;
    padding: 7px 22px;
    transition: all .5s ease;
    display: block;
    }
    
#button:hover{
    background-size:0 0,auto;
    color: #FFF;
}
<button id="button"> BUTTON </button>

That you can modify by adjusting the size and position:

#button {
    background: 
      linear-gradient(#FFF,#FFF) padding-box left, 
      linear-gradient(to right, blue 0%, green 100%) border-box;
    background-size:100% 100%,auto;
    background-repeat:no-repeat;
    border: 3px solid transparent;
    color: #000;
    border-radius: 25px;
    margin: 60px auto 0;
    font-weight: 600;
    font-size: 14px;
    padding: 7px 22px;
    transition: all .5s ease;
    display: block;
    }
    
#button:hover{
    background-size:0 100%,auto;
    color: #FFF;
}
<button id="button"> BUTTON </button>

Upvotes: 1

skobaljic
skobaljic

Reputation: 9614

Your hover/focus gradient should be border-box:

background: linear-gradient(to right, blue 0%, green 100%) border-box;

.myb {
    display: inline-block;
    cursor: pointer;
    background: linear-gradient(#FFF, #FFF) padding-box, linear-gradient(to right, blue 0%, green 100%) border-box;
    border: 3px solid transparent;
    color: #000;
    background-color: white;
    border-radius: 25px;
    margin-top: 60px;
    margin-bottom: 0px;
    font-weight: 600;
    font-size: 14px;
    padding: 7px 22px;
    transition: all .5s ease;
    margin-left: auto;
    margin-right: auto;
}

.myb:hover,
.myb:focus {
    background: linear-gradient(to right, blue 0%, green 100%) border-box;
    border: 3px solid transparent;
    color: #FFF;
    transition: all .5s ease;
    transition-property: all;
    transition-duration: 0.5s;
    transition-timing-function: ease;
    transition-delay: 0s;
}
<div class="myb">
    Button
</div>

Also on JSFiddle.

Upvotes: 4

Related Questions