【问题标题】:Make gradient background button with hover effect in css在css中制作具有悬停效果的渐变背景按钮
【发布时间】:2020-02-11 21:02:22
【问题描述】:

虽然这似乎是一个基本问题,但我无法找到解决此问题的方法。我想要的是这样的渐变背景按钮(https://prnt.sc/r0mx54)。当用户将鼠标悬停在此按钮上时,我想要此行为 (https://prnt.sc/r0mxtj)。现在的问题是我可以通过将背景颜色从渐变更改为任何正常颜色(在本例中为白色)来制作此效果。但是当我尝试通过将背景设置为透明或继承来实现这种效果时,我无法做到这一点。这是我的代码。请帮忙。 普通按钮具有渐变背景色。悬停按钮具有渐变边框,透明背景色。

.flex {
  display: flex;
  flex-wrap: wrap;
}

.border-radius-20 {
  border-radius: 20px;
}

.uppercase {
  text-transform: uppercase;
}

.color-white {
  color: #fff;
}

.all-center {
  align-items: center;
  justify-content: center;
}

.bg-gradient {
  background: linear-gradient(to right, #049eca, #12adb8, #1cb8aa 70%);
}

li {
  padding: 2px;
}

li:hover a {
  background: #fff;
}
<ul>
  <li class="flex relative all-center border-radius-20 bg-gradient">
    <a href="#" class="pd-8-10 border-radius-20 bg-gradient color-white uppercase">Schedule a Meeting</a>
  </li>
</ul>

【问题讨论】:

  • 可以发html代码吗?
  • @Manikandan2811 抱歉。我编辑了我的问题。这是一个拼写错误。
  • 悬停时要切换背景渐变色吗?
  • @Manikandan2811 是的。但我也想要一个渐变边框。当按钮具有渐变边框时,背景变为透明。

标签: javascript html css user-interface


【解决方案1】:

请检查这个简单的方法

<input type="button" class="button" value="Schedule a meeting"/>

.button {
    background-color: #059fc9;
    background-image: linear-gradient(to right, #059fc9, #1cb8aa);
    box-shadow: none;
    border: none;
    border-radius: 25px;
    height: 40px;
    padding: 0px 15px;
    color: #fff;
    font-size: 14px;
    font-weight: bold;
    text-transform: uppercase;
}
.button:hover {
    cursor: pointer;
    color: #1cb8aa;
    border: 2px solid #1cb8aa;
    background: transparent;
}

【讨论】:

    【解决方案2】:

    这是基于previous answer 使用mask 的想法:

    .button {
      display:inline-block;
      margin:10px;
      padding:5px;
      font-size:25px;
      width:250px;
      text-align:center;
      line-height:1.8;
      background:linear-gradient(to right,red,blue) 0 0/0 0;
      color:#fff;
      border-radius:50px;
      position:relative;
      z-index:0;
      transition:1s all;
    }
    .button:before,
    .button:after{
      content:"";
      position:absolute;
      z-index:-1;
      top:0;
      left:0;
      right:0;
      bottom:0;
      background-image:inherit;
      border-radius:inherit;
      transition:1s all;
    }
    .button:before {
      -webkit-mask:
        linear-gradient(#fff,#fff) top   /100% 5px no-repeat,
        linear-gradient(#fff,#fff) bottom/100% 5px no-repeat,
        radial-gradient(farthest-side at left,transparent calc(100% - 6px), #fff calc(100% - 5px)) right/27px 100% no-repeat,
        radial-gradient(farthest-side at right,transparent calc(100% - 6px), #fff calc(100% - 5px)) left/27px 100% no-repeat;
      mask:
        linear-gradient(#fff,#fff) top   /100% 5px no-repeat,
        linear-gradient(#fff,#fff) bottom/100% 5px no-repeat,
        radial-gradient(farthest-side at left,transparent calc(100% - 6px), #fff calc(100% - 5px)) right/27px 100% no-repeat,
        radial-gradient(farthest-side at right,transparent calc(100% - 6px), #fff calc(100% - 5px)) left/27px 100% no-repeat;
    }
    
    .button:hover::after {
      opacity:0;
    }
    .button:hover {
      color:red;
    }
    
    body {
      background:linear-gradient(to right,gray,white)
    }
    &lt;div class="button"&gt; some text&lt;/div&gt;

    使用 SVG 的相同技巧:

    .button {
      display:inline-block;
      margin:10px;
      padding:5px;
      font-size:25px;
      width:250px;
      text-align:center;
      line-height:1.8;
      background:linear-gradient(to right,red,blue) 0 0/0 0;
      color:#fff;
      border-radius:50px;
      position:relative;
      z-index:0;
      transition:1s all;
    }
    .button:after{
      content:"";
      position:absolute;
      z-index:-1;
      top:0;
      left:0;
      right:0;
      bottom:0;
      background-image:inherit;
      border-radius:inherit;
      transition:1s all;
    }
    
    .button:hover::after {
      opacity:0;
    }
    .button:hover {
      color:red;
    }
    
    .hide {
     height:0;
     width:0;
    }
    .button svg {
      position:absolute;
      top:0;
      left:0;
      height:100%;
      width:100%;
      z-index:-1;
    }
    
    body {
      background:linear-gradient(to right,gray,white)
    }
    <svg class="hide" xmlns="http://www.w3.org/2000/svg"><defs><linearGradient id="Gradient" x1="0" x2="250" y1="0" y2="0" gradientUnits="userSpaceOnUse"><stop stop-color="red" offset="0"/><stop stop-color="blue" offset="1"/></linearGradient></defs><rect x="3" y="3" width="100%" height="100%" id="border" style="height:calc(100% - 6px);width:calc(100% - 6px)" rx="25" ry="25" stroke-width="5" fill="transparent" stroke="url(#Gradient)"/></svg>
    
    <div class="button">
    <svg xmlns="http://www.w3.org/2000/svg">
      <use href="#border" />
    </svg>
    some text</div>

    【讨论】:

      【解决方案3】:

      您应该将.bg-gradient 仅添加到父元素,并在悬停时将background 设为transparent 并应用边框,请参见下面的示例。

      .flex {
        display: flex;
        flex-wrap: wrap;
      }
      
      .border-radius-20 {
        border-radius: 20px;
      }
      
      .uppercase {
        text-transform: uppercase;
      }
      
      .color-white {
        color: #fff;
      }
      
      .all-center {
        align-items: center;
        justify-content: center;
      }
      
      .bg-gradient {
        background: linear-gradient(to right, #049eca, #12adb8, #1cb8aa 70%);
      }
      
      .bg-gradient:hover {
       background: transparent;
       border: 1px solid #1cb8aa;
      }
      
      li {
        padding: 2px;
      }
      
      li:hover a {
        color: #1cb8aa;
      }
      <ul>
        <li class="flex relative all-center border-radius-20 bg-gradient">
          <a href="#" class="pd-8-10 border-radius-20 color-white uppercase">Schedule a Meeting</a>
        </li>
      </ul>

      【讨论】:

      • 我将 bg-gradient 添加到子元素中,因为我希望在悬停时使用渐变边框。
      【解决方案4】:

      渐变边框的来源 - https://css-tricks.com/gradient-borders-in-css/

      添加 :hover 以更改内部 div 的背景。

      body {
        height: 100vh;
        margin: 0;
        display: grid;
        place-items: center;
        background: #222;
      }
      
      .module-border-wrap {
        max-width: 250px;
        padding: 1rem;
        position: relative;
        background: linear-gradient(to right, red, purple);
        padding: 3px;
      }
      .module-border-wrap:hover .module {
        background: #222;
      }
      .module {
        background: linear-gradient(to right, red, purple);
        color: white;
        padding: 2rem;
      }
         
      
         <div class="module-border-wrap">
      <div class="module">
          Lorem ipsum
      </div>
         </div>

      【讨论】:

      • 当我尝试更改透明背景的代码(将模块的背景颜色更改为#222 的模块边框环绕悬停效果)时,它不起作用。背景透明,这是主要问题。
      【解决方案5】:

      试试这个

      .flex{
      display:flex;
      flex-wrap:wrap;
      }
      .border-radius-20{
      border-radius:20px;
      }
      .uppercase{
      text-transform:uppercase;
      }
      .color-white{
      color:#fff;
      }
      .all-center{
      align-items:center;
      justify-content:center;
      }
      
      li{
      padding:2px;
      position: relative;
      border-radius:20px;
      transition: all .3s ease;
      cursor:pointer;
      }
      li a {
      transition: all .3s ease;
      text-decoration: none;
      }
      .bg-gradient:before{
      content:'';
      position: absolute;
      display: block;
      top:0px;
      left:0px;
      width: 100%;
      height: 100%;
      background: linear-gradient(to right,#049eca,#12adb8,#1cb8aa 70%);
      border-radius:20px;
      z-index: -1;
      transition: all .3s ease;
      }
      .bg-gradient:after{
      content:'';
      position: absolute;
      display: block;
      opacity:0;
      top:2px;
      left:2px;
      width: calc(100% - 4px);
      height: calc(100% - 4px);
      background: white;
      border-radius:20px;
      z-index: -1;
      transition: all .3s ease;
      }
      .bg-gradient:hover:after {
      opacity: 1;
      }
      li:hover a {
      color: #1cb8aa;
      }
      <li class="flex relative all-center bg-gradient">
      <a href="#" class="pd-8-10 border-radius-20 color-white uppercase">Schedule a Meeting</a></li>

      【讨论】:

      • @Yousef 谢谢,但我也想要一个渐变边框。
      • 感谢@Yousef,它几乎解决了问题,但在悬停时,我想要透明背景和渐变边框。
      猜你喜欢
      • 1970-01-01
      • 2012-08-26
      • 2021-10-28
      • 1970-01-01
      • 2014-07-05
      • 2018-07-30
      • 1970-01-01
      • 2019-07-14
      • 2014-04-15
      相关资源
      最近更新 更多