Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to click textbox with negative z-index

Tags:

css

z-index

I have a textbox in the middle of the screen, but when I click on it, no clicks are registered. I think it's a CSS Z-Index issue. How do I go about diagnosing and fixing this issue?

JsFiddle

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>

My issue is that I can't actually click on the inputs. I have tried playing around with the layout, but I can't seem to get the inputs 'clickable'.

Could someone explain how this issue could be resolved?

like image 279
nkcmr Avatar asked Jun 14 '11 03:06

nkcmr


People also ask

Can you use negative Z index?

You can have negative z-index To place an element on a layer below another one, it just has to have a lower value of z-index but that lower value can be negative. One area where this is useful is when using pseudo elements and wanting to position them behind the content of their parent element.

Why your Z index is not working?

You set z-index on a static element By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

What happens when you assign a negative Z index value to an element?

negative z-index Setting negative z-index (< 0) to an element will stack the element behind its parent. However, if the parent is the root element of a stacking context, <html> creates the default stacking context, a negative z-index will still stack in front of the parent element.


2 Answers

Z-index should be used when layering actual elements on top of/behind other elements, and shouldn't really be given a negative value for inputs - especially when your body element (I believe) will be defaulted to 0.

This means that setting a value below 0 (i.e. a negative value), would mean that your element would be placed behind the body.

So, if this was an input element, you couldn't actually click on it as 'you would be clicking on the body instead', and not actually the input.


In the rare occasions that you would place a z-index on an input (most of the time you don't really need to declare one), rather than setting a negative value, try using a low positive value instead. That way it should be above the body (unless this has been changed).

Here is a short example:

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

By using a positive value (0 or greater), you can then select the textbox

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

In the above example, you could ultimately remove the z-index altogether.

For more information on the z-index property, please refer to its documentation


JsFiddle Solution

like image 57
jbutler483 Avatar answered Oct 13 '22 00:10

jbutler483


The issue is with the z-index property of div#container element assigned as -1.

Set the z-index for the element to some higher value like 1000 etc.

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;
}
like image 26
Chandu Avatar answered Oct 13 '22 01:10

Chandu