简体   繁体   English

是否可以有透明的边框?

[英]Is it possible to have a transparent border?

I have this code for my border: 我的边框有这个代码:

border: 1px solid #CCC;

Is it possible to have some similar CSS where the border has a width but it's not visible. 是否有可能有一些类似的CSS,其中边框有宽度但不可见。 In other words if there's a blue background then that would show right through the border? 换句话说,如果有蓝色背景,那么就会显示出边界?

A margin occupies space and is transparent. 保证金占据空间并且是透明的。 The space a margin occupies is on the outside of the element, so it takes the background color of the parent element. 边距占据的空间位于元素的外部,因此它采用父元素的背景颜色。 If you want it to be space with the same background color as the element you are modifying, you would want to use padding. 如果您希望它是与您正在修改的元素具有相同背景颜色的空间,则需要使用填充。

This is a great resource that shows you what you will probably want: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model 这是一个很好的资源,可以向您展示您可能需要的内容: https//developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model

您只需将border-color设置为transparent

Yes, it's entirely possible. 是的,这完全有可能。

Normally, a transparent border would show the background color of the element beneath it. 通常,透明边框会显示其下方元素的背景颜色。

This can be overcome by restricting the background to the "box" holding the padding & content using background-clip:padding-box (or optionally content-box which would clip the background to the content area only). 这可以通过使用background-clip:padding-box (或者可选地仅将内容剪辑到内容区域的内容content-box将背景限制为保持填充和内容的“框”来克服。

Then you can add a boxshadow (for instance) which will sit outside the transparent border. 然后你可以添加一个位于透明边框之外的boxshadow(例如)。

 body { background: linear-gradient(to bottom, orange, pink); } div { width: 200px; height: 200px; box-sizing: border-box; background: rebeccapurple; margin: 2em auto; border: 10px solid transparent; box-shadow: 0 0 5px 5px green; background-clip: padding-box; } 
 <div></div> 

Yes it is possible to have transparent border. 是的,可以有透明的边框。 Just use rgba color defination like so: 只需使用rgba颜色定义,如下所示:

border: 10px solid rgba(50,50,50,.5);

The last value 0.5 goes from 0 to 1 and is a opacity (or alpha) value for the color 最后一个值0.5从0变为1,并且是颜色的不透明度(或alpha)值

Working example (see how the border transparency changes on hover): jsFiddle 工作示例(查看悬停时边框透明度如何变化): jsFiddle

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

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