How to center a checkbox in a table cell?

后端 未结 10 1319
盖世英雄少女心
盖世英雄少女心 2020-12-05 03:57

The cell contains nothing but a checkbox. It is rather wide because of text in the table header row. How do I center the checkbox (with inline CSS in my HTML? (I know))

相关标签:
10条回答
  • 2020-12-05 04:16

    If you don't support legacy browsers, I'd use flexbox because it's well supported and will simply solve most of your layout problems.

    #table-id td:nth-child(1) { 
        /* nth-child(1) is the first column, change to fit your needs */
        display: flex;
        justify-content: center;
    }
    

    This centers all content in the first <td> of every row.

    0 讨论(0)
  • 2020-12-05 04:26

    Define the float property of the check element to none:

    float: none;
    

    And center the parent element:

    text-align: center;
    

    It was the only that works for me.

    0 讨论(0)
  • 2020-12-05 04:28

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
    
    <table class="table table-bordered">
      <thead>
        <tr>
          <th></th>
          <th class="text-center">Left</th>
          <th class="text-center">Center</th>
          <th class="text-center">Right</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>
            <span>Bootstrap (class="text-left")</span>
          </th>
          <td class="text-left">
            <input type="checkbox" />
          </td>
          <td class="text-center">
            <input type="checkbox" checked />
          </td>
          <td class="text-right">
            <input type="checkbox" />
          </td>
        </tr>
        <tr>
          <th>
            <span>HTML attribute (align="left")</span>
          </th>
          <td align="left">
            <input type="checkbox" />
          </td>
          <td align="center">
            <input type="checkbox" checked />
          </td>
          <td align="right">
            <input type="checkbox" />
          </td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-12-05 04:29

    This should work, I am using it:

    <td align="center"> <input type="checkbox" name="myTextEditBox" value="checked" ></td>
    
    0 讨论(0)
提交回复
热议问题