Unable to click textbox with negative z-index

前端 未结 2 1079
夕颜
夕颜 2021-01-18 06:49

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 t

相关标签:
2条回答
  • 2021-01-18 07:20

    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;
    }
    
    0 讨论(0)
  • 2021-01-18 07:30

    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

    0 讨论(0)
提交回复
热议问题