透明空心或镂空圆形
是否可以仅使用 CSS 来切出空心圆?
我们都可以这样做:
但是我们可以这样做吗?
圆必须是空心且透明的。因此,通过在 div
上放置纯色圆圈并不能解决问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以仅使用 CSS 来切出空心圆?
我们都可以这样做:
但是我们可以这样做吗?
圆必须是空心且透明的。因此,通过在 div
上放置纯色圆圈并不能解决问题。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
您可以使用 2 种不同的技术实现透明剪切圆:
1.SVG
以下示例使用 内联 svg。第一个片段使用 mask 元素 来剪切透明圆圈,第二个空心圆圈由 路径元素。该圆是用 2 个 arc 命令 制作的:
使用 mask 元素:
只有一个路径元素:
在这种情况下使用 SVG 的主要优点是:
2.CSS 仅使用 BOX-SHADOWS 使用
overflow:hidden;< 创建一个 div /code> 及其内部带有边框半径的圆形伪元素。给它一个巨大的盒子阴影并且没有背景:
浏览器对 box-shadows 的支持是 IE9+,请参阅 canIuse
相同的方法将使用边框而不是盒子阴影。如果您需要支持不支持框阴影的浏览器(例如 IE8),这很有趣。技术是相同的,但您需要补偿顶部和左侧的值以保持圆位于 div 的中心:
You can achieve a transparent cut out circle with 2 different techniques :
1.SVG
The following examples use an inline svg. The first snippet uses the mask element to cut out the transparent circle and the second hollow circle is made with a path element. The circle is made with 2 arc commands :
With the mask element :
With one path element :
The main advantages of using SVG in this case are :
2.CSS only using BOX-SHADOWS
Create a div with
overflow:hidden;
and a round pseudo element inside it with border-radius. Give it a huge box-shadow and no background :Browser support for box-shadows is IE9+ see canIuse
The same approach would be to use border instead of box-shadows. It is interesting if you need to support browsers that don't support box-shadows like IE8. The technique is the same but you need to compensate with the top and left values to keep the circle in the center of the div :
可以使用径向渐变背景和指针事件来完成(允许鼠标通过圆形层进行交互,例如文本选择)。这是演示页面和屏幕截图:
这将是它的代码:
It can be done using a radial gradient background and pointer-events (to allow mouse interaction behind through the circle layer, e.g. text selection). Here's a demo page and a screenshot:
And this would be the code for it:
参考 web-tiki 的答案,我想补充一点,您始终可以使用
translate(-50%,-50%)
将 div 居中,因此使用border
-属性,具有更好的浏览器支持。通过这种技术,你可以发挥真正的创意:
Referring to web-tiki's answer I'd like to add that you can always center a div with
translate(-50%,-50%)
, so it'd be no problem to use theborder
-property, which has better browser support.You can get really creative with this technique:
关于“Pius Nyakoojo”中的“方法1”,只需稍加改进(见下文)即可。我个人认为这是最简单的解决方案:
Regarding "Method 1" from "Pius Nyakoojo", with a minor improvement (see below) it would work. I personally think this is the simplest solution:
我们可以使用
radial-gradient
和mask
来做到这一点。对于单个 div,没有伪元素。圆半径也可以是百分比值:
另一个想法:
撤销:
We can do this with
radial-gradient
andmask
. With single div, no pseudo-element.Circle radius can be percent value also:
Another idea:
Reverse:
方法 1 - 首选
方法 2
Method 1- Preferred
Method 2
您可以使用 css 遮罩和额外的 svg 图像来完成此操作。
虽然浏览器支持很弱
You can do it using css masks and extra svg image.
Although browser support is weak