Reputation: 18630
I was trying to get a div which would take up the whole screen and display the word Loading... on it. I wanted the text to be horizontally and vertically in the centre.
I managed to get it horizontally aligned but the text is at the top of the screen not the middle.
The code is:
<div id="loadingdiv" class="screenMask"><p>Loading . . .</p></div>
div.screenMask
{
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
overflow-y: auto;
z-index: 1000;
background-color: #000000;
opacity: 0.7;
filter: alpha(opacity=70);
text-align: center;
font-size: 28px;
}
div.screenMask p
{
border: 1px solid red;
color: white;
display: inline;
z-index: 1001;
vertical-align: middle;
}
Here it is in jsFiddle:
Anyone know how I can get the text in the vertical middle position?
I thought vertical align would have worked but it doesn't
Upvotes: 0
Views: 442
Reputation: 573
Change div.screenMask p to...
div.screenMask p
{
border: 1px solid red;
position:absolute;
top:50%;
color: white;
display: inline;
z-index: 1001;
}
Then add a negative margin-top if you want to move it up a few pixels.
UPDATE If you don't need the red border (if it is for troubleshooting) use this...
div.screenMask
{
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
overflow-y: auto;
z-index: 1000;
background-color: #000000;
opacity: 0.7;
filter: alpha(opacity=70);
text-align: center;
font-size: 28px;
}
div.screenMask p
{
border: 1px solid red;
color: white;
position:absolute;
height:60px;
width:100%;
text-align:center;
top:50%;
margin:-30px 0 0 0px;
}
Upvotes: 1
Reputation: 15609
Change display
to table
on the div
, and to table-cell
on the p
. If you want the border around the text then simply add a span
around it, and give it that border: http://jsfiddle.net/azEaq/11/
This is all perfectly valid css, and your not using tables here (bad practice, right?) - you're just changing the display to mimic tables. This technique is good when you have no predefined height on the block you want to center - as in the case you presented.
Upvotes: 0
Reputation: 7345
Using % positioning and then using negative margining is one solution. Using this method is much better approach as it will not break the layout on window/frame resizing.
div.screenMask p
{
border: 1px solid red;
color: white;
display: inline;
z-index: 1001;
position:absolute;
height:30px;
top:50%;
margin:-15px 0 0 0px;
vertical-align: middle;
}
Here have a look http://jsfiddle.net/azEaq/9/ , resize the frame to see how it works.
Upvotes: 0
Reputation: 5914
This would work, just add top: 50%;
and position: absolute;
to the div.screenMask p
class in your css
, you can see the update here http://jsfiddle.net/azEaq/5/
CSS markup would look like:
div.screenMask p
{
border: 1px solid red;
color: white;
display: inline;
z-index: 1001;
top: 50%;
position: absolute;
}
Upvotes: 0