带CSS的半圆(仅边框和轮廓)

qzlgjiam  于 2023-01-10  发布在  其他
关注(0)|答案(6)|浏览(340)

我尝试用CSS创建一个圆圈,它看起来就像下面的图片:

...只有一个div

<div class="myCircle"></div>

并且只使用CSS**定义。不允许SVG,WebGL,DirectX,[...]。
我试过用另一个div画一个完整的圆,然后淡化它的一半,它确实起作用了,但我正在寻找一个更优雅的替代方案。

yfwxisqw

yfwxisqw1#

您可以使用border-top-left-radiusborder-top-right-radius属性,根据框的高度(和添加的边框)将框的角圆化。
然后在框的顶部/右侧/左侧添加边框以达到效果。
给你:

.half-circle {
    width: 200px;
    height: 100px; /* as the half of the width */
    background-color: gold;
    border-top-left-radius: 110px;  /* 100px of height + 10px of border */
    border-top-right-radius: 110px; /* 100px of height + 10px of border */
    border: 10px solid gray;
    border-bottom: 0;
}

或者,您可以将box-sizing: border-box添加到框中,以便计算框的宽度/高度(包括边框和填充)。

.half-circle {
    width: 200px;
    height: 100px; /* as the half of the width */
    border-top-left-radius: 100px;
    border-top-right-radius: 100px;
    border: 10px solid gray;
    border-bottom: 0;

    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
xuo3flqw

xuo3flqw2#

不久前我也遇到过类似的问题,我就是这样解决的

.rotated-half-circle {
  /* Create the circle */
  width: 40px;
  height: 40px;
  border: 10px solid black;
  border-radius: 50%;
  /* Halve the circle */
  border-bottom-color: transparent;
  border-left-color: transparent;
  /* Rotate the circle */
  transform: rotate(-45deg);
}
<div class="rotated-half-circle"></div>
krugob8w

krugob8w3#

下面是一个最小的代码来实现这个效果。
由于border-radius以百分比表示,因此这也可以响应。

.semi-circle{
width: 200px;
height: 100px;
border-radius: 50% 50% 0 0 / 100% 100% 0 0;
border: 10px solid #000;
border-bottom: 0;
}
<div class="semi-circle"></div>
c86crjj0

c86crjj04#

我用百分比法来实现

border: 3px solid rgb(1, 1, 1);
        border-top-left-radius: 100% 200%;
        border-top-right-radius: 100% 200%;
5fjcxozz

5fjcxozz5#

为半圆添加边框并删除下边框

#semi-ring{
  height: 100px;
  /* width = 2* height */
  width: 200px;  
  border: 30px solid black;
  /* border-radius = height + border */
  border-radius: 130px 130px 0 0;
  border-bottom: transparent;
}
<div id="semi-ring"></div>
7rfyedvj

7rfyedvj6#

试试这个:

.top {
  height: 30px;
  width: 30px * 2;
  border-top-left-radius: 30px * 2;
  border-top-right-radius: 30px * 2;
}
<div class="top"></div>

相关问题