你能在CSS中设置边框不透明度吗?
有没有一种直接的 CSS 方法可以使元素的边框像这样半透明?
border-opacity: 0.7;
如果没有,有人知道我如何在不使用图像的情况下做到这一点吗?
Is there a straight forward CSS way to make the border of an element semi-transparent with something like this?
border-opacity: 0.7;
If not, does anyone have an idea how I could do so without using images?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
不幸的是,
opacity
属性使整个元素(包括任何文本)半透明。使边框半透明的最佳方法是使用 rgba 颜色格式。例如,这将给出不透明度为 50% 的红色边框:对于不支持 rgba 的极旧浏览器(IE8 及更早版本),解决方案是提供两个边框声明。第一个是假的不透明度,第二个是真实的。如果浏览器有能力,它将使用第二个,如果没有,它将使用第一个。
第一个边框声明的颜色将与白色背景上 50% 不透明的红色边框的颜色相同(尽管边框下的任何图形都不会渗透)。
我在上面的示例中添加了
background-clip: padding-box;
,以确保即使应用纯色背景色,边框也保持透明。Unfortunately the
opacity
property makes the whole element (including any text) semi-transparent. The best way to make the border semi-transparent is with the rgba color format. For example, this would give a red border with 50% opacity:For extremely old browsers that don't support rgba (IE8 and older), the solution is to provide two border declarations. The first with a fake opacity, and the second with the actual. If a browser is capable, it will use the second, if not, it will use the first.
The first border declaration will be the equivalent color to a 50% opaque red border over a white background (although any graphics under the border will not bleed through).
I've added
background-clip: padding-box;
to the examples above to ensure the border remains transparent even if a solid background color is applied.这很简单,使用偏移量为 0 的实心阴影:
此外,如果为元素设置边框半径,它会为您提供漂亮的圆形边框
jsFiddle 演示
It's easy, use a solid shadow with 0 offset:
Also, if you set a border-radius to the element, it gives you pretty rounded borders
jsFiddle Demo
正如其他人提到的,CSS3 支持 rgba(...) 语法来指定具有不透明度 (alpha) 值的边框颜色。
这里有一个快速的 JSFiddle 演示(如果您想查看的话)。
它适用于 Safari 和 Chrome(可能适用于所有 webkit 浏览器)。
它适用于 Firefox
我怀疑它是否可以在 IE 中运行,但我怀疑有一些过滤器或行为可以使其运行。
还有 CSS RGBA 边框/背景 alpha double,这表明了一些其他问题 -即,边框呈现在您指定的任何背景颜色(或背景图像)之上;因此在许多情况下限制了 border alpha 的用处。
As others have mentioned, CSS3 supports the
rgba(...)
syntax to specify a border color with an opacity (alpha) value.Here's a quick JSFiddle demo if you'd like to check it.
It works in Safari and Chrome (probably works in all webkit browsers).
It works in Firefox
I doubt that it works at all in IE, but I suspect that there is some filter or behavior that will make it work.
There's also CSS RGBA border / background alpha double, which suggests some other issues—namely, that the border renders on-top-of any background color (or background image) that you've specified; thus limiting the usefulness of border alpha in many cases.
如果您使用 W3C 验证器检查您的 CSS 编码,您将看到您的 CSS 代码是否可以接受,即使它在主要浏览器中工作。
如上所述,通过 CSS 创建透明边框
不被 W3C 标准接受,甚至 CSS3 也不接受。我使用直接输入验证器和以下 CSS 代码,
结果是,
不幸的是,W3C 尚未接受 alpha 值(“rgb”末尾的字母“a”)作为边框颜色值的一部分。我确实想知道为什么它没有标准化,因为它适用于所有浏览器。唯一的问题是您是想坚持 W3C 标准还是放弃它而用 CSS 创建一些东西。
使用W3C在线CSS验证器/直接输入。
使用验证器来检查您的工作始终是一个好主意,当您在数小时的编码工作后斗鸡眼时,它确实有助于发现编码中的小甚至大错误。
If you check your CSS coding with W3C validator, you will see if your CSS code is acceptable, even if it worked in the major browsers.
Creating a transparent border via CSS, as written above,
is not accepted by W3C standards, not even for CSS3. I used the direct input validator with the following CSS code,
The results were,
Unfortunate that the alpha value (the letter "a" at the end of "rgb") is not accepted by W3C as part of the border color values as yet. I do wonder why it is not standardized, since it works in all browsers. The only hitch is whether you want to stick to W3C standards or step aside from it to create something in CSS.
To use W3C online CSS validator / Direct Input.
Always a good idea to use a validator to check your work, it really helps finding small or even large errors in coding when your going cross-eyed after hours of coding work.
*据我所知,在这种情况下我通常不会做的是在下面创建一个更大尺寸的块((bordersize * 2)+originalsize)并使其透明,使用
这里是一个示例
更新:
这个答案已经过时了,因为毕竟这个问题已经有 8 年多了。如今所有最新的浏览器都支持 rgba、盒子阴影等。但这是八年前的一个很好的例子。
*Not as far as i know there isn't what i do normally in this kind of circumstances is create a block beneath with a bigger size((bordersize*2)+originalsize) and make it transparent using
here is an example
Update:
This answer is outdated, since after all this question is more than 8 years old. Today all up to date browsers support rgba, box shadows and so on. But this is a decent example how it was 8+ years ago.
作为可能在某些情况下工作的替代解决方案:将
边框样式
更改为点式
。在前景色和背景色之间具有交替的像素组与部分透明像素的连续线不同。另一方面,这需要显着减少的 CSS,并且在每个浏览器之间的兼容性更高,无需任何特定于浏览器的指令。
As an alternate solution that may work in some cases: change the
border-style
todotted
.Having alternating groups of pixels between the foreground color and the background color isn't the same as a continuous line of partially transparent pixels. On the other hand, this requires significantly less CSS and it is much more compatible across every browser without any browser-specific directives.
人们还可以考虑其他边框样式(
dashed
、dotted
)以使边框部分完全透明:http://jsfiddle.net/c1rvp3ga
One may also consider other border styles (
dashed
,dotted
) to make the border partly fully transparent:http://jsfiddle.net/c1rvp3ga
其他答案涉及边框不透明度问题的技术方面,而我想提出一个 hack(仅限纯 CSS 和 HTML)。基本上创建一个容器 div,有一个边框 div,然后是内容 div。
然后CSS:(将内容边框设置为无,注意定位以考虑边框厚度)
Other answers deal with the technical aspect of the border-opacity issue, while I'd like to present a hack(pure CSS and HTML only). Basically create a container div, having a border div and then the content div.
And then the CSS:(set content border to none, take care of positioning such that border thickness is accounted for)
跳出框框思考一下:如果您正在使用 SVG 基本形状 ,您可以使用
lines
和lines-width
的组合 (浏览器支持 > 97%) 和中风不透明度
(浏览器支持> 99%)基本上与OP要求的内容相同。例如,此声明:
将在
周围添加一个半透明的蓝色光环,并使用#000
填充。这个 JSFiddle 提供了几种 SVG 基本形状的演示。小提琴使用红色的填充和蓝色的描边来突出显示描边和边框之间的主要区别 - 一半
lines-width
位于基本形状的周界内。这给出了“双边框”外观(每个基本形状从外部到中心:蓝色->(蓝色+红色=紫色)->红色),这是使用 CSS
border
单独(但是border
+outline
可以 - 请参阅上面的 JSFiddle 以获取使用的示例),并且很难使用
radial-gradient()
实现(在径向基本形状的情况下)。Thinking outside the box a bit here: if you are working with SVG basic shapes, you can use a combination of
stroke
,stroke-width
(browser support > 97%), andstroke-opacity
(browser support > 99%) to do essentially the same thing the OP is asking.For example, this declaration:
will add a translucent blue halo around the perimeter of a
<circle>
with#000
fill.This JSFiddle provides a demo for several SVG basic shapes. The fiddle uses a red
fill
and bluestroke
to highlight a major difference here betweenstroke
andborder
- half thestroke-width
is inside the perimeter of the basic shape.This gives a "double border" appearance (from outside towards the center for each basic shape: blue->(blue + red = purple)->red) that would be impossible to achieve with CSS
border
alone (butborder
+outline
could - see the above JSFiddle for an example using a<div>
), and difficult to achieve (in the case of radial basic shapes) with aradial-gradient()
.试试这个:
我们神奇的 CSS 来了。
查看演示。
try this:
And here comes our magical CSS..
Check out the Demo here.
由于这个答案位于 Google 结果的顶部,因此决定为像我这样的新手发布更新的(2021)答案。
您可以使用 rgba 颜色设置边框不透明度。
请参阅此处的示例小提琴 - http://jsfiddle.net/joshdane/74pybasm/33/
您可以使用它,并享受它。
有一些讨论称旧版浏览器不支持 rgba,但大多数人都支持不再使用旧版浏览器。如果您刚刚学习,不必担心支持旧版浏览器。
Since this answer is top of Google results, decided to post an updated (2021) answer for the rookies like me.
You can set a border opacity buy using an rgba color.
See example fiddle here - http://jsfiddle.net/joshdane/74pybasm/33/
You can use it, and enjoy it.
There is some discussion that older browsers don't support rgba, but most people are not using older browsers anymore. If you are just learning, don't worry about supporting older browsers.