Home >Web Front-end >CSS Tutorial >Why Do Two 50% Width Inline-Block Elements Not Fit Side-by-Side?
Two Inline-Block Elements with 50% Width Fail to Fit Side by Side in a Row: Why?
When utilizing inline-block elements, there's an inherent issue with whitespace between them (approximately 4px wide). This means that two divs with 50% width each, plus this whitespace, exceed 100% width, causing them to break out of their intended single row.
Example:
body { margin: 0; } div { display: inline-block; width: 50%; } .left { background-color: aqua; } .right { background-color: gold; }
<div class="left">foo</div> <div class="right">bar</div>
Reasons for the Whitespace Issue:
Alternative Solutions:
While setting one div to 49% width can resolve the gap issue, it's not an ideal practice. Alternatives include:
The above is the detailed content of Why Do Two 50% Width Inline-Block Elements Not Fit Side-by-Side?. For more information, please follow other related articles on the PHP Chinese website!