繁体   English   中英

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

[英]Using gradient on div border with rounded corners

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

我已经找到了所有关于如何使用渐变的网站( http://css-tricks.com/css3-gradients/、http://gradients.glrzad.com/等),但我还是想不通了解如何使它适用于圆形边缘边界。

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

您可以嵌套两个元素并让外部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

在我看来,使用 :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/

这是一个简单的解决方案:

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

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

我的解决方案仅适用于水平渐变和圆角(但不是圆形)按钮。 我同时使用了“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/

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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