当包裹在另一个 div 中时,Divs 不会并排浮动吗?

Divs does't float next to each other when wrapped inside another div?

我正在尝试制作一个网站,主要内容在左侧,其他内容在右侧。两个 div 包裹在另一个 div 中,使页面居中并在两侧留下一些白色 space。然而右边的 div 总是在左边的下方,即使右边的显然还有空间。我知道这个问题很常见,但我尝试了很多解决方案,例如 display:inline-block,但它根本不起作用。这是我的页面现在的样子:https://hongweichen0.github.io/

body {
  font-family: "Roboto", sans-serif;
  margin: 0px;
  padding: 0px;
  background-color: rgb(220, 240, 230);
}

.banner {
  text-align: center;
  background: rgb(20, 16, 16);
  position: fixed;
  top: 0px;
  width: 100%;
}

.banner h1 {
  color: rgb(255, 255, 255);
  font-weight: bold;
  font-style: italic;
}

.centerPage {
  background-color: rgb(255, 249, 249);
  width: 80%;
  margin: auto;
  margin-top: 60px;
}

.content {
  float: left;
  width: 70%;
  padding: 20px;
  border: 5px solid black;
}

.contentRight {
  width: 10%;
}

.clear {
  width: 100%;
  line-height: 0px;
  clear: both;
}

.content p,
.content ul {
  width: 100%;
  line-height: 1.5em;
  text-align: justify;
}

.content h2 {
  width: 100%;
  border-bottom: 2px solid black
}

