< div>中的圆孔 [英] Circular hole in a <div>

查看:91
本文介绍了< div>中的圆孔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来创建一个相对大小的正方形,例如70%x 70%的背景颜色和一个圆孔(以便背后可以看到背景),大小相同所以div的两边是相切的。

解决方案

您可以使用径向渐变来达到此目的。



html,body {height:100%;保证金:0;背景:url(http://www.lorempixel.com/600/400)100%100%;} div {position:relative;宽度:70%;身高:70%;背景:-webkit-radial-gradient(50%50%,circle,transparent 10%,#000 10%);背景:-moz-radial-gradient(50%50%,circle,transparent 10%,#000 10%);背景:径向梯度(50%50%,圆形,透明10%,#000 10%); margin:0 auto;}

< div>< / div>








  body {background:url(http:// www.lorempixel.com/600/400/);背景大小:100%100%; margin:0;}  

< svg viewBox =0 0 400 200width =400height =200> <路径d =M0,0 L400,0 L400,200 L0,200z M200,100 m-50,0 a50,50 0 1,0 100,0 a50,50 0 1,0 -100,0zfill = black/>< / svg>

I am looking for a way to create a square div of relative size, say, 70% x 70% with a background color and a circular hole in it (so that the background can be seen behind it) of the same size so that the sides of the div are tangent.

解决方案

You could use radial-gradient to achieve this.

html,
body {
  height: 100%;
  margin: 0;
  background: url(http://www.lorempixel.com/600/400) 100% 100%;
}
div {
  position: relative;
  width: 70%;
  height: 70%;
  background: -webkit-radial-gradient(50% 50%, circle, transparent 10%, #000 10%);
  background: -moz-radial-gradient(50% 50%, circle, transparent 10%, #000 10%);
  background: radial-gradient(50% 50%, circle, transparent 10%, #000 10%);
  margin: 0 auto;
}

<div></div>


Here's an svg approach.

body {
  background: url(http://www.lorempixel.com/600/400/);
  background-size: 100% 100%;
  margin: 0;
}

<svg viewBox="0 0 400 200" width="400" height="200">
  <path d="M0,0 L400,0 L400,200 L0,200z M200,100 m-50,0 a50,50 0 1,0 100,0 a50,50 0 1,0 -100,0z" fill="black" />
</svg>

这篇关于&lt; div&gt;中的圆孔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