Home > Web Front-end > CSS Tutorial > How to Position Two Divs Side by Side Using CSS Floats?

How to Position Two Divs Side by Side Using CSS Floats?

Susan Sarandon
Release: 2025-01-03 05:41:39
Original
791 people have browsed it

How to Position Two Divs Side by Side Using CSS Floats?

How to Position Two Divs Side-by-Side

To place two div elements side-by-side, you can use the float property in CSS.

Within the parent div (#wrapper), set its width and add a border for visual clarity.

#wrapper {
  width: 500px;
  border: 1px solid black;
}
Copy after login

For the first div (#first), give it a fixed width and apply a border:

#first {
  width: 300px;
  border: 1px solid red;
}
Copy after login

To float the first div left, add the following:

#first {
  ...
  float: left;
}
Copy after login

Leave the second div (#second) without specifying a width, but give it a border:

#second {
  border: 1px solid green;
}
Copy after login

To ensure the second div respects the height of the first div, add an overflow property to the wrapper div:

#wrapper {
  ...
  overflow: hidden;
}
Copy after login

Alternatively, you can float both the first and second divs, but remember to set the overflow property on the wrapper div to contain the floated children:

#wrapper {
  ...
  overflow: hidden;
}

#first {
  ...
  float: left;
}

#second {
  ...
  float: left;
}
Copy after login

The above is the detailed content of How to Position Two Divs Side by Side Using CSS Floats?. 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