I have a parent div (display: table) that wraps an unknown number of floating children. Now I want it to always have the width of the child lines.
In other words: if the parent div is wider or narrower than a multiple of its child, the unused space remains in the parent div. “How can I avoid this?”
Is this only possible with css, or do I need JS to calculate the width of the lines inside and provide it to the parent div?
Here's the simplified code:
.wrapper { border: 1px solid black; display:table; width:90%; } .child { float:left; width: 100px; background-color:red; margin: 0 10px 10px 0; }
<div class="wrapper"> <div class="child">Text</div> <div class="child">Text</div> <div class="child">Text</div> <div class="child">Text</div> <div class="child">Text</div> <div class="child">Text</div> </div>
Play around with the width of your browser to see unused empty space on the right side.
source share