.content img {
  float: left;
  margin: 10px;
  margin-top: 0px;
  clear: both;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Hong Wei Chen's Website</title>
  <link rel="stylesheet" type="text/css" href="main.css" />
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>

<body>
  <div class="banner">
    <h1>Hong Wei Chen</h1>
  </div>
  <div class="centerPage">
    <div class="content">
      <h2>Introduction</h2>
      <p>
        My name is Hong Wei Chen. I'm currently enrolled in Midwood High School as a Junior. I created this personal website for fun just to show off what I learned about HTML/CSS from KhanAcademy. Still pretty new to web design and I plan to learn more about
        how to implement Javascript into websites. There's still a lot for me to learn but I plan to keep on going and become a badass programmer. My ultimate goal is to become expert in both front-end and back-end development. Now, let me show off as
        much as I can. No one will visit this site anyway.
      </p>
      <h2>Useless Info (not personal)</h2>
      <ul>
        <li>First Name: Hong Wei</li>
        <li>Last Name: Chen</li>
        <li>Age: 16</li>
        <li>Favorite Food: EGGS!!!</li>
        <li>Favorite Movie: "Groundhog Day", "Interstellar"</li>
        <li>Cats or Dogs: Dogs</li>
        <li>Favorite Companies: SpaceX, Google, Tesla</li>
        <li>Favorite Color: Blue</li>
      </ul>
      <h2>My Incredible Works</h2>
      <img src="Abstract%20Fish.PNG" alt="Abstract Fish. A random fish painter program." width="200">
      <p>This is an intro to Javascript project I made on Khan Academy during my early stages of learning. The idea is that when the user clicks on the canvas, a fish with random size and color will be drawn on the position of the cursor. This lesson is
        about functions and how to call them with parameters. In this case, the parameter is the x and y position, which is provided by the user's mouse click. This worked out pretty well and I even took a screen shot of it and used it as my avatar for
        many websites. It's a piece of art made with Processing JS.</p>
      <img src="Shooting%20Star.PNG" alt="Shooting Star. A shooting star and explosion animation" width="200">
      <p>This one is also from the Intro to Javascript course on Khan Academy. I love their free courses, probably the best ones out there. Anyway, this is actually an animation using the draw function. I did this on the second day I started this course
        and it took me a solid 2 hours to finish. I went way pass the expectation and added a bunch of stuffs. At the end I was saying to myself, "hey, this is fun". I finally realized how amazing and fun programming can be.</p>
    </div>
    <div class="clear"></div>
    <div class="contentRight">
      <h1>Cool Buttons!</h1>
    </div>
  </div>
  <script type="text/javascript" src=script.js></script>
</body>

</html>

为什么他们之间有div.clear?当我删除它时,它起作用了。 Clear 指定在哪一侧是不允许浮动的浮动元素。此外,您可以为此使用 flexbox。

您可以使用flexbox来处理左右容器的定位。请参阅下面的代码片段。

body {
  font-family: "Roboto", sans-serif;
  margin: 0px;
  padding: 0px;
  background-color: rgb(220, 240, 230);
}

.banner {
  text-align: center;
  background: rgb(20, 16, 16);
  position: fixed;
  top: 0px;
  width: 100%;
}

.banner h1 {
  color: rgb(255, 255, 255);
  font-weight: bold;
  font-style: italic;
}

.centerPage {
  background-color: rgb(255, 249, 249);
  width: 80%;
  margin: auto;
  margin-top: 60px;
  display: flex;
  flex-direction: row;
}

.content {
  flex: 1 1 auto;
  padding: 20px;
  border: 5px solid black;
}

.contentRight {
  flex: 0 0 80px;
  padding: 20px 0 0 0;
}

.clear {
  width: 100%;
  line-height: 0px;
  clear: both;
}

.content p,
.content ul {
  width: 100%;
  line-height: 1.5em;
  text-align: justify;
}

.content h2 {
  width: 100%;
  border-bottom: 2px solid black
}

.content img {
  float: left;
  margin: 10px;
  margin-top: 0px;
  clear: both;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Hong Wei Chen's Website</title>
  <link rel="stylesheet" type="text/css" href="main.css" />
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>

<body>
  <div class="banner">
    <h1>Hong Wei Chen</h1>
  </div>
  <div class="centerPage">
    <div class="content">
      <h2>Introduction</h2>
      <p>
        My name is Hong Wei Chen. I'm currently enrolled in Midwood High School as a Junior. I created this personal website for fun just to show off what I learned about HTML/CSS from KhanAcademy. Still pretty new to web design and I plan to learn more about
        how to implement Javascript into websites. There's still a lot for me to learn but I plan to keep on going and become a badass programmer. My ultimate goal is to become expert in both front-end and back-end development. Now, let me show off as
        much as I can. No one will visit this site anyway.
      </p>
      <h2>Useless Info (not personal)</h2>
      <ul>
        <li>First Name: Hong Wei</li>
        <li>Last Name: Chen</li>
        <li>Age: 16</li>
        <li>Favorite Food: EGGS!!!</li>
        <li>Favorite Movie: "Groundhog Day", "Interstellar"</li>
        <li>Cats or Dogs: Dogs</li>
        <li>Favorite Companies: SpaceX, Google, Tesla</li>
        <li>Favorite Color: Blue</li>
      </ul>
      <h2>My Incredible Works</h2>
      <img src="Abstract%20Fish.PNG" alt="Abstract Fish. A random fish painter program." width="200">
      <p>This is an intro to Javascript project I made on Khan Academy during my early stages of learning. The idea is that when the user clicks on the canvas, a fish with random size and color will be drawn on the position of the cursor. This lesson is
        about functions and how to call them with parameters. In this case, the parameter is the x and y position, which is provided by the user's mouse click. This worked out pretty well and I even took a screen shot of it and used it as my avatar for
        many websites. It's a piece of art made with Processing JS.</p>
      <img src="Shooting%20Star.PNG" alt="Shooting Star. A shooting star and explosion animation" width="200">
      <p>This one is also from the Intro to Javascript course on Khan Academy. I love their free courses, probably the best ones out there. Anyway, this is actually an animation using the draw function. I did this on the second day I started this course
        and it took me a solid 2 hours to finish. I went way pass the expectation and added a bunch of stuffs. At the end I was saying to myself, "hey, this is fun". I finally realized how amazing and fun programming can be.</p>
    </div>
    <div class="clear"></div>
    <div class="contentRight">
      <h1>Cool Buttons!</h1>
    </div>
  </div>
  <script type="text/javascript" src=script.js></script>
</body>

</html>