在带有圆角的div边框上使用渐变

问题描述 投票:0回答:5

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

我已经找到了所有关于如何使用渐变的网站(http://css-tricks.com/css3-gradients/http://gradients.glrzad.com/等),但我仍然不知道如何让它适用于圆边边框。

有人可以引导我访问一个描述如何执行此操作的网站,甚至可以帮助我编写代码吗?

css html
5个回答
10
投票

这是简单的解决方案:

结果:带有渐变边框的 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;
} 

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


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/


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/


0
投票
border="solid 1px transparent"
background="linear-gradient(Canvas, Canvas) padding-box, linear-gradient(red, blue) border-box"
borderRadius="1rem"

背景的第二部分是你想要的渐变^

© www.soinside.com 2019 - 2024. All rights reserved.