Trying to center align button in td
Asked Answered
M

8

47

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.

Milo answered 13/3, 2014 at 16:46 Comment(3)
Have you tried adding align="center" on the TD?"Downtown
what about text-align:center?Michelinemichell
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.Milo
F
83

You can use :

display: block;
margin: auto;

Here is your updated plunkr

Filthy answered 13/3, 2014 at 16:49 Comment(2)
It figures that the correct solution is the least understandable.Milo
css got its own mysteries :)Filthy
D
17

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>

Downtown answered 13/3, 2014 at 16:49 Comment(2)
The align attribute was removed in HTML5.Christi
@null - You learn a new thing everyday! It does still work but best to use CSS instead then I guess.Downtown
L
9

This worked for me

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

Lamdin answered 21/6, 2016 at 14:36 Comment(0)
S
4

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.

Salliesallow answered 18/7, 2019 at 14:8 Comment(0)
I
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>
Intellect answered 10/4, 2015 at 6:50 Comment(0)
P
2

It worked fine for me

<td align="center">
 <input type="button" style="float:none!important;display:inline;" value="click" />
</td>
Psalterium answered 21/6, 2018 at 11:56 Comment(0)
I
1

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

table .btn{
  vertical-align: top;
}
Iny answered 9/3, 2016 at 13:39 Comment(0)
P
0
HTML:
    <td class="table-row-radio-button"> <input type="radio"> </td>
CSS:
    .table-row-radio-button {
        text-align: center;
    }

worked for me.

Puto answered 9/4, 2018 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.