html - 将列标题与 CSS/HTML 中的数据对齐
问题描述
我正在学习如何在 HTML 中制作表格,但是“c”列标题与其数据不对齐,而是与“b”列标题组合在一起。
输出:
这是代码:
table,th,td{
padding:20px;
border:10px solid navy;
}
<body>
<table>
<th>a</th>
<th>b</th>
<th>c</th>
<tr>
<td>1</td>
<td colspan="30">Apples</td>
<td><form>
<input type="checkbox" name="50g"/>0.5Kg
<input type="checkbox" name="50g"/>1Kg
<input type="checkbox" name="bag"/>Paper Bag?
</td></form>
</tr>
<tr>
<td>1</td>
<td colspan="30">Apples</td>
<td><form>
<input type="checkbox" name="50g"/>0.5Kg
<input type="checkbox" name="50g"/>1Kg
<input type="checkbox" name="bag"/>Paper Bag?
</td></form>
</tr>
<tr>
<td>1</td>
<td colspan="30">Apples</td>
<td><form>
<input type="checkbox" name="50g"/>0.5Kg
<input type="checkbox" name="50g"/>1Kg
<input type="checkbox" name="bag"/>Paper Bag?
</td></form>
</tr>
</table>
</body>
我尝试更改<th>
"c" 和 "b" 标记中的填充值,但似乎无法解决问题。我该如何解决?
解决方案
两件事可以解决您的问题。
- 确保您的 HTML 有效。您的 HTML 当前无效,因为您在收盘
</td>
前有收盘</form>
。应该是这样的</form></td>
。 - 从 every 中删除
colspan="30"
属性td
,将其更改为:
<td colspan="30">Apples</td>
到
<td>Apples</td>
colspan
指示单元格扩展了多少列。现在它扩展到 30 列,而您没有那么多列。在 MDN 上阅读更多内容。
<html>
<style>
table,
th,
td {
padding: 20px;
border: 10px solid navy;
}
</style>
<body>
<table>
<th>a</th>
<th>b</th>
<th>c</th>
<tr>
<td>1</td>
<td>Apples</td>
<td>
<form>
<input type="checkbox" name="50g" />0.5Kg
<input type="checkbox" name="50g" />1Kg
<input type="checkbox" name="bag" />Paper Bag?
</form>
</td>
</tr>
<tr>
<td>1</td>
<td>Apples</td>
<td>
<form>
<input type="checkbox" name="50g" />0.5Kg
<input type="checkbox" name="50g" />1Kg
<input type="checkbox" name="bag" />Paper Bag?
</form>
</td>
</tr>
<tr>
<td>1</td>
<td>Apples</td>
<td>
<form>
<input type="checkbox" name="50g" />0.5Kg
<input type="checkbox" name="50g" />1Kg
<input type="checkbox" name="bag" />Paper Bag?
</form>
</td>
</tr>
</table>
</body>
</html>
推荐阅读
- c - 带有字符串参数的 VOID 函数与带有返回 char 数组的字符串参数的函数之间的区别?
- iphone - 什么是咆哮者和模态?
- sharepoint - 如何在 SharePoint 中创建接受有限回复的调查列表?
- php - 尝试更新数组的元素删除元素而不是实现它
- android - 如何根据 PhilJay 的 MPAndroidChart 库在图表中的 x 轴分区值设置背景颜色?
- javascript - 无法使用 reducer 中的条件语句更改处于状态的数组的第二个值 --- Redux
- linux - 如何修改 netcat 命令行以一次发送多个请求
- javascript - 我该如何做这个 if 语句?
- python - 将复杂的 txt 文件(包括数组)读取到 Python
- laravel - 如何在编辑表单中捕获表上的 1 个列(id)