I would like to make a dialog div
appear within the viewport every time it is shown without using position: fixed;
. Essentially when the Show buttons in the below example are clicked, I want the dialog to appear where it would if its position were fixed, but I want its position to be absolute. Or, in slightly different words, I "... want it to be positioned as if it were fixed, and then stop being fixed immediately after placement." (Thanks apsillers) I want it to be position: absolute;
so that I can still scroll past it in the browser, but I still want it to appear in the viewport when it is initially shown.
I've tried a lot of different CSS from here, here, and here, but none seemed to fulfill my requirements. Note that I cannot use JQuery or other JS libraries.
.dialog {
position: absolute;
}
<div>
<div id="dialog" class="dialog" style="display:none;">Dialog</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<button onclick="document.getElementById('dialog').style.display=null;">Show</button>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<button onclick="document.getElementById('dialog').style.display=null;">Show</button>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<button onclick="document.getElementById('dialog').style.display=null;">Show</button>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<button onclick="document.getElementById('dialog').style.display=null;">Show</button>
</div>