html - Box-Shadow 需要出现在所有玩家面前

标签 html css box-shadow

我创建了一个可滚动的元素列表。该列表在顶部有一个标题部分,并使用覆盖在列表顶部的框阴影。当我向列表项添加突出显示悬停效果时,列表项突出显示在框阴影的前面。有没有办法让盒子阴影始终显示在前面?

示例提供: https://codepen.io/jwaugh3/pen/WNbopGX

```
<div class="gameList">
          <div class="gameListTitle">
            <h1>Title Block</h1>
            <h2>Box-Shadow Below</h2>
          </div>
        <div class="scroller">
          <div class="force-overflow">
          <div class="gameTile">
            <h class="gameTileText gameTimeGrid">*GameTime*</h>
            <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
            <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
            <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
            <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
            <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
          </div>
          <div class="gameTileSpacer"></div>
    </div>
</div>
```


.gameList {
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 2;
  grid-row-end: 4;
  background-color: #6B6B6B;
  border-radius: 10px;
}

.gameListTitle {
  z-index: 1;
  border-radius: 10px 10px 0px 0px;
  background-color: #6B6B6B;
  color: white;
  padding: 10px;
  box-shadow: 0px 10px 6px -1px rgba(0,0,0,0.58);
}

.scroller {
  overflow-y: scroll;
  height: 85%;
  margin-right: 2px;
  width: 100%;
}

::-webkit-scrollbar {
  margin-top: 10px;
    width: 12px;
    background-color: rgb(0,0,0,0);
}

::-webkit-scrollbar-track {
  margin-top: 10px;
  margin-bottom: 10px;
  background-color: rgb(0,0,0,0);
  box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
  border-radius: 10px;
}

::-webkit-scrollbar-thumb {
  margin-top: 10px;
  margin-bottom: 10px;
  background: white;
  box-shadow: inset 0 0 6px rgba(0,0,0,.3);
  border-radius: 10px;
}

.force-overflow {
  margin-right: 3px; /*margin added to right of list for scrollbar overlap*/
}

.gameTile {
  padding: 10px;
  display: grid;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-columns: 1fr 1fr;
}

.gameTile:hover {
  background-color: #A0A0A0;
  z-index: 1;
}

.gameTileText {
  font-size: 1rem;
  font-weight: bold;
  color: white;
  padding: 3px;
  vertical-align: middle;
}

.teamIcon {
  max-width: 25px;
}

.gameTimeGrid {
  grid-row-start: 1;
  grid-row-end: 2;
  grid-column-start: 1;
  grid-column-end: 2;
}

.tournamentNameGrid {
  grid-row-start: 1;
  grid-row-end: 2;
  grid-column-start: 2;
  grid-column-end: 3;
}

.teamOneGrid {
  grid-row-start: 2;
  grid-row-end: 3;
  grid-column-start: 1;
  grid-column-end: 2;
}

.teamOneScoreGrid {
  grid-row-start: 2;
  grid-row-end: 3;
  grid-column-start: 2;
  grid-column-end: 3;
  text-align: center;
}

.teamTwoGrid {
  grid-row-start: 3;
  grid-row-end: 4;
  grid-column-start: 1;
  grid-column-end: 2;
}

.teamTwoScoreGrid {
  grid-row-start: 3;
  grid-row-end: 4;
  grid-column-start: 2;
  grid-column-end: 3;
  text-align: center;
}

