8

我有一些带有圆角和彩色边框的 div。我希望 div 的边框具有渐变,以便当用户将鼠标悬停在 div 上时它会发生变化。

我找到了所有关于如何使用渐变的网站(http://css-tricks.com/css3-gradients/http://gradients.glrzad.com/等),但我仍然想不通了解如何使其适用于圆形边缘边框。

有人会指导我到一个描述如何做到这一点的网站,甚至可以帮助我编写代码吗?

4

4 回答 4

5

这是简单的解决方案:

结果:带有渐变边框的 CSS 圆角

.yourClass {
  display: inline-flex;
  border: double 6px transparent;
  border-radius: 80px;
  background-image: linear-gradient(white, white), radial-gradient(circle at top left, #f00, #3020ff);
  background-origin: border-box;
  background-clip: content-box, border-box;
} 
于 2020-05-27T19:15:29.413 回答
4

您可以嵌套两个元素并让外部div充当渐变边框,然后您可以解决此问题,例如:

<div class="container">
  <div class="content">
    ...
  </div>
</div> 

然后在你的 CSS 中:

/* 
   unprefixed for conciseness, use a gradient generator por production code 
*/

.container { 
   background: linear-gradient(red, black);
 }

.content   { 
   background: white; 
   padding: 10px;
 }

有关工作示例,请查看https://stackoverflow.com/a/7066176/524555

于 2013-07-10T15:12:26.023 回答
2

在我看来,使用 :before 元素是最理想的解决方案,因为您可以通过 CSS 完全控制并且 HTML 标记保持干净。

.circle {
  width: 300px;
  height: 200px;
  background: white;
  border-radius: 100%;
  position: relative;
  text-align: center;
  padding: 20px;
  box-sizing: border-box;
}
.circle::before {
  border-radius: 100%;
  width: 100%;
  height:100%;
  content: '';
  background-image: linear-gradient(to bottom, #3acfd5 0%, #3a4ed5 100%);
  padding: 10px;
  top: -10px;
  left: -10px;
  position:absolute;
  z-index:-1;
}

您可以调整填充以及顶部和左侧值以调整边框粗细。

这是一个显示实际示例的 JSFiddle:http: //jsfiddle.net/wschwarz/e2ckdp2v/

于 2014-11-25T11:34:27.877 回答
1

我知道这个答案已经被回答和接受,但我想发布我使用的不同方法,因为如果按钮位于具有另一个渐变或图像的背景上,这个接受的答案将不起作用。

我的解决方案仅适用于水平渐变和圆角(但不是圆形)按钮。我同时使用了“border-image”属性和伪元素来实现这个效果:

该按钮将只有顶部和底部的“边框图像”边框。左右边界将用伪元素完成。

这是一个工作示例:

HTML:

<div class="background">
  <button class="button">
    My button!
  </button>
</div>

CSS:

*, *:before, *:after {
    box-sizing: border-box;
}

.background {
  background: linear-gradient(to bottom, #002e4b 0%,#1c4722 100%);
  width:500px;
  height:500px;
  display:flex;
  align-items:center;
  justify-content:center;
}

.button {
    box-sizing:border-box;
    display: inline-block;
    padding:0.5em 0;
    background:none;
    border:none;
    border-top:2px solid #0498f8;
    border-bottom:2px solid #0498f8;
    border-image: linear-gradient(to right, #0498f8 0%, #4db848 100%);
    border-image-slice: 1;
    position: relative;
    text-transform: lowercase;
    transition:background 0.3s;
    color: #fff;
    cursor: pointer;
    font-size:1em;
    &:before,
    &:after {
        content: '';
        display: block;
        width: 2em;
        height: calc(100% + 4px);
        border-radius: 3em 0 0 3em;
        border: 2px solid #0498f8;
        position: absolute;
        border-right: none;
        transition:background 0.3s;
        left: -2em;
        top: -2px;
    }
    &:after {
        border-radius: 0 3em 3em 0;
        border: 2px solid #4db848;
        position: absolute;
        border-left: none;
        left:auto;
        right: -2em;
        top: -2px;
    }
    &:hover {
        background:rgba(0,0,0,0.3);
        &:after,
        &:before {
            background:rgba(0,0,0,0.3);
        }
    }
}

https://jsfiddle.net/fnbq92sc/2/

于 2017-12-14T12:28:13.873 回答