CSS:悬停过渡时,按钮边界渐变会反转

时间:2019-04-25 08:42:21

标签: css border transition gradient

我正在设置此按钮转换,但是我发现我用于网络中其他按钮的效果不适用于此按钮。 边框颜色在过渡时会反转。

我必须使用渐变背景,所以不能更改它。

#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>

2 个答案:

答案 0 :(得分:3)

您的悬停/焦点渐变应为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>

也在JSFiddle上。

答案 1 :(得分:1)

这是一种过渡概念,您可以在其中使用背景尺寸玩游戏:

#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>

您可以通过调整大小和位置进行修改:

#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>