请看一下代码片段:http://codepen.io/anon/pen/JItLa
我试图显示 2 行 block ,每行包含不同数量的元素。 悬停事件应该显示 CSS 阴影,但是有一个问题:阴影的右边框与下一个 block 重叠。 你会说这里可能的解决方案是使用 display:inline-block,它在 block 之间留下间隙,但我不需要这些间隙。这些 block 应该彼此粘在一起,但右边的阴影应该在悬停时与下一个 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, 255, 190, .75), 0 0 23px 1px #000;
box-shadow: 0 0 2px rgba(255, 255, 190, .75), 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>
这怎么可能?
这里还有另一个问题:当我在 block 之间添加边框时,最后的 block 移动到下一行,这是不正确的。请参阅上面给出的示例中的第 2 行。
最佳答案
添加一个 z-index
悬停在元素上。
此外,该元素还必须按照 z-index
的顺序进行定位。 属性(property)工作。因此也添加 position:relative
。
9.9.1 Specifying the stack level: the 'z-index' property
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
-width: calc(33.3% - 1px)
更改框模型以在元素的宽度计算中包含边框 -
box-sizing
如果您选择使用 box-sizing
,如果您想支持所有浏览器,则需要适当的供应商/前缀。我会使用类似的东西:
.tile2 {
width: 33.3%;
border-left: 1px solid #ddd;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
关于html - CSS Box/shadow 重叠问题 z-index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20208200/