Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set a max width to div with children in absolute

I have a root div with a max width and then a container and some children in absolute.

.root {
  background-color: tomato;
  max-width: 400px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  position: relative;
}

.item {
  background-color: steelblue;
  width: 100px;
  height: 100px;
  position: absolute;
}
<div class="root">
    <div class="container ">
        <div class="item" style="top: 0px; left: 0px"></div>
        <div class="item" style="top: 0px; left: 110px"></div>
      <div class="item" style="top: 0px; left: 220px"></div>
        <div class="item" style="top: 110px; left: 0px"></div>
    </div>
</div>

Why the tomato background isn’t visible? Why that div has height = 0?

What I want to achieve is a the root div with a max-width and then some children in absolute position that do not overflow the parent div

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You root doesn’t have height. When you set your items position to absolute you removed element from the normal document flow, and no space is created for the element in the page layout

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading