无法单击z索引为负的文本框



我在屏幕中间有一个文本框,但当我点击它时,没有注册任何点击。我认为这是一个CSS Z索引问题。如何诊断和解决此问题?

Js报价

div#container {
    z-index:-1;
    position:relative;
    height:300px;
    width:300px;
    margin:0 auto;
    background-color: #E9E9E9;
    box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.25);
    padding-top: 8px;
}
h2 {
    text-align:center;
}
#container input[type="submit"] {
    position:absolute;
    bottom:0;
    left:0;
    height:50px;
    width:100%;
}
#container input[type="text"], #container input[type="password"] {
    position:absolute;
    outline:0;
    border:none;
    padding:0;
    margin:0;
    height:50px;
    top:50px;
    width:100%;
}
#container input[type="password"] {
    top:150px;
}
.overlay {
    position:absolute;
    top:0;
    left:0;
    z-index:2;
    height:100%;
    width:100%;
    background:gray;
    text-align:center;
    line-height:300px;
    box-shadow:inset 0 120px 0 0 darkgray, inset 0 122px 0 0 gray, inset 0 124px 0 0 darkgray, inset 0 -120px 0 0 darkgray, inset 0 -122px 0 0 gray, inset 0 -124px 0 0 darkgray;
}
body:hover .overlay {
    display:none;
}
<div id="container">
     <h2>LOGIN</h2>
    <input type="text" id="name" placeholder="Enter your Name" />
    <input type="password" id="password" placeholder="Enter your Password" />
    <input type="submit" id="submit" value="Login" />
    <div class="overlay">LOGIN HERE</div>
</div>

我的问题是,我实际上无法点击输入。我试过摆弄布局,但似乎无法获得"可点击"的输入。

有人能解释一下这个问题是如何解决的吗?

Z-index应该在将实际元素层叠在其他元素之上/之后时使用,并且不应该为输入提供负值,尤其是当您的body元素(我相信)将默认为0时。

这意味着,如果将值设置为0以下(即负值),则意味着您的元素将被放置在主体后面。

因此,如果这是一个输入元素,你实际上不能点击它,因为"你会点击主体",而不是实际的输入。


在极少数情况下,您会在输入上放置z索引(大多数情况下,实际上不需要声明z索引),而不是设置负值,请尝试使用低正值。这样,它应该在身体上方(除非已经更改)。

下面是一个简短的例子:

body {
  background: tomato;
}
input {
  z-index: -2;
  position: relative;
}
<input type="text" placeholder="i'm unclickable!" />


通过使用正值(0或更大),您可以选择文本框

body{
  background:tomato;
  }
input{
  position:relative;
  z-index:0;
  }
<input type="text" placeholder="i'm now clickable!"/>

在上面的示例中,您最终可以完全删除z索引。

有关z-index属性的更多信息,请参阅其文档


JsFiddle解决方案

问题在于div#container元素的z-index属性被赋值为-1。

将元素的z索引设置为更高的值,如1000等。

div#container {
    background-color: #E9E9E9;
    box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.25);
    clear: left;
    height: 75px;
    margin: 0 auto 13px;
    overflow: auto;
    padding-top: 8px;
    position: relative;
    text-align: center;
    width: 510px;
    z-index: 1000;
}

最新更新