.gameTileSpacer {
  width: 100%;
  height: 3px;
  background-color: #2D2D2D;
}
```

最佳答案

您正在使用 CSS Grid,您需要做的就是将 display:grid 添加到 .gameList 中,这似乎丢失了?

.gameList {
  grid-column-start: 1;
  grid-column-end: 2;
  grid-row-start: 2;
  grid-row-end: 4;
  background-color: #6B6B6B;
  border-radius: 10px;
  
  display: grid /* Added */
}

/* z-index :1 */
.gameListTitle {
  z-index: 1;
  border-radius: 10px 10px 0px 0px;
  background-color: #6B6B6B;
  color: white;
  padding: 10px;
  box-shadow: 0px 10px 6px -1px rgba(0, 0, 0, 0.58);
}

/* no z-index or lower than .gameListTitle z-index*/
.scroller {
  overflow-y: scroll;
  height: 85%;
  margin-right: 2px;
  width: 100%;
}

::-webkit-scrollbar {
  margin-top: 10px;
  width: 12px;
  background-color: rgb(0, 0, 0, 0);
}

::-webkit-scrollbar-track {
  margin-top: 10px;
  margin-bottom: 10px;
  background-color: rgb(0, 0, 0, 0);
  box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  border-radius: 10px;
}

::-webkit-scrollbar-thumb {
  margin-top: 10px;
  margin-bottom: 10px;
  background: white;
  box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
  border-radius: 10px;
}

.force-overflow {
  margin-right: 3px;
  /*margin added to right of list for scrollbar overlap*/
}

.gameTile {
  padding: 10px;
  display: grid;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-columns: 1fr 1fr;
}

.gameTile:hover {
  background-color: #A0A0A0;
  z-index: 1;
}

.gameTileText {
  font-size: 1rem;
  font-weight: bold;
  color: white;
  padding: 3px;
  vertical-align: middle;
}

.teamIcon {
  max-width: 25px;
}

.gameTimeGrid {
  grid-row-start: 1;
  grid-row-end: 2;
  grid-column-start: 1;
  grid-column-end: 2;
}

.tournamentNameGrid {
  grid-row-start: 1;
  grid-row-end: 2;
  grid-column-start: 2;
  grid-column-end: 3;
}

.teamOneGrid {
  grid-row-start: 2;
  grid-row-end: 3;
  grid-column-start: 1;
  grid-column-end: 2;
}

.teamOneScoreGrid {
  grid-row-start: 2;
  grid-row-end: 3;
  grid-column-start: 2;
  grid-column-end: 3;
  text-align: center;
}

.teamTwoGrid {
  grid-row-start: 3;
  grid-row-end: 4;
  grid-column-start: 1;
  grid-column-end: 2;
}

.teamTwoScoreGrid {
  grid-row-start: 3;
  grid-row-end: 4;
  grid-column-start: 2;
  grid-column-end: 3;
  text-align: center;
}

.gameTileSpacer {
  width: 100%;
  height: 3px;
  background-color: #2D2D2D;
}
<div class="gameList">
  <div class="gameListTitle">
    <h1>Title Block</h1>
    <h2>Box-Shadow Below</h2>
  </div>
  <div class="scroller">
    <div class="force-overflow">
      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

      <div class="gameTile">
        <h class="gameTileText gameTimeGrid">*GameTime*</h>
        <h class="gameTileText tournamentNameGrid">*TournamentName, Year*</h>
        <h class="gameTileText teamOneGrid"><img src="../resources/Griffinlogo_square.png" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamOneScoreGrid">*SeriesScore*</h>
        <h class="gameTileText teamTwoGrid"><img src="../resources/IG.jpg" class="teamIcon">*TeamName*</h>
        <h class="gameTileText teamTwoScoreGrid">*SeriesScore*</h>
      </div>
      <div class="gameTileSpacer"></div>

    </div>
  </div>
</div>

为什么有效?

The z-index property sets the z-order of a positioned element and its descendants or flex items.

定位元素:是position属性设置为静态以外的值的元素

Flex item: 是 flex 容器 display:flex; 或 grid 容器 display:grid; 的子元素

>

现在您在 .gameListTitle 上的 z-index:1 将生效。

关于html - Box-Shadow 需要出现在所有玩家面前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362064/

相关文章:

javascript - 从 Service Worker 获取主页 URL

javascript - 在IE8中替换 "find"进行优化?

jquery - owl-carousel slider on drag and dot click 不工作

css - 将 'div' 垂直居中,高度为 'div'

html - 父div溢出隐藏时如何设置框阴影底部?

html - 更改子项的背景颜色会删除父项上的框阴影

javascript - CSS 技巧或 javascript

html - 这些 css 可折叠 div 默认打开了吗?

html - Django 中的内联 CSS 是否可以避免?

CSS "inverse border-radius"在元素的边界框外部创建手机凹口设计