Centering a button vertically in table cell, using Twitter Bootstrap

前端 未结 6 769
盖世英雄少女心
盖世英雄少女心 2020-12-07 15:42

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

相关标签:
6条回答
  • 2020-12-07 15:50

    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 #ffffdffffd;
    }
    

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

    0 讨论(0)
  • 2020-12-07 15:54

    Add vertical-align: middle; to the td element that contains the button

    <td style="vertical-align:middle;">  <--add this to center vertically
      <a href="#" class="btn btn-primary">
        <i class="icon-check icon-white"></i>
      </a>
    </td>
    
    0 讨论(0)
  • 2020-12-07 15:55

    To fix this, i put this class on the webpage

    <style>                        
        td.vcenter {
            vertical-align: middle !important;
            text-align: center !important;
        }
    </style>
    

    and this in my TemplateField

    <asp:TemplateField ItemStyle-CssClass="vcenter">
    

    as the CSS class points directly to the td (tabledata) element and has the !important statment at the end each setting. It will over rule bootsraps CSS class settings.

    Hope it helps

    0 讨论(0)
  • 2020-12-07 16:02

    So why is td default set to vertical-align: top;? I really don't know that yet. I would not dare to touch it. Instead add this to your stylesheet. It alters the buttons in the tables.

    table .btn{
      vertical-align: top;
    }
    
    0 讨论(0)
  • 2020-12-07 16:04

    add this to your css

    .table-vcenter td {
       vertical-align: middle!important;
    }
    

    then add to the class to your table:

            <table class="table table-hover table-striped table-vcenter">
    
    0 讨论(0)
  • 2020-12-07 16:11

    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>
    
    0 讨论(0)
提交回复
热议问题