Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent page jump from :target selector

Tags:

css

So I have this modal that pops up using the CSS :target selector. However, the page jumps to the anchor when clicked. I would like to prevent the page from jumping to the :target selector. How can I do this?

<a href="#openModal">Info</a>

<div id="openModal" class="modalDialog">

CSS:

.modalDialog {
    position: absolute;
    pointer-events: none;
    z-index: 99999;
opacity:0;
}

.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}

.modalDialog > div {
    width: 900px;
    height: 506px;
    position: relative;
    background: rgba(0,0,0,0.9);
}
like image 826
jick Avatar asked Apr 05 '13 07:04

jick


1 Answers

Make the .modalDialog position: fixed instead of absolute. This will cause it to always be positioned at wherever the page is currently scrolled.

A more complete example: http://codepen.io/mblase75/pen/xbRNeV

(There's some other trickery involved in that codepen demo -- adding another target to the 'close' button on the modal which is also fixed keeps the page from scrolling when the modal is closed, and changing the z-index of your modal from -1 to 100 (or some other suitably large integer) will keep it from blocking clicks right after you close it.)

like image 105
Blazemonger Avatar answered Oct 24 '22 13:10

Blazemonger