css如何在三角形内画一个圆
我试图将这个圆圈放在三角形的中心。我将类三角形设置为显示 Flex,但它不起作用。请帮我。
代码:
body {
display: flex;
align-items: center;
justify-content: center;
}
.triangle {
position: relative;
width: 0;
height: 0;
border-left: 300px solid transparent;
border-right: 300px solid transparent;
border-bottom: 300px solid black;
}
.circle {
position: absolute;
width: 200px;
height: 200px;
border-radius: 50%;
background: blue;
}
<div class="triangle">
<div class="circle">
</div>
</div>
I am trying to put this circle in the center of the triangle. I set the class triangle to display flex but it didn't work. Please help me.
The code:
body {
display: flex;
align-items: center;
justify-content: center;
}
.triangle {
position: relative;
width: 0;
height: 0;
border-left: 300px solid transparent;
border-right: 300px solid transparent;
border-bottom: 300px solid black;
}
.circle {
position: absolute;
width: 200px;
height: 200px;
border-radius: 50%;
background: blue;
}
<div class="triangle">
<div class="circle">
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实现此目的的最简单的方法之一(非 SVG)如下所示,并在代码中附有解释性注释:
JS Fiddle 演示。
当然,如果您准备使用 SVG:
JS Fiddle 演示。
SVG 解释起来有点复杂,所以不幸的是,我基本上放弃了这一责任,并在下面的参考书目中留下了一个链接,您(和其他人)可以从中了解更多信息。
还值得一提的是,我自己对 SVG 的了解是有限的,我倾向于使用程序来创建它们,例如 InkScape(当然也可以使用其他程序)或在线生成器,就像我在这里所做的那样。
参考文献:
align-contents
。宽高比
。背景颜色
。clip-path
。显示
。网格区域
。高度
。justify-content
。地点项目
。。
z-index
。参考书目:
One of the easiest – non SVG – ways to achieve this is as below, with explanatory comments in the code:
JS Fiddle demo.
Of course, if you're prepared to use SVG:
JS Fiddle demo.
SVG is a little complex to explain, so unfortunately I've largely abdicated from that responsibility and left a link – in the bibliography below – from which you (and others) can learn more about it.
It's also worth stating that my own knowledge of SVG is limited, and I tend to use a program to create them, such as InkScape (other programs are, of course, available) or an online generator, as I did here.
References:
align-contents
.aspect-ratio
.background-color
.clip-path
.display
.grid-area
.height
.justify-content
.place-items
.<SVG>
.z-index
.Bibliography:
您可以考虑使用一个元素来绘制两种形状:
You can consider one element to draw both shapes:
您必须在 css 中为您的圆类设置顶部、右侧、左侧、底部。
例如:
You must to set top, right, left, bottom for your circle class in css.
For example: