如何将 div 一个放在另一个下面?

How do I place divs one under the other?

这就是我想要做的:

顶部的导航栏是固定的,因此它会在您滚动时向下移动,

那我想让第一个div在导航div的正下方。这个 div 将覆盖 90% 的屏幕(10% 用于导航栏)并且只包含一个图像。

在此之下,是 div,我将在其中插入主要的 content/text。如果您明白我的意思,当您向下滚动时它也应该占据 100% 的屏幕?

终于在 div 中添加了一个小页脚栏。

看看我的代码,您会发现它非常基础,我想保持这种方式。我只需要 div 一个一个地放在另一个下面。

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  width: 100%;
}

body {
  padding: 0;
  margin: 0;
  border: 0;
  background-color: grey;
  background-attachment: fixed;
  background-size: 100% auto;
}

ul#horizontal-list {
  list-style: none;
}

ul#horizontal-list li {
  display: inline;
}

ul {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

li {
  float: center;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 16px;
  text-decoration: none;
}

li a:hover {
  background-color: red;
}

.navbar {
  position: relative;
  /*height: 10%;*/
  width: 100%;
  background-color: black;
  color: white;
  text-align: center;
}

.navbar ul {
  display: flex;
  align-items: center;
  justify-content: center;
  list-style-type: none;
  margin-top: 0px;
}

.header {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("img/background.jpg");
  background-color: grey;
}

.content {
  position: absolute;
  height: 100%;
  width: 100%;
  background-color: white;
}

.footer {
  height: 50px;
  width: 100%;
  background-color: yellow;
}
<!DOCTYPE html>

<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="stylesheet.css">
  <title>John's Work</title>
  <meta name="description" content="My Personal Portfolio">
</head>

<body>


  <div class="navbar">

    <ul>
      <li><a href="index.html">HOME</a></li>
      <li><a href="about.html">ABOUT</a></li>
      <li><a href="contact.html">CONTACT</a></li>
    </ul>

  </div>


  <div class="header">

  </div>

  <div class="content">
    lorem ipsum text
  </div>

  <div class="footer">
    Copyright by
  </div>




</body>

</html>

这让你满意吗(实际上我不明白你想要什么但假设是这样的)

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  width: 100%;
}

body {
  padding: 0;
  margin: 0;
  border: 0;
  background-color: grey;
  background-attachment: fixed;
  background-size: 100% auto;
}

ul#horizontal-list {
  list-style: none;
}

ul#horizontal-list li {
  display: inline;
}

ul {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

li {
  float: center;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 16px;
  text-decoration: none;
}

li a:hover {
  background-color: red;
}

.navbar {
    position: fixed;
    height: 50px;
    top: 0px;
    width: 100%;
    background-color: black;
    color: white;
    text-align: center;
    z-index: 1;
}

.navbar ul {
  display: flex;
  align-items: center;
  justify-content: center;
  list-style-type: none;
  margin-top: 0px;
}

.header {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("img/background.jpg");
  background-color: grey;
}

.content {
  position: absolute;
      margin-top: 50px;
  height: 100%;
  width: 100%;
  background-color: white;
}

.footer {
  height: 50px;
  width: 100%;
  background-color: yellow;
}
<div class="navbar">

    <ul>
      <li><a href="index.html">HOME</a></li>
      <li><a href="about.html">ABOUT</a></li>
      <li><a href="contact.html">CONTACT</a></li>
    </ul>

  </div>


  <div class="header">

  </div>

  <div class="content">
    lorem ipsum text
  </div>

  <div class="footer">
    Copyright by
  </div>

删除了所有 div 的所有 position:absolute 属性。希望这有帮助

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  width: 100%;
}
body {
  padding: 0;
  margin: 0;
  border: 0;
  background-color: grey;
  background-attachment: fixed;
  background-size: 100% auto;
}
ul#horizontal-list {
  list-style: none;
}
ul#horizontal-list li {
  display: inline;
}
ul {
  margin: 0;
  padding: 0;
  overflow: hidden;
}
li {
  float: center;
}
li a {
  display: block;
  color: white;
  text-align: center;
  padding: 16px;
  text-decoration: none;
}
li a:hover {
  background-color: red;
}
.navbar {
  position: fixed;
    top: 0;
    height: 50px;
    width: 100%;
    background-color: black;
    color: white;
    text-align: center;
    left: 0;
    right: 0;
    z-index: 1;
}
.navbar ul {
  display: flex;
  align-items: center;
  justify-content: center;
  list-style-type: none;
  margin-top: 0px;
}
.header {
  width: 100%;
  height: 100%;
  background-image: url("img/background.jpg");
  background-color: grey;
}
.content {
  height: 100%;
    width: 100%;
    background-color: red;
}
.footer {
  height: 50px;
  width: 100%;
  background-color: yellow;
}
<!DOCTYPE html>

<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="stylesheet.css">
  <title>John's Work</title>
  <meta name="description" content="My Personal Portfolio">
</head>
<body>
  <div class="navbar">
    <ul>
      <li><a href="index.html">HOME</a></li>
      <li><a href="about.html">ABOUT</a></li>
      <li><a href="contact.html">CONTACT</a></li>
    </ul>
  </div>
  <div class="header"></div>
  <div class="content">lorem ipsum text</div>
  <div class="footer">Copyright by</div>
</body>
</html>

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  width: 100%;
}

body {
  padding: 0;
  margin: 0;
  border: 0;
  background-color: grey;
  background-attachment: fixed;
  background-size: 100% auto;
}

ul#horizontal-list {
  list-style: none;
}

ul#horizontal-list li {
  display: inline;
}

ul {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

li {
  float: center;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 16px;
  text-decoration: none;
}

li a:hover {
  background-color: red;
}
.holder{
    height: 100%;
    width: 100%;
}
.navbar {
    position: fixed;
    top: 0;
    height: 10%;
    width: 100%;
    background-color: black;
    color: white;
    text-align: center;
    left: 0;
    right: 0;
    z-index: 1;
}

.navbar ul {
  display: flex;
  align-items: center;
  justify-content: center;
  list-style-type: none;
  margin-top: 0px;
}

.header {
  width: 100%;
  height: 90%;
  background-image: url("img/background.jpg");
  background-color: grey;

}

.content {
    height: 100%;
    width: 100%;
    background-color: red;
}

.footer {
  height: 50px;
  width: 100%;
  background-color: orange;
}
<!DOCTYPE html>

<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="stylesheet.css">
  <title>John's Work</title>
  <meta name="description" content="My Personal Portfolio">

</head>

<body>

<div class="holder">
  <div class="navbar">

    <ul>
      <li><a href="index.html">HOME</a></li>
      <li><a href="about.html">ABOUT</a></li>
      <li><a href="contact.html">CONTACT</a></li>
    </ul>

  </div>


  <div class="header">

  </div>
</div>
  <div class="content">
    lorem ipsum text
  </div>

  <div class="footer">
    Copyright by
  </div>




</body>

</html>