So how do I make sure that the containers 2 & 3 automatically re-size based on the container 1 ?
I have seen a similar question here. As per this post, it is mentioned that the flexbox and grid are to be considered for this instead of position:absolute. But I couldn’t quite understand how to use those in my scenario. Please help me on how should I approach this ?
Tried to change the parent div dynamically based on the child div which is on top of the parent div.
2
Answers
Based solely on the screenshot, I’d implement this as two elements: 2 and 3 would be a single div with a very large blue top border and a gray background color; the text-containing div would be inside it with a negative top margin to cause it to overlap the blue border:
Normal, unmodified document flow will give you the resizing you want, as the container will naturally resize to fit its contents.
(In general,
position:absolute
is to be avoided precisely because it interferes with the normal document flow; once you start specifying the exact absolute position of elements, you start having to do it for everything else around that element too. Gets ugly fast once you start having to take different screen sizes into account.)If you want to use three elements in particular, do it like this: