html - 无法创建网页布局设计

标签 html css web bootstrap-5 web-frontend

我无法创建仅使用 HTML 和 CSS 的网络布局。请在这里帮助我

我的代码:

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

section {
  min-height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fddcc356;
}

.container {
  width: 90%;
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.img_part {
  width: 40%;
  height: 250px;
  background: url(images/hat.jpg) no-repeat center/cover;
  border-radius: 8px;
  position: relative;
}

.text_part {
  width: 50%;
  min-height: 550px;
  background-color: black;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30px;
  border-radius: 8px;
  color: white;
  margin-left: -20px;
}

.text_part h1 {
  font-size: 40px;
  font-weight: lighter;
}

.text_part p {
  margin: 20px 0;
  line-height: 25px;
  font-weight: 500;
}

.text_part a {
  text-decoration: none;
  color: black;
  padding: 20px 30px;
  letter-spacing: 2px;
  display: inline-block;
  text-transform: uppercase;
  background-color: white;
  position: absolute;
}

.behind_part {
  min-width: 25%;
  min-height: 100px;
  background-color: brown;
  position: absolute;
  border: 2px solid red;
  margin-right: 350px;
  margin-top: -350px;
}
<section>
  <div class="container">
    <div class="img_part"></div>
    <div class="text_part">
      <div class="content">
        <h1>French girl</h1>
        <h1>Summer Outfit</h1>
        <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt.
        </p>
        <a href="#" class="btn">Learn More</a>

      </div>
    </div>
    <div class="behind_part"></div>

  </div>
</section>

我的输出:enter image description here

实际输出:enter image description here

问题:这里的主要问题是我无法将棕色容器移到图像后面,因为它完全覆盖了我的图像。我想在图像后面显示棕色容器,这样它就不会覆盖图像的表面。 虽然我设法完成了 90% 的任务,但我该如何解决这个问题。

最佳答案

CSS 中的 z-index 属性指定元素如何相互重叠。

The z-index CSS property sets the z-order of a positioned element and its descendants or flex items. Overlapping elements with a larger z-index cover those with a smaller one.

MDN Docs

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

section {
  min-height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fddcc356;
}

.container {
  width: 90%;
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.img_part {
  width: 40%;
  height: 250px;
  background: url(https://via.placeholder.com/200) no-repeat center/cover;
  border-radius: 8px;
  position: relative;
}

.text_part {
  width: 50%;
  min-height: 550px;
  background-color: black;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30px;
  border-radius: 8px;
  color: white;
  margin-left: -20px;
}

.text_part h1 {
  font-size: 40px;
  font-weight: lighter;
}

.text_part p {
  margin: 20px 0;
  line-height: 25px;
  font-weight: 500;
}

.text_part a {
  text-decoration: none;
  color: black;
  padding: 20px 30px;
  letter-spacing: 2px;
  display: inline-block;
  text-transform: uppercase;
  background-color: white;
  position: absolute;
}

.behind_part {
  min-width: 25%;
  min-height: 150px;
  background-color: brown;
  position: absolute;
  border: 2px solid red;
  margin-right: 350px;
  margin-top: -350px;
  
  z-index: -999;
}
<section>
  <div class="container">
    <div class="img_part"></div>
    <div class="text_part">
      <div class="content">
        <h1>French girl</h1>
        <h1>Summer Outfit</h1>
        <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt.
        </p>
        <a href="#" class="btn">Learn More</a>

      </div>
    </div>
    <div class="behind_part"></div>

  </div>
</section>

在图像中添加z-index: -999; 会使其向后移动。

关于html - 无法创建网页布局设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68782994/

相关文章:

javascript - 通过将鼠标悬停在图像上来滚动 div 的内容

html - 如何在页面中心顶部显示一个div

javascript - 如何设置Dojo按钮全宽

javascript - 在 JQuery 中通过 anchor 时进行检测

html - CSS 不适用于响应式网站

html - 为什么当浏览器大小改变时我的列不调整大小

java - Android Filter类performFiltering方法

java - 如何设置java Spring服务器的运行时间?

html - 无法在 CSS 中导入字体

jquery - 添加或删除动态内容时,jScrollPane 不会保持其位置 (jQuery)