Home >Web Front-end >CSS Tutorial >Why Do Two 50% Width Inline-Block Elements Wrap to the Next Line?
When attempting to create two columns with equal 50% width using display:inline-block, it's observed that if the total width of the elements exceeds 99%, the second column wraps to the next line. This behavior may seem counterintuitive.
The reason for this behavior lies in the way display:inline-block interacts with whitespace in HTML. Whitespace, such as line breaks, tabs, and spaces, is collapsed by display:inline-block. This means that when there is whitespace between the inline-block elements, it is treated as a single space and the elements are effectively positioned next to each other.
When the total width of the inline-block elements exceeds 100%, there is no remaining space to accommodate the whitespace. As a result, the second column is forced to wrap to the next line.
To resolve this issue, it is necessary to remove the whitespace between the inline-block elements. This can be achieved by using the following HTML code:
<div>
By concatenating the divs without any whitespace, the display:inline-block elements are effectively placed side by side, without any space between them. This ensures that they wrap correctly within the container's width.
With this adjustment, the two inline-block elements will maintain a 50% width and will not wrap to a second line, even when their total width exceeds 100%.
The above is the detailed content of Why Do Two 50% Width Inline-Block Elements Wrap to the Next Line?. For more information, please follow other related articles on the PHP Chinese website!