创建CSS半圈

时间:2016-04-27 13:32:23

标签: html css html5 css3 css-shapes

我有一个填充的圆圈,其中包含如下数字:

enter image description here

我如何将其分成两个半圆圈,以便我可以在其中存储两个不同的数字,如下所示:

enter image description here

我的CSS如下:

.oval {
    display: inline-block;
    width: 75px;
    height: 75px;
    border-radius: 50%;
    background: #000000;
    color: white;
    line-height: 75px;
    font-size: medium;
}

4 个答案:

答案 0 :(得分:9)

您可以使用border-radius



.left-half,
.right-half {
  float: left;
  width: 40px;
  height: 80px;
  line-height: 80px;
  color: white;
  text-align: center;
  margin: 0 1px;
  background: black;
}
.left-half:nth-child(1) {
  border-radius: 80px 0 0 80px;
}
.right-half:nth-child(2) {
  border-radius: 0 80px 80px 0;
}

<div class="circle">
  <div class="left-half">21</div>
  <div class="right-half">12</div>
</div>
&#13;
&#13;
&#13;

或者您可以使用 SVG

&#13;
&#13;
.text {
  font-size: 16px;
  fill: white;
}
&#13;
<svg width="105.5px" height="97.874px" viewBox="0 0 105.5 97.874">
  <path d="M50.423,0.609v96.76c-26.72,0-48.38-21.66-48.38-48.38C2.043,22.269,23.703,0.609,50.423,0.609z" />
  <path d="M103.526,49.494c0,26.72-21.66,48.38-48.38,48.38V1.114C81.866,1.114,103.526,22.774,103.526,49.494z" />
  <text transform="matrix(1 0 0 1 20.0771 52.5107)" class="text">21</text>
  <text transform="matrix(1 0 0 1 73.1807 53.0166)" class="text">12</text>
</svg>
&#13;
&#13;
&#13;

更新:您也可以创建内部有两个跨度的圆圈,并在中间添加一行伪类

&#13;
&#13;
.circle {
  width: 70px;
  height: 70px;
  background: black;
  border-radius: 50%;
  display: flex;
  align-items: center;
  color: white;
  position: relative;
}
span {
  flex: 1;
  text-align: center;
}
.circle:before {
  content: "";
  width: 2px;
  position: absolute;
  height: 100%;
  background: white;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
}
&#13;
<div class="circle">
  <span>12</span><span>24</span>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:7)

您只需要在中间添加一行(使用伪元素或使用下面代码段中的渐变),然后使用两个span元素将内容放入其中。

.oval {
  display: inline-block;
  width: 75px;
  height: 75px;
  border-radius: 50%;
  background: #000000;
  color: white;
  line-height: 75px;
  font-size: medium;
  background-image: linear-gradient(to right, transparent calc(50% - 1px), white calc(50% - 1px), white calc(50% + 1px), transparent calc(50% + 1px));
}
span {
  float: left;
  width: 50%;
  line-height: 75px;
  text-align: center;
}
<div class='oval'>
  <span>21</span>
  <span>12</span>
</div>

或者你可以像下面的代码片段那样使用伪元素创建半圆。

.oval {
  position: relative;
  float: left;
  width: 37.5px;
  height: 75px;
  color: white;
  line-height: 75px;
  font-size: medium;
  text-align: center;
  margin: 1px;
  overflow: hidden;
}
.oval:before {
  position: absolute;
  content: '';
  height: 100%;
  width: 200%;
  top: 0px;
  border-radius: 50%;
  background: black;
  z-index: -1;
}
.oval:nth-of-type(1):before {
  left: 0px;
}
.oval:nth-of-type(2):before {
  right: 0px;
}
<div class='oval'>
  21
</div>
<div class='oval'>
  12
</div>

或者您甚至可以使用radial-gradient背景。这只是另一种方法( for fun ),我不推荐这个选项。

.oval {
  float: left;
  width: 37.5px;
  height: 75px;
  color: white;
  line-height: 75px;
  font-size: medium;
  text-align: center;
  margin: 1px;
}
div:nth-of-type(1) {
  background: radial-gradient(circle closest-corner at center right, black 97.5%, transparent 100%);
}
div:nth-of-type(2) {
  background: radial-gradient(circle closest-corner at center left, black 97.5%, transparent 100%);
}
<div class='oval'>21</div>
<div class='oval'>12</div>

答案 2 :(得分:2)

SVG解决方案

这似乎是一个图形问题。 所以我建议使用SVG而不是CSS来创建这样的形状。

svg {
  width: 150px;
}
<svg viewBox="0 0 100 100">
  <path d="M47, 5
           A 45 45, 0, 0, 0, 47 95" fill="Orange"/>
  <path d="M52, 5
           A 45 45, 0, 1, 1, 52 95" fill="Orange"/>
  <text x="12" y="60" fill="black" font-size="25px">25</text>
  <text x="57" y="60" fill="black" font-size="25px">16</text>
</svg>

答案 3 :(得分:1)

&#13;
&#13;
.l {
  display: inline-block;
  width: 40px;
  height: 80px;
  border-top-left-radius: 40px;
  border-bottom-left-radius: 40px;
  background: #000000;
  color: white;
  text-align: center;
  line-height: 80px;
  font-size: 103%;
}
.r {
  display: inline-block;
  width: 40px;
  height: 80px;
  border-top-right-radius: 40px;
  border-bottom-right-radius: 40px;
  background: #000000;
  color: white;
  text-align: center;
  line-height:80px;
  font-size: 103%;
}
&#13;
<div class='l'>x1</div>
<div class='r'>x2</div>
&#13;
&#13;
&#13;