css 是否可以为元素指定比其父元素更高的z轴索引?[duplicate]

332nm8kg  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(148)
    • 此问题在此处已有答案**:

Why does z-index not work?(10个答案)
Why can't an element with a z-index value cover its child?(5个答案)
2天前关闭。
我有一个堆叠上下文的问题。有四行装饰。这四行应该在导航栏标志和页面内容下面。
这是我的代码片段:

body {
  padding: 0;
  margin: 0;
  background: #333;
}
.navbar{
  padding: 1rem;
  color: white;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 1;
}
.navbar.bg-dark {
  background-color: #333;
}

.navbar-logo {
  font-style: italic;
  font-weight: bold;
  font-size: 24px;
  letter-spacing: 10px;
}

.content {
  padding: 100px 20px;
  height: 600px;
  color: white;
  font-weight: bold;
}

.page-container {
  max-width: 1024px;
  margin: 0 auto;
}

/* Line */
.line {
  width: 100vw;
  left: 100px;
  height: 3px;
  background-color: #5c5c5c;
  position: fixed;
  transform-origin: left;  
  top: -1px;
  z-index: 1;
}
.line.line-1 {
  transform: rotate(27.45deg);
}
.line.line-2 {
  transform: rotate(47deg);
}
.line.line-3 {
  transform: rotate(70deg);
}
.line.line-4 {
  transform: rotate(105deg);
}
<div class="navbar bg-dark">
  <div class="navbar-logo">
    Navbar Logo
  </div>
</div>

<div class="line line-1"></div>
<div class="line line-2"></div>
<div class="line line-3"></div>
<div class="line line-4"></div>

<div class="page-container">
  <div class="content">
  <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
</div>

正如你在演示中看到的,导航条标志被装饰线挡住了。我试图给导航条标志一个更高的z轴,但它不工作,因为它的父母。对于这种问题,什么是最合适的方法?给四行的身体作为背景图像?或者我应该用另一种方法?
这是一张我想在决赛中看到的截图:

7eumitmz

7eumitmz1#

从行类中删除z-index,并将z-index: 1;position: relative;添加到页面容器类中。
这样,徽标和文本将位于行的上方。

body {
  padding: 0;
  margin: 0;
  background: #333;
}
.navbar{
  padding: 1rem;
  color: white;
  position: fixed;
  top: 0;
  height: 30px;
  width: 100%;
  z-index: 2;
  overflow: hidden;
}
.navbar.bg-dark {
  background-color: #333;
}

.navbar-logo {
  position:relative;
  font-style: italic;
  font-weight: bold;
  font-size: 24px;
  letter-spacing: 10px;
  z-index: 3;
}

.content {
  padding: 100px 20px;
  height: 600px;
  color: white;
  font-weight: bold;
}

.page-container {
  position: relative;
  max-width: 1024px;
  margin: 0 auto;
  z-index: 1;
}

/* Line */
.line {
  width: 100vw;
  left: 100px;
  height: 3px;
  background-color: #5c5c5c;
  position: absolute;
  transform-origin: left;  
  top: -1px;
}
.line.line-1 {
  transform: rotate(27.45deg);
}
.line.line-2 {
  transform: rotate(47deg);
}
.line.line-3 {
  transform: rotate(70deg);
}
.line.line-4 {
  transform: rotate(105deg);
}
.background {
  position: fixed;
}
<div class="navbar bg-dark">
  <div class="navbar-logo">
    Navbar Logo
  </div>
  <div class="line line-1"></div>
  <div class="line line-2"></div>
  <div class="line line-3"></div>
  <div class="line line-4"></div>
</div>

<div class="background line line-1"></div>
<div class="background line line-2"></div>
<div class="background line line-3"></div>
<div class="background line line-4"></div>

<div class="page-container">
  <div class="content">
  <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
</div>
    • 编辑**:更改答案,使其看起来像屏幕截图,并使滚动正常工作。

相关问题