Home >Web Front-end >CSS Tutorial >Why are my divs not aligning horizontally and how can I fix it?
Horizontal Alignment of Divs
For some reason, divs are not always aligned horizontally within their container divs, leaving a visually undesirable gap. This issue can arise when using float to position child divs. Consider the following:
<code class="css">.row { width: 100%; margin: 0 auto; } .block { width: 100px; float: left; }</code>
This code aims to position child divs horizontally within the row div. However, sometimes there will be a row with only one child div, resulting in a gap.
To rectify this, an alternative approach is to utilize display: inline-block instead of float. This allows you to achieve horizontal alignment while also maintaining block-like properties, including width and height. Here's the modified CSS:
<code class="css">.block { width: 100px; display: inline-block; }</code>
The above is the detailed content of Why are my divs not aligning horizontally and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!