Reputation: 29179
I need to vertical align a span, but the thing that makes this complicated is that this span needs to occupy its whole parent. Something like this jsfiddle
So, the HTML is
<div>
<span>OK</span>
</div>
CSS:
div {
width: 200px;
height: 200px;
background-color: lightgrey;
}
span {
display: block;
width: 100%;
height: 100%;
vertical-align: middle;
}
Is this possible without changing the html ?
UPDATE: Although the below answers are all correct and interesting, I chose the one I actually used to be the correct one!
Upvotes: 2
Views: 84
Reputation: 9313
You could achieve that with a 'ghost' pseudo element
span {
display: block;
width: 100%;
height: 100%;
vertical-align: middle;
text-align: center;
}
span:before {
content: "";
display:inline-block;
vertical-align: middle;
height: 100%;
}
An example : http://jsfiddle.net/9uaE6/11/
Upvotes: 1
Reputation: 16204
Tell it to behave like a table:
div {
width: 200px;
height: 200px;
background-color: lightgrey;
display: table;
}
span {
display: table-cell;
text-align: center;
vertical-align: middle;
}
Upvotes: 4
Reputation: 3373
div{
/* Internet Explorer 10 */
display:-ms-flexbox;
-ms-flex-pack:start;
-ms-flex-align:center;
/* Firefox */
display:-moz-box;
-moz-box-pack:start;
-moz-box-align:center;
/* Safari, Opera, and Chrome */
display:-webkit-box;
-webkit-box-pack:start;
-webkit-box-align:center;
/* W3C */
display:box;
box-pack:start;
box-align:center;
}
Source:ms-flex
Upvotes: 1