我无法创建仅使用 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>
问题:这里的主要问题是我无法将棕色容器移到图像后面,因为它完全覆盖了我的图像。我想在图像后面显示棕色容器,这样它就不会覆盖图像的表面。 虽然我设法完成了 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.
* {
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/