可以将边框半径与具有渐变的边框图像一起使用吗?
我正在设计一个具有圆形边框(边框半径)的输入字段,并尝试向所述边框添加渐变。我可以成功地制作渐变和圆形边框,但是两者都不能一起工作。它要么是没有渐变的圆角,要么是带有渐变但没有圆角的边框。
-webkit-border-radius: 5px;
-webkit-border-image: -webkit-gradient(linear, 0 0, 0 100%, from(#b0bbc4), to(#ced9de)) 1 100%;
无论如何,有没有办法让两个 CSS 属性一起工作,或者这是不可能的?
I'm styling an input field which has a rounded border (border-radius), and attempting to add a gradient to said border. I can successfully make the gradient and the rounded border, however neither work together. It's either rounded with no gradient, or a border with a gradient, but no rounded corners.
-webkit-border-radius: 5px;
-webkit-border-image: -webkit-gradient(linear, 0 0, 0 100%, from(#b0bbc4), to(#ced9de)) 1 100%;
Is there anyway to have both CSS properties work together, or is this not possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
致力于解决同样的问题。遇到了一个非 svg 解决方案,它比这里的其他解决方案更简洁:
这不是我自己的解决方案,取自此处: https://gist.github.com/stereokai/ 36dc0095b9d24ce93b045e2ddc60d7a0
Working on this same problem. Came across a non-svg solution which is more succinct than others here:
This is not my own solution and has been taken from here: https://gist.github.com/stereokai/36dc0095b9d24ce93b045e2ddc60d7a0
这是可能的,并且它不需要额外的标记,但使用
::after
伪元素。sp;
它涉及放置一个伪- 下面带有渐变背景的元素并对其进行剪裁。这适用于所有当前没有供应商前缀或 hack 的浏览器(甚至 IE),但如果您想支持 IE 的复古版本,您应该考虑纯色后备、javascript 和/或自定义 MSIE CSS 扩展(即
过滤器
,CSSPie -像矢量欺骗等)。这是一个实例(jsfiddle 版本):
上面的额外样式是为了显示:
box-shadow
、inset
或不同样,这适用于 IE、Firefox 和 Webkit/Blink 浏览器。
This is possible, and it does not require extra markup, but uses an
::after
pseudo-element.It involves putting a pseudo-element with a gradient background below and clipping that. This works in all current browsers without vendor prefixes or hacks (even IE), but if you want to support vintage versions of IE, you should either consider solid color fallbacks, javascript, and/or custom MSIE CSS extensions (i.e.,
filter
, CSSPie-like vector trickery, etc).Here's a live example (jsfiddle version):
The extra styling above is to show:
box-shadow
,inset
or notAgain, this works with IE, Firefox and Webkit/Blink browsers.
现在我们可以使用 mask 轻松实现这一点,同时具有透明度和响应能力
更多详细信息: https://dev.to/afif/border-with-渐变和半径-387f
Now we can use mask to easily achieve this while having transparency and responsiveness
More details: https://dev.to/afif/border-with-gradient-and-radius-387f
根据 W3C 规范,可能不可能:
这可能是因为
border-image
可以采用一些潜在的复杂模式。如果您想要圆形图像边框,则需要自己创建一个。Probably not possible, as per the W3C spec:
This is likely because
border-image
can take some potentially complicated patterns. If you want a rounded, image border, you'll need to create one yourself.我会为此使用 SVG:
SVG 可以用作单独的文件(首选方式)或类似
background
值的一部分(下面的代码仅适用于 webkit 浏览器):为了在 MS Edge 和 Firefox 中工作,我们应该在
utf8
之后转义我们的标记,因此我们将用单引号'
替换双引号"
,#
与%23
和%
与%25
:I would use SVG for this:
SVG can be used as separate file (preferred way) or like part of value of
background
(code below will work only in webkit-browsers):For this to work in MS Edge and Firefox we should escape our markup after
utf8
, so we will be replacing double quotes"
with single quotes'
,#
with%23
and%
with%25
:对我来说,这个可以快速帮助我,
for me this one help me quickly,
这在 WebKit 中总是对我有用,尽管它有点棘手!
基本上,您只需使边框变大,然后用更大或更小的伪元素边框将其遮盖即可:)。
http://plnkr.co/edit/luO6G95GtxdywZF0Qxf7?p=preview
This always works for me in WebKit, although its a bit tricky!
Basically you just make the border bigger then mask it out with bigger and smaller pseudo-element's borders : ).
http://plnkr.co/edit/luO6G95GtxdywZF0Qxf7?p=preview
透明元素的解决方案:
至少可以在 Firefox 中工作。
实际上我发现了一种没有伪类的方法 - 但它只适用于径向渐变:
获取带有伪类的透明元素我只发现了这种方式 - 好吧,它不是渐变,但它至少是一个彩色条纹边框(看起来像救生圈):
使用 svg (在可变性方面最令人满意,但也需要大多数代码行):
Solutions for transparent elements:
working at least in Firefox.
There is actually one way I found without pseudo classes - but it only works for radial gradients:
Getting a transparent element with pseudo classes I only found this way - ok it is not a gradient, but it is at least a multicolored striped border (looking like life-rings):
with a svg (most satisfying in terms of variability but needs most codelines too):
如果将渐变应用到背景会怎样?然后在里面添加一个额外的 div,将边距设置为旧的边框宽度并使用白色背景,当然还有边框半径。这样你就有了边框的效果,但实际上使用的是正确裁剪的背景。
What if you apply the gradient to the background. Than and add an extra div inside, with margin set to the old border-width and with a white background, and of course a borderradius. That way you have the effect of a border, but are actually using background, which is clipped correctly.
您可以使用 CSS 剪辑路径属性。
当在普通、红色、40x40 图像的
img
元素周围使用时,它看起来像这样。希望这有帮助!
You could use the CSS clip-path property.
Which looks something like this when used around an
img
element of a plain, red, 40x40 image.Hope this helps!
根据这个页面,可以使用「clip-path」 」
According to this page, you can use 「clip-path」
渐变边框的解决方案。
这段代码对我来说工作得很好!
Solution for Gradient Border.
This code work fine for me!