Div не плавает рядом друг с другом, когда находится внутри другого div? - PullRequest
0 голосов
/ 29 августа 2018

Я пытаюсь сделать сайт, где основной контент находится слева, а разные вещи справа. Два элемента div обернуты внутри другого элемента div, который центрирует страницу и оставляет некоторое пустое пространство с обеих сторон. Тем не менее, правый 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>

Ответы [ 2 ]

0 голосов
/ 29 августа 2018

Вы можете использовать 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>
0 голосов
/ 29 августа 2018

Почему у вас есть div.clear между ними? Когда я его убрал, все работает. Clear указывает, на какой стороне плавающие элементы не могут плавать. Также для этого вы можете использовать flexbox.

...