Vertically centering button using css

2019-02-05 21:01发布

I am trying to make a simple input button center-align within a table cell.

My markup is:

<table width="500" border="1">
    <tr>
        <td width="390">XXXXXXXXX</td>
        <td width="110" rowspan="2" valign="middle"><input type="button" value="submit"></td>
    </tr>
    <tr>
        <td>YYYYYYYY</td>
    </tr>
</table>
<br /><br />
<div style="width:500px;">

And may be viewed here:

http://jsfiddle.net/tP4sD/

I have done a table version showing you the layout that I am trying to achieve. Note that the text represented by "XXXXX" or "YYYYYY" is of variable length.

标签: html css layout
2条回答
Fickle 薄情
2楼-- · 2019-02-05 21:29

http://jsfiddle.net/8v8gLn4y/

.container {
  background: lightblue;
  display: table;
  width:100%;
}
        
input[type=button] {    
  display: block;
  width: 50%;
  margin: 0 auto;
}
        
.button-wrapper {
  background: darkorange;
  display: table-cell;
  vertical-align: middle;
}
<div class='container'>
    
  <div>some line with text</div>
  <div>another line with text</div>    
    
  <div class='button-wrapper'>
    <input type="button" value="submit"  />
  </div>
    
</div>

update 2016:
flexbox http://jsfiddle.net/9knLeab6/1/

查看更多
时光不老,我们不散
3楼-- · 2019-02-05 21:33

If you make your button an inline element and add text-align: center to the parent td you should be fine.

See also: http://jsfiddle.net/c4urself/NucL9/

查看更多
登录 后发表回答