css - 如何让一个带有 z-index 的 div 在悬停时覆盖另一个 div

标签 css hover position z-index

我有一个容器 div,里面有几个较小的 div:s,它们都带有 float:left;。如果我将鼠标悬停在较小的 div:s 之一上,则 heightwidth 应该增加并且它应该覆盖另一个 div:s 不移动它们。

HTML:

 <div id="boxcontainer">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
 </div>

CSS:

.box {
  float:left;
  position: relative;
  width:150px;
  height:150px;
  margin:5px;
  background-color: yellow;
}

#boxcontainer {
  position: relative;
  width:500px;
  height:auto;
}

.box:hover {
  z-index:100;
  width:300px;
  height:auto;
}

我怎样才能做到这一点?

最佳答案

z-index 仅适用于定位元素 (position:absolute、position:relative或位置:固定)

尝试绝对位置而不是向左浮动。

我在每个盒子周围添加了一个容器,并将每个元素绝对定位在其中。通过这种方式,您可以根据需要添加任意数量的框并保持同一类。

示例

HTML

<div id="boxcontainer">
    <div class="box">
        <div class="Inside"></div> 
    </div>         
    <div class="box">
        <div class="Inside"></div> 
    </div>         
    <div class="box">
        <div class="Inside"></div> 
    </div>     
</div>

CSS

#boxcontainer{
    position: relative;
    width:500px;
    height:500px;
}
.box{
    position:relative;
    float:left;
    width:150px;
    height:150px;
    margin:5px;
}
.Inside{    
    background:green;
    position:absolute;
    top:0;
    left:0;
    width:150px;
    height:150px;
    transition:all 0.5s ease-in-out;
    -webkit-transition:all 0.2s ease-in-out;
}
.Inside:hover{
    z-index:100;
    width:250px;
    height:250px;
    background:#666666;
}

http://jsfiddle.net/JJ3v4/3/

关于css - 如何让一个带有 z-index 的 div 在悬停时覆盖另一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793462/

相关文章:

html - 用自定义符号替换列表元素符号(右对齐)

html - Bootstrap Navbar 在窗口变小时扩展

javascript - 带有背景覆盖的随机背景?

css - Html/CSS 消失的 div 背景

jquery - 禁用 :hover effect when unchecking a checkbox

Javascript 破坏 CSS 悬停

ios - 选择日期时如何获取日期单元格的位置,ios

javascript - 元素的位置

html - CSS 位置 : fixed

CSS 悬停背景更改不适用于 Safari