46

I'm having trouble centering a button in a td.

This is probably a simple CSS issue, but the app is using bootstrap, AngularJS, AngularJS-ui-bootstrap, and ngTable. I've included all of these components in my plunkr.

I'm trying to set "horizontal-align: middle" on the td with the button, but that doesn't seem to get applied. The button still leans to the left side of the cell.

David M. Karr
  • 14,317
  • 20
  • 94
  • 199
  • Have you tried adding align="center" on the TD?" – Billy Moat Mar 13 '14 at 16:48
  • 4
    what about `text-align:center`? – T J Mar 13 '14 at 16:48
  • Did anyone but @dawuut notice that I already had this? I would have thought I have a problem with the "specificity" of that css rule, but dawuut's solution, which just changes the css rule where I had the "horizontal-align" and "text-align" settings, does actually work. – David M. Karr Mar 13 '14 at 17:01

8 Answers8

81

You can use :

display: block;
margin: auto;

Here is your updated plunkr

meriadec
  • 2,953
  • 18
  • 20
15

As I said in my comment above simply change the TD to have the align="center" property.

<td align="center"></td>

Since posting this I've discovered that this is deprecated in HTML5 so best just to use "text-align: center" on the TD in your CSS>

Billy Moat
  • 20,792
  • 7
  • 45
  • 37
9

This worked for me

<td style="text-align: center">

vineel
  • 3,483
  • 2
  • 29
  • 33
3

I found that I had to combine the following attributes to center JSF controls on a page:

<h:form style="display: block; text-align: center; margin: auto; width: 200px">
<!-- components here -->
</h:form>
Fuzzy Analysis
  • 3,168
  • 2
  • 42
  • 66
3

If you are using Bootstrap 4 add these classes to the <td> element (mine has 2 buttons):

<td class="text-center align-middle">
    <div class="btn-group">
        <button class="btn btn-outline-primary">+</button>
        <button class="btn btn-outline-primary">-</button>
    </div>                
</td>

text-center will center content horizontally, while align-middle centers content vertically.

Abdelillah Aissani
  • 3,058
  • 2
  • 10
  • 25
tdahman1325
  • 210
  • 3
  • 6
2

It worked fine for me

<td align="center">
 <input type="button" style="float:none!important;display:inline;" value="click" />
</td>
1

Alter the button instead of td? Make less impact on the style.

table .btn{
  vertical-align: top;
}
Jens Alenius
  • 1,931
  • 2
  • 16
  • 20
0
HTML:
    <td class="table-row-radio-button"> <input type="radio"> </td>
CSS:
    .table-row-radio-button {
        text-align: center;
    }

worked for me.

Vinay
  • 153
  • 1
  • 2
  • 10