Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align in bootstrap table

I am trying to display a table with 4 columns, one of which is an image. Below is the snapshot:-enter image description here

I want to vertically align the text to the center position, but somehow the css doesn't seem to work. I have used the bootstrap responsive tables. I want to know why my code doesn't work and whats is the correct method to make it work.

following is the code for the table

CSS

img {     height: 150px;     width: 200px; } th, td {     text-align: center;     vertical-align: middle; } 

HTML

<table id="news" class="table table-striped table-responsive">     <thead>         <tr>           <th>Name</th>           <th>Email</th>           <th>Phone</th>           <th>Photo</th>         </tr>     </thead>     <tbody>         <?php         $i=0;         foreach ($result as $row)          { ?>         <tr>             <td>                 <?php echo 'Lorem Ispum'; ?>             </td>             <td>                 <?php echo '[email protected]'; ?>             </td>             <td>                 <?php echo '9999999999'; ?>             </td>             <td>                 <?php echo '<img src="'.  base_url('files/images/test.jpg').'">'; ?>             </td>         </tr>          <?php         }         ?>                </tbody> </table> 
like image 286
Piyush Vishwakarma Avatar asked Jan 02 '15 18:01

Piyush Vishwakarma


People also ask

How do I vertically align text in bootstrap 4?

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 text vertically in bootstrap?

Answer: Use the align-items-center Class In Bootstrap 4, if you want to vertically align a <div> element in the center or middle, you can simply apply the class align-items-center on the containing element.

How do I center text in bootstrap table?

By adding the ” text-center” class of Bootstrap 3 We can use the “text-center” class of bootstrap 3 for the center-alignment of an element. So in our td when we add “text-center” class, and then our table text goes to the center.

How do I make my Div center vertically and horizontally in bootstrap?

Add d-flex align-items-center justify-content-center classes to the parent element to center its content vertically and horizontally.


2 Answers

Based on what you have provided your CSS selector is not specific enough to override the CSS rules defined by Bootstrap.

Try this:

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

In Boostrap 4 and 5, this can be achieved with the .align-middle Vertical Alignment utility class.

<td class="align-middle">Text</td> 
like image 69
hungerstar Avatar answered Oct 12 '22 07:10

hungerstar


As of Bootstrap 4 this is now much easier using the included utilities instead of custom CSS. You simply have to add the class align-middle to the td-element:

<table>   <tbody>     <tr>       <td class="align-baseline">baseline</td>       <td class="align-top">top</td>       <td class="align-middle">middle</td>       <td class="align-bottom">bottom</td>       <td class="align-text-top">text-top</td>       <td class="align-text-bottom">text-bottom</td>     </tr>   </tbody> </table> 
like image 44
Andreas Bergström Avatar answered Oct 12 '22 08:10

Andreas Bergström