Home >Web Front-end >CSS Tutorial >How to Achieve Horizontal Div Alignment in CSS?

How to Achieve Horizontal Div Alignment in CSS?

DDD
DDDOriginal
2024-11-11 01:33:02646browse

How to Achieve Horizontal Div Alignment in CSS?

Horizontal Div Alignment in CSS

When working with CSS, aligning divs horizontally within a container can present a challenge. By default, floated divs will stack vertically if their combined width exceeds the parent container's width, even if the container's height permits horizontal arrangement.

To achieve the desired horizontal alignment, we can utilize an inner div within the container that is sufficiently wide to accommodate all the floated divs.

#container {
  background-color: red;
  overflow: hidden;
  width: 200px;
}

#inner {
  overflow: hidden;
  width: 2000px;
}

.child {
  float: left;
  background-color: blue;
  width: 50px;
  height: 50px;
}
<div>

In this solution, the #inner div has a width greater than the combined width of the floated #child divs. Therefore, all the floated divs align horizontally within the #container div, resulting in the desired layout.

The above is the detailed content of How to Achieve Horizontal Div Alignment in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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