加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – CSS Box / shadow重叠问题z-index

发布时间:2020-12-14 18:26:13 所属栏目:资源 来源:网络整理
导读:请查看代码段: http://codepen.io/anon/pen/JItLa 我试图连续显示2行具有不同数量项的块. 悬停事件应该显示CSS阴影,但是存在一个问题:阴影的右边界与下一个块重叠. 你会说这里可能的解决方案是使用display:inline-block,它留下了块之间的空隙,但我不需要
请查看代码段: http://codepen.io/anon/pen/JItLa

我试图连续显示2行具有不同数量项的块.
悬停事件应该显示CSS阴影,但是存在一个问题:阴影的右边界与下一个块重叠.
你会说这里可能的解决方案是使用display:inline-block,它留下了块之间的空隙,但我不需要间隙.这些块应该保持彼此粘性,但正确的阴影应该与下一个块悬停重叠.

html,body {
  margin: 0;
  padding: 0
}
.wrapper {
  width: 100%;
  margin-top: 20px
}
.tile,.tile2 {
  float: left;
  background: #f2f2f2
}
.tile {
  width: 25%
}
.tile2 {
  width: 33.3%;
  border-left: 1px solid #ddd
}
.tile:hover,.tile2:hover {
  -webkit-box-shadow: 0 0 2px rgba(255,255,190,.75),0 0 23px 1px #000;
  -moz-box-shadow: 0 0 2px rgba(255,0 0 23px 1px #000;
  box-shadow: 0 0 2px rgba(255,0 0 23px 1px #000
}
.header {
  padding: 20px 0px 10px;
  text-align: center
}
.clear {
  clear: both
}
<div class="wrapper">
  <div class="tile">
    <div class="header">some text</div>
  </div>
  <div class="tile">
    <div class="header">some text</div>
  </div>
  <div class="tile">
    <div class="header">some text</div>
  </div>
  <div class="tile">
    <div class="header">some text</div>
  </div>
  <div class="clear"></div>
</div>
<div class="wrapper">
  <div class="tile2">
    <div class="header">some text</div>
  </div>
  <div class="tile2">
    <div class="header">some text</div>
  </div>
  <div class="tile2">
    <div class="header">some text</div>
  </div>
  <div class="clear"></div>
</div>

怎么可能?

这里还有另一个问题:当我在块之间添加边框时,最后一个块移动到下一行,这是不正常的.请参阅上面给出的示例中的第2行.

解决方法

在悬停时向元素添加 z-index.

此外,还必须定位元素以使z-index属性起作用.因此添加位置:相对.

07002

z-index: Applies to: positioned elements

Each box has a position in three dimensions. In addition to their horizontal and vertical positions,boxes lie along a “z-axis” and are formatted one on top of the other. Z-axis positions are particularly relevant when boxes overlap visually. This section discusses how boxes may be positioned along the z-axis.

Updated Codepen – 它现在有效.

.tile:hover,.tile2:hover {
    z-index: 1;
    position: relative;
}

为了解决您的第二个问题,元素出现在一个新行上,因为它们的宽度不会相加,因为它因边框而关闭1px.

33.3%33.3%33.3%1px!= 100%

你有几个不同的选择:

>使用calc()从宽度中减去1px – 宽度:calc(33.3% – 1px)
>更改框模型以在元素的宽度计算中包含边框 – box-sizing

如果您选择使用box-sizing,则需要适当的供应商/ prexes,如果您需要所有浏览器的支持.我会使用类似的东西:

.tile2 {
    width: 33.3%;
    border-left: 1px solid #ddd;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
}

Updated Codepen使用box-sizing.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读