简体   繁体   English

如何将线性渐变应用于输入边框?

[英]How to apply linear-gradient to input border?

Is it possible to to apply a linear-gradient color to input border?是否可以将线性渐变颜色应用于输入边框?
I've tried the following:我尝试了以下方法:

input{
    border: 5pt solid linear-gradient(rgba(0, 25, 50, 0.5), rgba(0, 0, 25, 0.5));
}


The problem with the code above is that you can't use linear-gradient as a color.上面代码的问题是你不能使用线性渐变作为颜色。
Just clarifying, I don't want to change the input background color, only border color.只是澄清一下,我不想改变输入背景颜色,只改变边框颜色。

JSFIDDLE: JSFIDDLE:
http://jsfiddle.net/o1yhod9t/ http://jsfiddle.net/o1yhod9t/

You cannot directly add a gradient to the border property as the third parameter only takes a color . 您不能直接向border属性添加渐变,因为第三个参数仅采用颜色 Instead you would have to use the border-image property like in the below snippet. 相反,您必须使用border-image属性,如下面的代码段所示。

Note that the browser support for this property is pretty low at present. 请注意,目前浏览器对此属性的支持非常低 For better browser support, the same could mimicked using background-image property. 为了获得更好的浏览器支持,可以使用background-image属性模仿。

 input { border-image-source: linear-gradient(rgba(0, 51, 102, 0.5), rgba(0, 0, 51, 0.5)); border-width: 5pt; border-image-slice: 1; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script> <input type="text" /> 

I see that the original answer provides no way to set a border radius.我看到原始答案无法设置边界半径。 You can pull this off with a weird combination of esoteric CSS elements:您可以通过深奥的 CSS 元素的奇怪组合来实现这一点:

input {
  padding: 0.5rem;
  border: double 3px transparent;
  border-radius: 6px;
  background-image: linear-gradient(white, white), 
                    linear-gradient(to right, orange, yellow);
  background-origin: border-box;
  background-clip: padding-box, border-box;
}

What this effectively does is create layered gradients.这有效地做的是创建分层渐变。 The inner one (the bit that fits within the element) is just white, so it doesn't appear to be anything unusual.内部的(适合元素的位)只是白色的,所以它看起来没有什么不寻常的。 Then around that, you add the actual gradient that you want (this can be as complicated or simple as you like).然后围绕它,添加你想要的实际渐变(这可以像你喜欢的那样复杂或简单)。

Finally, you set a transparent border (with the relevant border-radius to get your curved corners), before clipping the backgrounds to the relevant areas.最后,在将背景剪裁到相关区域之前,设置一个透明边框(使用相关的边框半径来获得弯角)。 The first background (the white one) gets clipped to the padding-box, which means it takes up all the space of the content第一个背景(白色的)被剪裁到填充框,这意味着它占据了内容的所有空间

  • padding.填充。 Then the second background (the one we want as a border) gets clipped to the edge of the border itself.然后第二个背景(我们想要作为边框的那个)被剪裁到边框本身的边缘。

So our gradient is effectively covered up by a second, white background, apart from the border.所以我们的渐变被第二个白色背景有效地覆盖了,除了边界。 You can therefore adjust the size of the border to whatever thickness you want, and the gradient will fill it.因此,您可以将边框的大小调整为您想要的任何厚度,渐变将填充它。

Original source: https://theadhocracy.co.uk/wrote/gradient-borders-with-rounded-corners-on-input-fields原始来源: https://theadhocracy.co.uk/wrote/gradient-borders-with-rounded-corners-on-input-fields

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

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