-1

I want to show a circle in webpage at some X and Y and then show title text just above the circle.

What I want is - Center of the text should match with center of circle vertically(Y).

Even when the length of text is increased at run-time, the text should still be centered.

Circle dia and text length are variable.

I tried to use text-align center, but could not achieve.

html -

<div id="par">
    <div id="txt">Some Text</div>
    <div id="circ"></div>
</div>

css -

#par{
    text-align: center;
}

#txt{
    background-color: red;
}

#circ {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background-color: green;
}
Gayan S. Muthukumarana
  • 1,182
  • 1
  • 7
  • 24
Mukund
  • 19
  • 3

2 Answers2

1

You should use display: flex; flex-direction: column; justify-content: center; for the wrapper element.

Ilya Novojilov
  • 851
  • 6
  • 12
0

Maybe this example can help you:

#par{
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 300px;
}

#txt{
    display: inline-block;
    text-align: center;
    background-color: red;
}

#circ {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background-color: green;
}
<div id="par">
    <div id="txt">Some Text</div>
    <div id="circ"></div>
</div>

<br>
<br>

<div id="par">
    <div id="txt">Some Text Some Text Some Text Some Text Some Text</div>
    <div id="circ"></div>
</div>
kmgt
  • 5,826
  • 4
  • 18
  • 41