Home > Web Front-end > CSS Tutorial > Why are my divs not aligning horizontally and how can I fix it?

Why are my divs not aligning horizontally and how can I fix it?

Susan Sarandon
Release: 2024-11-03 04:01:02
Original
1014 people have browsed it

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

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

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!

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