导致问题的相对位置 我的容器在陈列柜下面 为什么会这样?

Relative position causing problem my container go under the showcase Why It's happening?

我遇到了相对位置问题

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  min-height: 100vh;
}

a {
  color: #000;
  text-decoration: none;
}

.container {
  max-width: 1200px;
  width: 80%;
  margin: auto;
}

.d-4 {
  font-size: 2.6rem;
  text-align: center;
  font-weight: 600;
}

.lead {
  font-size: 1.8rem;
  font-weight: 300;
}

.relative {
  position: relative;
  top: 0;
  left: 0;
}

nav {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  display: flex;
  font-size: 17px;
  font-weight: bold;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 10px 0;
}

nav>a {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  font-size: 28px;
  padding: 0 20px;
}

nav ul a {
  padding: 15px;
  margin: 0 5px;
  color: #fff;
  border-radius: 4px;
}

nav ul a:hover {
  background: rgba(0, 0, 0, 0.6);
}

.showcase {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background: url('https://cdn.pixabay.com/photo/2017/06/24/23/03/railway-2439189_1280.jpg') no-repeat center center/cover, rgba(0, 0, 0, 0.5);
  background-blend-mode: darken;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Website | Getting Started</title>
  <!-- My CSS -->
  <link rel="stylesheet" href="css/style.css">
</head>

<body>


  <div class="relative">
    <nav>
      <a href="#"><img height="70" src="img/logo.png" alt="Logo"> &nbsp;Title</a>
      <ul>
        <a href="#">Home</a>
        <a href="#">Login</a>
        <a href="#">SignUp</a>
        <a href="#">Dashboard</a>
      </ul>
    </nav>
    <div class="showcase">
      <h1 class="d-4">Welcome to my Website.</h1>
      <p class="lead">
        Way of heaven is here.
      </p>
    </div>
  </div>

  <div class="container">
    <h1 class="text-center">Contact Us</h1>
    <form id="footer" class="control">
      <input class="input" type="text" id="name" placeholder="Enter your Name">
      <input class="input" type="email" id="email" placeholder="Enter your E-mail">
      <input class="input" type="number" id="mob" placeholder="Enter your phone No.">
      <input class="input" type="textr" id="subject" placeholder="Enter your subject">
      <textarea class="textarea" placeholder="Enter your message"></textarea>
    </form>
  </div>


</body>

</html>

截图

https://drive.google.com/file/d/1hn4-dyx0h8BrY42zAjL4801yCZ_0YYnB/view?usp=drivesdk

您需要将 position:absolute 添加到您的 .container。 CSS 中的位置 属性 是这样工作的:

property-value 对 position:relative 告诉您的布局哪个元素应用作其子元素的参考,这些子元素包含 property-value 对 position:absolute,用于它们的定位.

然后,您可以使用属性 toprightbottomleft 来指定子 HTML 元素的距离position: absolute 对从其第一个父元素的边缘开始(在 HTML 文档中从子元素向上走),它具有 position:relative 对。

在您的 CSS 代码中,您只需将父元素设置为 position:relative,这不会改变布局中的任何内容;它只是告诉您的页面,具有 position:relativeposition:absolute 的元素 X 的任何子元素必须使用该 X 使用四个属性 topright 进行相对定位bottom,以及 left。是的,很明显,只要您将具有 position:relative 的父项的 HTML 子项设置为 position:absolute,它就会被放置在其上方。

需要这样的结果吗?

* {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    body {
      min-height: 100vh;
    }

    a {
      color: #000;
      text-decoration: none;
    }

    .container {
      max-width: 1200px;
      width: 80%;
      margin: auto;
    }

    .d-4 {
      font-size: 2.6rem;
      text-align: center;
      font-weight: 600;
    }

    .lead {
      font-size: 1.8rem;
      font-weight: 300;
    }

    .relative {
      display: flex; /* added */
      height: 100vh; /* added */
      flex-direction: column; /* added */
      position: relative;
      top: 0;
      left: 0;
    }

    nav {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1;
      display: flex;
      font-size: 17px;
      font-weight: bold;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      padding: 10px 0;

    }

    nav > a {
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      font-size: 28px;
      padding: 0 20px;

    }

    nav ul a {
      padding: 15px;
      margin: 0 5px;
      color: #fff;
      border-radius: 4px;
    }

    nav ul a:hover {
      background: rgba(0,0,0,0.6);
    }

    .showcase {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100vh;
      background: url('https://cdn.pixabay.com/photo/2017/06/24/23/03/railway-2439189_1280.jpg') no-repeat center center/cover,rgba(0,0,0,0.5);
      background-blend-mode: darken;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
    }
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Website | Getting Started</title>
  <!-- My CSS -->
  <link rel="stylesheet" href="css/style.css">
</head>
<body>


  <div class="relative">
    <nav>
      <a href="#"><img height="70" src="img/logo.png" alt="Logo"> &nbsp;Title</a>
      <ul>
        <a href="#">Home</a>
        <a href="#">Login</a>
        <a href="#">SignUp</a>
        <a href="#">Dashboard</a>
      </ul>
    </nav>
    <div class="showcase">
      <h1 class="d-4">Welcome to my Website.</h1>
      <p class="lead">
        Way of heaven is here.
      </p>
    </div>
  </div>

  <div class="container">
    <h1 class="text-center">Contact Us</h1>
    <form id="footer" class="control">
      <input class="input" type="text" id="name" placeholder="Enter your Name">
      <input class="input" type="email" id="email" placeholder="Enter your E-mail">
      <input class="input" type="number" id="mob" placeholder="Enter your phone No.">
      <input class="input" type="textr" id="subject" placeholder="Enter your subject">
      <textarea class="textarea" placeholder="Enter your message"></textarea>
    </form>
  </div>


</body>
</html>

要防止 showcasecontainer 重叠,请将 position: relative; 添加到 .container

如果你想让conatiner不与.relative重叠,那么只需在.relative中添加height: 100vh;即可。

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  min-height: 100vh;
}

a {
  color: #000;
  text-decoration: none;
}

.container {
  max-width: 1200px;
  width: 80%;
  position: relative;
  background: red;
  margin: auto;
}

.d-4 {
  font-size: 2.6rem;
  text-align: center;
  font-weight: 600;
}

.lead {
  font-size: 1.8rem;
  font-weight: 300;
}

.relative {
  position: relative;
}

nav {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  display: flex;
  font-size: 17px;
  font-weight: bold;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 10px 0;
}

nav>a {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  font-size: 28px;
  padding: 0 20px;
}

nav ul a {
  padding: 15px;
  margin: 0 5px;
  color: #fff;
  border-radius: 4px;
}

nav ul a:hover {
  background: rgba(0, 0, 0, 0.6);
}

.showcase {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background: url('https://cdn.pixabay.com/photo/2017/06/24/23/03/railway-2439189_1280.jpg') no-repeat center center/cover, rgba(0, 0, 0, 0.5);
  background-blend-mode: darken;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
<div>
  <div class="relative">
    <nav>
      <a href="#"><img height="70" src="img/logo.png" alt="Logo"> &nbsp;Title</a>
      <ul>
        <a href="#">Home</a>
        <a href="#">Login</a>
        <a href="#">SignUp</a>
        <a href="#">Dashboard</a>
      </ul>
    </nav>
    <div class="showcase">
      <h1 class="d-4">Welcome to my Website.</h1>
      <p class="lead">
        Way of heaven is here.
      </p>
    </div>
  </div>

  <div class="container">
    <h1 class="text-center">Contact Us</h1>
    <form id="footer" class="control">
      <input class="input" type="text" id="name" placeholder="Enter your Name">
      <input class="input" type="email" id="email" placeholder="Enter your E-mail">
      <input class="input" type="number" id="mob" placeholder="Enter your phone No.">
      <input class="input" type="textr" id="subject" placeholder="Enter your subject">
      <textarea class="textarea" placeholder="Enter your message"></textarea>
    </form>
  </div>
</div>