Unable to click textbox with negative z-index

前端 未结 2 1078
夕颜
夕颜 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: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;
    }


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

    body{
      background:tomato;
      }
    input{
      position:relative;
      z-index:0;
      }

    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

提交回复
热议问题