I have a parent container that contain some inline child elements and the parent container has dynamic width. It looks great till all child elements are in one line but when you shrink the browser window and a child element go to the next line then it leave extra space on right side in parent container. As you can see the extra space between child and container's border in below image.
.container {
display: inline-block;
border: 5px solid #000;
padding: 10px;
}
.child {
display: inline-block;
background-color: #F00;
width: 100px;
height: 100px;
margin: 5px;
}
<div class="container">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
I wanna know two things:
1: Why don't the parent container's width shrink after an inline child element go to the next line?
2: Is there any workaround to remove that extra space with CSS?
Please note that I used the display: inline-block;
for .child
tags just to demonstrate that but I can also use any css property like flex
or float
to achieve that.