I would like to make a position: fixed;
popup box centered to the screen with a dynamic width and height. I used margin: 5% auto;
for this. Without position: fixed;
it centers fine horizontally, but not vertically. After adding position: fixed;
, it's even not centering horizontally.
Here's the complete set:
.jqbox_innerhtml {
position: fixed;
width: 500px;
height: 200px;
margin: 5% auto;
padding: 10px;
border: 5px solid #ccc;
background-color: #fff;
}
<div class="jqbox_innerhtml">
This should be inside a horizontally
and vertically centered box.
</div>
How do I center this box in screen with CSS?
This solution does not require of you to define a width and height to your popup div.
http://jsfiddle.net/4Ly4B/33/
And instead of calculating the size of the popup, and minus half to the top, javascript is resizeing the popupContainer to fill out the whole screen...
(100% height, does not work when useing display:table-cell; (wich is required to center something vertically))...
Anyway it works :)
To fix the position use this :-
Here is a modern approach for horizontally centering an element with a dynamic width - it works in all modern browsers; support can be seen here.
Updated Example
For both vertical and horizontal centering you could use the following:
Updated Example
You may wish to add in more vendor prefixed properties too (see the examples).
Add a container like:
Then put your box into this div will do the work.
One possible answer:
Just add: