Is there a way to prevent the parent from overflowing with the child when the position of the children element is absolute and the childrenβs height is higher than the parent? I do not want to hide the overflow: hidden overflow part; I want the height of the div.alert parent div.alert be no lower than its children. As you can see, I use position:absolute and transform:tranlateY(-50%) to place the contents of the div in the middle regardless of the length of the content. Unfortunately, when content is above the minimum height of the parent, it goes beyond the parent.
<div class="alert"> <span> text text text text text text text text text text text text </span> </div> <div class="alert"> <span> text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text </span> </div> <div class="alert"> <span> text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text </span> </div>
CSS
body{ background: #000; } .alert{ position: relative; margin: 35px 5px; background: #4679BD; color: #fff; padding: 10px 15px; min-height: 40px; } span{ position: absolute; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); }
JS FIDDLE
Thanks in advance.
source share