Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a button vertically in table cell, using Twitter Bootstrap

I'm using Twitter Bootstrap, and attempting to center a button within a table cell. I only really need it centered vertically.

<table class="table table-bordered table-condensed">   <tbody>     <tr>       <td><a href="#" class="btn btn-primary"><i class="icon-check icon-white"></i></a></td>       <td><a href="#">Todo Item One</a><br /><span class="label label-success">One thing</span></td>     </tr>   </tbody> </table> 

Please see my JSFiddle for the problem. I've tried several table centering tricks I know about, but none seem to work properly. Any ideas? Thanks in advance.

UPDATE: Yes, I've tried vertical-align:middle;, and that works if it's inline, but not if it's in a class the td has. Updated the JSFiddle to reflect this.

Final Update: I'm an idiot, and didn't check to see if it was being overwritten by bootstrap.css

like image 942
Tim Habersack Avatar asked Dec 07 '12 21:12

Tim Habersack


People also ask

How do I center something vertically in bootstrap?

1 — Vertical Center Using Auto Margins One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.

How do I center align a button in bootstrap?

Answer: Use the text-center Class You can simply use the built-in class . text-center on the wrapper element to center align buttons or other inline elements in Bootstrap. It will work in both Bootstrap 3 and 4 versions.

How do you align text in the center of the table cell vertically?

All you need to do is position the insertion point somewhere within the text and then click on the Center button on the Home tab of the ribbon or press Ctrl+E. Centering text vertically is almost as easy: Position the insertion point within the cell you want to vertically center.


2 Answers

FOR BOOTSTRAP 3.X:

Bootstrap now has the following style for table cells:

.table tbody > tr > td{     vertical-align: top; } 

The way to go is to add your own class, adding more specificity to the previous selector:

.table tbody > tr > td.vert-aligned {     vertical-align: middle; } 

And then add the class to your tds:

<tr>     <td class="vert-aligned"></td>     ... </tr> 

FOR BOOTSTRAP 2.X

There is no way to do this with Bootstrap.

When used in table cells, vertical-align does what most people expect it to, which is to mimic the (old, deprecated) valign attribute. In a modern, standards-compliant browser, the following three code snippets do the same thing:

<td valign="middle"> <!-- but you shouldn't ever use valign --> </td> <td style="vertical-align:middle"> ... </td> <div style="display:table-cell; vertical-align:middle"> ... </div> 

Check your fiddle updated

Further

  • vertical-align: middle with Bootstrap 2
  • Understanding vertical-align
  • Vertical alignment of elements in a div

Also, you can't refer to the td class using .vert because Bootstrap already has this class:

.table td {    padding: 8px;    line-height: 20px;    text-align: left;    vertical-align: top; // The problem!    border-top: 1px solid #dddddd; } 

And is overloading the vertical-align: middle in '.vert' class, so you have to define this class as td.vert.

like image 185
Tom Sarduy Avatar answered Sep 20 '22 09:09

Tom Sarduy


A little update for Bootstrap 3.

Bootstrap now has the following style for table cells:

.table tbody>tr>td {     vertical-align: top; } 

The way to go is to add a your own class, with the same selector:

.table tbody>tr>td.vert-align {     vertical-align: middle; } 

And then add it to your tds

<td class="vert-align"></td> 
like image 39
Gabriel G. Roy Avatar answered Sep 24 '22 09:09

Gabriel G. Roy