Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't z-index allow the OK button to be clickable?

I have a #popup-shadow div that takes all the viewport, in order to make #popup modal, and prevent everything out of #popup to be clickable. This works.

Problem: it even makes everything inside #popup unclickable. Why?

Since #popup has a higher z-index (value 100!), why is the OK button unclickable?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#popup-shadow { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 10; }
#popup { width: 400px; background-color: #eee; z-index: 100; }
#popup-buttons div { font-size: 2em; text-align: center; padding: 15px 0; }
#popup-buttons div:hover { cursor: pointer; }
<div id="popup-wrapper">
    <div id="popup-shadow"></div>    
    <div id="popup">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        <div id="popup-buttons">
            <div onclick="alert();">OK</div>
        </div>
    </div>
</div>

Note: if we remove <div id="popup-shadow"></div>, it works.

>Solution :

z-index doesn’t affect unpositioned elements. add position :relative e.g. to popup

#popup-shadow { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 10; }
#popup { position:relative; width: 400px; background-color: #eee; z-index: 100; }
#popup-buttons div { font-size: 2em; text-align: center; padding: 15px 0; }
#popup-buttons div:hover { cursor: pointer; }
<div id="popup-wrapper">
    <div id="popup-shadow"></div>    
    <div id="popup">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        <div id="popup-buttons">
            <div onclick="alert();">OK</div>
        </div>
    </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading