Css Vertical Alignment
I have buttons with single column order inside div. Button co开发者_Python百科unt is changeable. I want to align buttons vertically. "vertical-align:middle" is not work. How can I do that?
Div height is fixed. http://jsfiddle.net/WmD3L/ How can I align theese buttons vertically?
If the button has a fixed height you can use line-height to place the text in the middle.
e.g.
#button {
height: 50px;
line-height: 50px; //Must be the same as height to vertically align to the middle
}
You can also use
#button {
display: table-cell;
vertical-align: middle;
}
But I don't think this works cross browser unfortunately.
This is the best way in my opinion (It's IE8+):
HTML:
<div class="block">
<div class="centered">
<div><input type="submit" value="ABC"/></div>
<div><input type="submit" value="ABC"/></div>
<div><input type="submit" value="ABC"/></div>
</div>
</div>
CSS:
/* Can be any width and height */
.block {
height:500px;
text-align: center;
}
/* The ghost, nudged to maintain perfect centering */
.block:before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can be any width or height */
.centered {
display: inline-block;
vertical-align: middle;
width: 300px;
}
Demo: http://jsfiddle.net/WmD3L/13/
精彩评论