How do I draw a semicircle with CSS when the height and width are percentages?

Viewed 459

How do I draw a semicircle with CSS when the height and width are percentages?

.container{
  width: 200px;
  height: 200px;
  background: blue;
  display: flex;
}

.semiCircle{
  position:relative;
  height: 90%;
  width: 45%;
  border-radius: 90% 0 0 90%;
  background: red;
  margin:auto;
}
<div class='container'>
  <div class='semiCircle'></div>
</div>

2 Answers

First, create a circle with border-radius: 100%;. Then, make half of it transparent with background: linear-gradient(90deg, red 50%, transparent 50%);.

.container {
  width: 200px;
  height: 200px;
  background: blue;
  display: flex;
}
.semiCircle {
  height: 90%;
  width: 45%;
  border-radius: 100%;
  margin: auto;
  background: linear-gradient(90deg, red 50%, transparent 50%);
}
<div class='container'>
  <div class='semiCircle'></div>
</div>

In CSS Code, I'm added Css like this:

height: 50%;
width: 100%;
background-color: red;
border-top-left-radius: 110px;
border-top-right-radius: 110px;
transform: rotate(-90deg);

Example:

.container{
  width: 200px;
  height: 200px;
  background: blue;
  display: flex;
}

.semiCircle{
    height: 50%;
  width: 100%;
    background-color: red;
    border-top-left-radius: 110px;
    border-top-right-radius: 110px;
    transform: rotate(-90deg);
    position:relative;
    margin:auto;
}
<div class='container'>
  <div class='semiCircle'></div>
</div>

Related