html - show div in the center of current viewport on mobile devices -
how show div in center of current viewport? asking mobile devices, because problematic.
for example, have 2 buttons show same div. 1 @ top of page, , 1 @ bottom. page's height 2 viewport heights in total. if padding-top of presented div 40px, div not visible when bottom button being pressed. solution isn't flexible enough mobile devices. solution recommend?
diagram: if set padding-top 40px, green div appear @ top of page (as shown) if bottom red button pressed, green div appear @ top, 40px it. isn't visible user. should appear 40px top of second viewport (the screen users sees.) hope helps.
use position: fixed
.
function togglediv() { var div = document.getelementbyid('togglable'); if (div.style.display !== 'none') { div.style.display = 'none'; } else { div.style.display = 'block'; } };
#container-parent { position: absolute; background-color: #eee; height: 95%; width: 90%; overflow: scroll; } #container { height: 500px; } #togglable { position: fixed; top: 10%; height: 80%; left: 25%; width: 50%; background-color: red; } #spacer { height: 90%; }
<div id="container-parent"> <div id="container"> <input type=button onclick="togglediv()"> <div id="togglable"></div> <div id="spacer"> <!-- don't know how have got buttons attached point `position: fixed` positions togglable div relative viewport --> </div> <input type=button onclick="togglediv()"> </div> </div>
Comments
Post a Comment