HTML 表格边框间距

标签 html css

我正在尝试在 html 表格中的列之间设置间距,

实际上,我只是想在每列的右侧留出间距。

到目前为止,我只设法使用 border-spacing 属性来增加每列之间的间距。当我使用这个属性时,我意识到每列的左侧和右侧都有间距。是否可以使用 css 规则只在右侧应用间距?

下面是我当前方法的代码片段:

.table{
  border-spacing: 10px 0;
}
<!DOCTYPE html>
<html>
<head>
<title>html table</title>
</head>
<body>
<!-- Start your code here -->

 <table class="table"  >
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td><input type="text" value="some text"/></td>
    <td><input type="text" value="another text"/></td>
    <td><input type="text" value="looks like you giving"/></td>
    
  </tr>

</table> 

<!-- End your code here -->
</body>
</html>

感谢您的帮助。

最佳答案

您不能在表格的特定部分应用间距,也不能在 td 上放置边距也不td ,对此的某种解决方案是在 <td> 上使用填充.

table {
  border-spacing: 0;
}

td {
  padding-right: 10px
}
<!--border for refrence -->
<table border="1">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td><input type="text" value="" /></td>
    <td><input type="text" value="" /></td>
    <td><input type="text" value="" /></td>
  </tr>
</table>

或使用 >* 直接子级的边距

table {
  border-spacing: 0;
}

td>* {
  margin-right: 10px
}
<!--border for refrence -->
<table border="1">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td><input type="text" value="" /></td>
    <td><input type="text" value="" /></td>
    <td><input type="text" value="" /></td>
  </tr>
</table>

关于HTML 表格边框间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50821653/

相关文章:

javascript - 将 jQuery 存储在变量中

javascript - 从 JSON 对象的 HTML 中解析 img

css - 位置为 :absolute loses layout inside 的 Bootstrap 容器

android - Bootstrap CSS - 移动菜单 100% 高度不适用于 Android

php - 如何在 HTML 中运行 PHP?

html - 图像向左浮动超出了 div

当规则首先被删除然后再次添加时,CSS滚动捕捉不起作用(chrome)

css - 在 Edge 和 Chrome 上应用全屏媒体查询

jquery - 将排序图像放在 DataTable 标题的左侧

html - 元素堆叠而不是与内联 block 对齐