如何将表格单元格中的复选框居中?

时间:2011-02-19 02:38:56

标签: html css

单元格只包含一个复选框。由于表标题行中的文本,它相当宽。如何使复选框居中(在我的HTML中使用内联CSS?(我知道))

我试过

 <td>
      <input type="checkbox" name="myTextEditBox" value="checked" 
      style="margin-left:auto; margin-right:auto;">
 </td>

但这不起作用。我究竟做错了什么?


更新:这是一个测试页面。有人可以纠正它 - 在HTML中使用CSS内联 - 以便复选框在其列中居中?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Alignment test</title>
</head>
<body>

<table style="empty-cells:hide; margin-left:auto; margin-right:auto;" border="1"   cellpadding="1" cellspacing="1">

  <tr>
    <th>Search?</th><th>Field</th><th colspan="2">Search criteria</th><th>Include in report?<br></th>
  </tr>

  <tr>
    <td>
        <input type="checkbox" name="query_myTextEditBox" style="text-align:center; vertical-align: middle;">    
    </td>

    <td>
      myTextEditBox
    </td>

    <td>
       <select size ="1" name="myTextEditBox_compare_operator">
        <option value="=">equals</option>
        <option value="<>">does not equal</option>
       </select>
    </td>

    <td>
      <input type="text" name="myTextEditBox_compare_value">
    </td>

    <td>
      <input type="checkbox" name="report_myTextEditBox" value="checked" style="text-align:center; vertical-align: middle;">
    </td>

  </tr>

</table>


</body>
</html>

我接受了@Hristo的答案 - 这里是内联格式化的......

<table style="empty-cells:hide;" border="1"   cellpadding="1" cellspacing="1">

    <tr>
        <th>Search?</th><th>Field</th><th colspan="2">Search criteria</th><th>Include in report?<br></th>
    </tr>

    <tr>
        <td style="text-align: center; vertical-align: middle;">
            <input type="checkbox" name="query_myTextEditBox">    
        </td>

        <td>
            myTextEditBox
        </td>

        <td>
            <select size ="1" name="myTextEditBox_compare_operator">
                <option value="=">equals</option>
                <option value="<>">does not equal</option>
            </select>
        </td>

        <td>
            <input type="text" name="myTextEditBox_compare_value">
        </td>

        <td style="text-align: center; vertical-align: middle;">
            <input type="checkbox" name="report_myTextEditBox" value="checked">
        </td>

    </tr>

</table>

11 个答案:

答案 0 :(得分:99)

更新

这个怎么样...... http://jsfiddle.net/gSaPb/


查看我在jsFiddle上的示例:http://jsfiddle.net/QzPGu/

HTML

<table>
    <tr>
        <td>
            <input type="checkbox" name="myTextEditBox" value="checked" /> checkbox
        </td>
    </tr>
</table>

CSS

td {
    text-align: center; /* center checkbox horizontally */
    vertical-align: middle; /* center checkbox vertically */
}
table {
    border: 1px solid;
    width: 200px;
}
tr {
    height: 80px;   
}

我希望这会有所帮助。

答案 1 :(得分:26)

尝试

<td style="text-align:center;">
  <input type="checkbox" name="myTextEditBox" value="checked" />
</td>

答案 2 :(得分:19)

试试这个,这应该有效,

td input[type="checkbox"] {
    float: left;
    margin: 0 auto;
    width: 100%;
}

答案 3 :(得分:9)

这应该有效,我正在使用它:

<td align="center"> <input type="checkbox" name="myTextEditBox" value="checked" ></td>

答案 4 :(得分:4)

拉出所有内嵌CSS ,并将其移至头部。然后在单元格上使用类,这样你就可以根据需要调整所有内容(不要使用像“center”这样的名字 - 你可以在6个月之后将它改为左边......)。对齐答案仍然相同 - 将其应用于<td>而不是复选框(这只会使您的支票居中:-))

使用你的代码...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Alignment test</title>
<style>
table { margin:10px auto; border-collapse:collapse; border:1px solid gray; }
td,th { border:1px solid gray; text-align:left; padding:20px; }
td.opt1 { text-align:center; vertical-align:middle; }
td.opt2 { text-align:right; }
</style>
</head>
<body>

<table>

      <tr>
        <th>Search?</th><th>Field</th><th colspan="2">Search criteria</th><th>Include in report?<br></th>
      </tr>
      <tr>
        <td class="opt1"><input type="checkbox" name="query_myTextEditBox"></td>
        <td>
          myTextEditBox
        </td>
        <td>
           <select size ="1" name="myTextEditBox_compare_operator">
            <option value="=">equals</option>
            <option value="<>">does not equal</option>
           </select>
        </td>
        <td><input type="text" name="myTextEditBox_compare_value"></td>
        <td class="opt2">
          <input type="checkbox" name="report_myTextEditBox" value="checked">
        </td>
      </tr>
    </table>
    </body>
    </html>

答案 5 :(得分:3)

如果您不支持旧版浏览器,我会使用flexbox,因为它是well supported,只会解决您的大部分布局问题。

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

这会将所有内容集中在每行的第一个<td>中。

答案 6 :(得分:2)

用于动态编写td元素而不直接依赖于td Style

  <td>
     <div style="text-align: center;">
         <input  type="checkbox">
     </div>
  </td>

答案 7 :(得分:1)

将check元素的float属性定义为none:

float: none;

将父元素居中:

text-align: center;

这是唯一对我有用的。

答案 8 :(得分:0)

<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>

答案 9 :(得分:0)

我的问题是有一个带有 position: absolute !important 的父样式,我不允许对其进行编辑。

所以我指定了我的特定复选框 position: relative !important 并修复了垂直未对齐问题。

答案 10 :(得分:-1)

确保<td>不是display: block;

浮动会做到这一点,但更容易:display: inline;