Home > Web Front-end > CSS Tutorial > Why Do Two 50% Width Inline-Block Elements Wrap to the Next Line?

Why Do Two 50% Width Inline-Block Elements Wrap to the Next Line?

Barbara Streisand
Release: 2024-12-05 21:12:10
Original
209 people have browsed it

Why Do Two 50% Width Inline-Block Elements Wrap to the Next Line?

Two Inline-block Elements with 50% Width Breaking to Second 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.

Understanding the Issue

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.

Solution: Removing Whitespace

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template