-1

Recently discovered that a relative positioned element inside a container with "min-height" defined but no "height" can't be positioned vertically, only horizontally. ¿Am I wrong? ¿Is there a reason for that? ¿Workarounds? Edit: The case is a % min-height; Here is the example:

<div style="position:relative; width:100%; min-height:100%; background-color:pink;">
        <div style="position:relative; top:30%; left:30%; width:700px; height:500px; background-color:yellow;"></div>

The "top:30%" is ignored

1 Answers1

-1

Yes you can, you can use flexbox to center your relative div inside of your container.

<div class="container">
  <div class="center"></div>
</div>

.container {
  min-height: 300px;
  background-color: red;
  display: flex;
}

.center {
  width: 100px;
  height: 100px;
  background-color: blue;
  margin: auto;
  position: relative;
}
Roy
  • 23
  • 5
  • Doesn't work for me, I guess it's the % min-height that is the problem, but can't imagine why – user6846020 Oct 24 '18 at 09:28
  • My answer is a workaround I think you should use to accomplish what you're trying to do. If you're trying to get "top: 30%" to be included, you should make the centered div "position: absolute" and the container div "position: relative". Also: Your container div is 100% min-height. So it'll adapt to the content. – Roy Oct 24 '18 at 09:33
  • I need the centered div to be "relative" for other purpose, if it could be "absolute" it would work fine. – user6846020 Oct 24 '18 at 09:38