Skip to content Skip to sidebar Skip to footer

How Do I Vertically Center A Circle On A Line?

I'm trying to center a circle vertically in the middle of a horizontal line, but using a negative margin doesn't seem to work. How should I approach this?

Solution 1:

This should work:

margin-left: calc(50% - 25px);

You can read about css calc here: https://developer.mozilla.org/en-US/docs/Web/CSS/calc

Solution 2:

Use absolute positioning and a transform.

#line {
  width: 100%;
  height: 5px;
  background: gray;
  position: relative;
}

#circle {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background: blue;
  text-align: center;
  color: white;
  font-weight: 700;
  line-height: 50px;
  font-size: 14px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<divid="Value">0.33</div><divid="Result"></div><divid="line"><divid="circle">
    2
  </div></div>

Solution 3:

Please check whether you are looking for this?

#line {
  width: 100%;
  /* 2 */height: 5px;
  background: gray;
  position: relative;
}

#circle {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background: blue;
  margin-left: -25px;
  text-align: center;
  color: white;
  font-weight: 700;
  line-height: 50px;
  font-size: 14px;
  position: absolute;
  left: 50%;
  bottom: 0;
}
<divid="Value">0.33</div><divid="Result"></div><divid="line"><divid="circle">
    2
  </div></div>

If you want to move the circle to the top of the line, adjust bottom: 0;

Solution 4:

Using position:absolute and change margin-left:calc(50% - 25px) and margin-top:-22.5px for center vertically and horizontally of line (hardcoded width and height circle)

#line {
  width: 100%;
  /* 2 */height: 5px;
  background: gray;
  position:relative;
}

#circle {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background: blue;
  margin-left: calc(50% - 25px);
  text-align: center;
  color: white;
  font-weight: 700;
  line-height: 50px;
  font-size: 14px;
  position:absolute;
  margin-top:-22.5px;
}
<divid="Value">0.33</div><divid="Result"></div><divid="line"><divid="circle">
    2
  </div></div>

Post a Comment for "How Do I Vertically Center A Circle On A Line?"