简体   繁体   中英

Make gradient background button with hover effect in css

Although it seems like a basic question i wasn't able to find the solution to this problem. What I want is a gradient background button like this( https://prnt.sc/r0mx54 ). When the user hovers over this button then I want this behavior ( https://prnt.sc/r0mxtj ). Now the problem is I can make this effect by changing the background color from gradient to any normal color(in this case white). But when I try to achieve this effect by setting the background to either transparent or inherit then I wasn't able to do so. Here is my code. Please help. Normal button feature gradient background-color. Hover button feature gradient border, transparent background-color.

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

Please check this simple method

<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;
}

You should add .bg-gradient to only parent element and on hover make background as transparent and apply border, See the example below.

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

Source for gradient border - https://css-tricks.com/gradient-borders-in-css/

Added:hover to change the background for the inside 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>

try this

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

Here is an idea using mask based on this previous answer :

 .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) }
 <div class="button"> some text</div>

The same trick using an 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM