Skip to content Skip to sidebar Skip to footer

Css Vertical Alignment

I have buttons with single column order inside div. Button count is changeable. I want to align buttons vertically. 'vertical-align:middle' is not work. How can I do that? Div heig

Solution 1:

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 heightto 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.

Solution 2:

This is the best way in my opinion (It's IE8+):

HTML:

<divclass="block"><divclass="centered"><div><inputtype="submit"value="ABC"/></div><div><inputtype="submit"value="ABC"/></div><div><inputtype="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/

Post a Comment for "Css Vertical Alignment"