如何在CSS中制作渐变边框
我正在尝试将渐变应用于边框,我认为这就像这样做一样简单:
border-color: -moz-linear-gradient(top, #555555, #111111);
但这不起作用。
有谁知道边框渐变的正确方法是什么?
I'm trying to apply a gradient to a border, I thought it was as simple as doing this:
border-color: -moz-linear-gradient(top, #555555, #111111);
But this does not work.
Does anyone know what is the correct way to do border gradients?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
border-image 属性可以完成此任务。您还需要指定
border-style
和border-width
。详细了解 MDN。
The
border-image
property can accomplish this. You'll need to specifyborder-style
andborder-width
too.Read more on MDN.
我会使用背景渐变和填充来代替边框。外观相同,但更容易。
一个简单的例子:
正如 Walter Schwarz 指出的那样,您还可以利用
::before
选择器:Instead of borders, I would use background gradients and padding. Same look, but much easier.
A simple example:
You can also leverage the
::before
selector as Walter Schwarz pointed out:border-image-slice 将扩展 CSS border-image 渐变
这(据我所知)可以防止将“图像”默认切片为多个部分 - 没有它,如果边框位于一个部分上,则不会出现任何内容仅侧面,如果它围绕整个元素,则每个角会出现四个微小的渐变。
border-image-slice
will extend a CSS border-image gradientThis (as I understand it) prevents the default slicing of the "image" into sections - without it, nothing appears if the border is on one side only, and if it's around the entire element four tiny gradients appear in each corner.
— https://developer.mozilla.org/en/CSS/-moz-线性渐变
— http://www.cssportal.com/css3-preview /边框.htm
— https://developer.mozilla.org/en/CSS/-moz-linear-gradient
— http://www.cssportal.com/css3-preview/borders.htm
您可以在不删除边框半径的情况下实现此目的 background, < a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip" rel="noreferrer">background-clip 和 背景来源:
基本上,这会将白色背景定位在渐变背景上,从内边框剪切白色背景,并从外边框剪切渐变背景。这就是为什么您需要将边框定义为
实心透明
。归功于此开发帖子中的方法 2。
You can achieve this without removing the border radius with background, background-clip, and background-origin:
Basically, this positions the white background over the gradient background, clips the white background from the inner border, and clips the gradient background from the outer border. This is why you need to define the border as
solid transparent
.Credit to Method 2 from this dev.to post.
试试这个,在 web-kit 上工作得很好
Try this, works fine on web-kit
这是一个技巧,但在某些情况下,您可以通过使用背景图像指定渐变,然后使用框阴影遮盖实际背景来实现此效果。例如:
来自: http://blog.nateps.com/the-elusive- css-边框-渐变
It's a hack, but you can achieve this effect in some cases by using the background-image to specify the gradient and then masking the actual background with a box-shadow. For example:
From: http://blog.nateps.com/the-elusive-css-border-gradient
尝试下面的例子:
Try the below example:
试试这个,它对我有用。
链接是小提琴
https://jsfiddle.net/yash009/kayjqve3/1/
Try this, it worked for me.
The link is to the fiddle
https://jsfiddle.net/yash009/kayjqve3/1/
Webkit 支持边框渐变,现在接受 Mozilla 格式的渐变。
Firefox 声称以两种方式支持渐变:
IE9不支持。
Webkit supports gradients in borders, and now accepts the gradient in the Mozilla format.
Firefox claims to support gradients in two ways:
IE9 has no support.
我同意萨伊蒙的观点。他和昆汀的答案的唯一问题是跨浏览器兼容性。
HTML:
CSS:
I agree with szajmon. The only problem with his and Quentin's answers is cross-browser compatibility.
HTML:
CSS:
的渐变边框示例
使用 border-image css 属性
归功于:Mozilla 中的边框图像
Example for Gradient Border
Using border-image css property
Credits to : border-image in Mozilla
实现相同效果的另一个技巧是利用多个背景图像,IE9+、较新的 Firefox 和大多数基于 WebKit 的浏览器都支持该功能:http://caniuse.com/#feat=multibackgrounds
在 IE6-8 中还有一些使用多个背景的选项:http://www.beyondhyper.com/css3-multiple-backgrounds-in-non-supportive-browsers/
例如,假设您想要一个 5 像素宽的左边框,它是从蓝色到白色的线性渐变。将渐变创建为图像并导出为 PNG。列出左边框渐变背景之后的所有其他 CSS 背景:
您可以通过更改
背景
速记属性。这是给定示例的 jsFiddle: http://jsfiddle.net/jLnDt/
Another hack for achieving the same effect is to utilize multiple background images, a feature that is supported in IE9+, newish Firefox, and most WebKit-based browsers: http://caniuse.com/#feat=multibackgrounds
There are also some options for using multiple backgrounds in IE6-8: http://www.beyondhyper.com/css3-multiple-backgrounds-in-non-supportive-browsers/
For example, suppose you want a 5px-wide left border that is a linear gradient from blue to white. Create the gradient as an image and export to a PNG. List any other CSS backgrounds after the one for the left border gradient:
You can adapt this technique to top, right, and bottom border gradients by changing the background position part of the
background
shorthand property.Here is a jsFiddle for the given example: http://jsfiddle.net/jLnDt/
这是一个具有透明背景的渐变边框,可与 border-radius 一起使用
现场演示: https://jsfiddle.net /jbernier/0eypxc74/1/
Here's a gradient border with transparent background that works with border-radius
Live demo: https://jsfiddle.net/jbernier/0eypxc74/1/
对于跨浏览器支持,您还可以尝试使用
:before
或:after
伪元素模仿渐变边框,具体取决于您想要执行的操作。For cross-browser support you can try as well imitate a gradient border with
:before
or:after
pseudo elements, depends on what you want to do.由于我们在使用边框图像时无法添加边框半径,因此我能够通过使用 @NFTMaster 建议的技巧来解决该问题,如下所示:
Since we cannot add border-radius when we use the border-image, I was able to solve the problem by using a trick as suggest by @NFTMaster as following:
我发现使用 tailwind 效果很好(将其添加到全局 index.css 中):
然后在您需要的元素上使用渐变边框
这相当于:
i found this to be working well using tailwind (added this to global index.css):
then use the gradient-border on the element you need
this is equivalent to:
尝试这个代码
或者参考这个小提琴: http://jsfiddle.net/necolas/vqnk9/
try this code
or maybe refer to this fiddle: http://jsfiddle.net/necolas/vqnk9/
这是一种很好的半跨浏览器方式,可以使渐变边框向下淡出一半。只需将 color-stop 设置为
rgba(0, 0, 0, 0)
用法说明:
更多信息请参见:https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
Here's a nice semi cross-browser way to have gradient borders that fade out half way down. Simply by setting the color-stop to
rgba(0, 0, 0, 0)
Usage explained:
More here: https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
使用 HTML 和CSS
使用 Tailwind CSS
Using HTML & CSS
Using Tailwind CSS
这里有一篇关于此的很好的 css 技巧文章:https://css-tricks。 com/gradient-borders-in-css/
我能够使用多个背景和 background-origin 属性想出一个非常简单的单一元素解决方案。
这种方法的优点是:
检查一下: https://codepen.io/AlexOverbeck/pen/axGQyv?editors=1100
There is a nice css tricks article about this here: https://css-tricks.com/gradient-borders-in-css/
I was able to come up with a pretty simple, single element, solution to this using multiple backgrounds and the background-origin property.
The nice things about this approach are:
Check it out: https://codepen.io/AlexOverbeck/pen/axGQyv?editors=1100
使用伪元素的按钮边框渐变
Button Border Gradient using pseudo-element
试试这个,它支持透明背景和圆角,但不是那么简洁。
您也可以将其用作背景图像。
Try this which supports transparent background and rounded corner not that concise though.
also you can use it as background image.