0

UPDATE about duplicated question: it is not the same question since I can not set the height of the containter div.

How to horizontally and vertically align a span element inside a div.

The div is resposive element, so it can takes several sizes. The span has not set height nor the width

<div class="the-div">
   <span class="the-span" >span here</span>
</div>

I tried with:

.the-span{top: 50%; text-align: center}

I don't want to use js for this.

Capture:enter image description here

JPashs
  • 13,044
  • 10
  • 42
  • 65

1 Answers1

2

You can transform its position thusly:

html,
body {
  width: 100%;
  height: 100%;
}
div,
span {
  width: 50%;
  height: 50%;
  border: 1px solid;
  display: block;
  position: relative;
}
span {
  transform: translateY(-50%) translateX(-50%); /* <--offset top/left by half its own height/width */
  left: 50%; /* position half way within containing div */
  top: 50%; /* position half way within containing div */
}
<div>
  <span>
  </span>
</div>
SW4
  • 69,876
  • 20
  • 132
  • 137