Сделать кнопку градиента фона с эффектом наведения на css - PullRequest
0 голосов
/ 11 февраля 2020

Хотя это кажется базовым c вопросом, я не смог найти решение этой проблемы. Мне нужна кнопка с градиентным фоном (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>

Ответы [ 5 ]

1 голос
/ 11 февраля 2020

Пожалуйста, проверьте этот простой метод

<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;
}
0 голосов
/ 11 февраля 2020

Вот идея использования 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)
}
<div class="button"> some text</div>

Тот же трюк с использованием 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>
0 голосов
/ 11 февраля 2020

Источник для градиентной границы - https://css-tricks.com/gradient-borders-in-css/

Добавлено: наведите курсор на изменение фона для внутреннего элемента 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>
0 голосов
/ 11 февраля 2020

попробуйте

.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>
0 голосов
/ 11 февраля 2020

Вы должны добавить .bg-gradient только к родительскому элементу и при наведении мыши сделать background как прозрачный и применить рамку, см. Пример ниже.

.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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...