简体   繁体   English

HTML 表格需要列间距,而不是行间距

[英]HTML table needs spacing between columns, not rows

I have an HTML table.我有一个 HTML 表格。 I need to have spacing between the table columns, but not the table rows.我需要表格列之间有间距,但表格行之间不需要。

If I use the cellspacing CSS property it does it between both rows and columns.如果我使用cellspacing CSS 属性,它会在行和列之间进行。 I also cannot use CSS in what I doing.我也不能在我所做的事情中使用 CSS。 I need to use pure HTML.我需要使用纯 HTML。

The better approach uses Shredder's css rule: padding: 0 15px 0 15px only instead of inline css, define a css rule that applies to all tds.更好的方法是使用 Shredder 的 css 规则:padding: 0 15px 0 15px only 而不是内联 css,定义一个适用于所有 tds 的 css 规则。 Do This by using a style tag in your page:通过在页面中使用样式标记来执行此操作:

<style type="text/css">
  td {
    padding: 0 15px;
  }
</style>

or give the table a class like "paddingBetweenCols" and in the site css use或者给表格一个类似“paddingBetweenCols”的类,并在站点 css 中使用

.paddingBetweenCols td {
  padding: 0 15px;
}

The site css approach defines a central rule that can be reused by all pages.站点 css 方法定义了一个可以被所有页面重用的中心规则。

If your doing to use the site css approach, it would be best to define a class like above and apply the padding to the class...unless you want all td's on the entire site to have the same rule applied.如果您使用站点 css 方法,最好定义一个类似上面的类并将填充应用于类......除非您希望整个站点上的所有 td 都应用相同的规则。

Fiddle for using style tag使用样式标签的小提琴

Fiddle for using site css使用站点 css 的小提琴

If you can use inline styling, you can set the left and right padding on each td .. Or you use an extra td between columns and set a number of non-breaking spaces as @rene kindly suggested.如果您可以使用内联样式,则可以在每个td上设置左右填充.. 或者您可以在列之间使用额外的td并按照@rene 的建议设置一些不间断空格。

http://jsfiddle.net/u5mN4/ http://jsfiddle.net/u5mN4/

http://jsfiddle.net/u5mN4/1/ http://jsfiddle.net/u5mN4/1/

Both are pretty ugly ;p css ftw两者都很丑陋;p css ftw

<table cellpadding="pixels"cellspacing="pixels"></table>
<td align="position"valign="position"></td>

cellpadding ="length in pixels" ~ The cellpadding attribute, used in the <table> tag, specifies how much blank space to display in between the content of each table cell and its respective border. cellpadding ="长度以像素为单位" ~ <table>标签中使用的 cellpadding 属性指定在每个表格单元格的内容与其各自边框之间显示多少空白空间。 The value is defined as a length in pixels.该值被定义为以像素为单位的长度。 Hence, a cellpadding="10" attribute-value pair will display 10 pixels of blank space on all four sides of the content of each cell in that table.因此,一个cellpadding="10"属性值对将在该表中每个单元格内容的所有四个边上显示 10 个像素的空白区域。

cellspacing ="length in pixels" ~ The cellspacing attribute, also used in the <table> tag, defines how much blank space to display in between adjacent table cells and in between table cells and the table border. cellspacing ="长度以像素为单位" ~ cellspacing 属性,也在<table>标签中使用,定义了在相邻表格单元格之间以及表格单元格和表格边框之间显示多少空白空间。 The value is defined as a length in pixels.该值被定义为以像素为单位的长度。 Hence, a cellspacing="10" attribute-value pair will horizontally and vertically separate all adjacent cells in the respective table by a length of 10 pixels.因此, cellspacing="10"属性值对会将相应表中的所有相邻单元格水平和垂直分隔 10 个像素的长度。 It will also offset all cells from the table's frame on all four sides by a length of 10 pixels.它还会将所有单元格从表格框架的所有四个边偏移 10 个像素的长度。

In most cases it could be better to pad the columns only on the right so just the spacing between the columns gets padded, and the first column is still aligned with the table.在大多数情况下,最好仅在右侧填充列,以便仅填充列之间的间距,并且第一列仍与表格对齐。

CSS: CSS:

.padding-table-columns td
{
    padding:0 5px 0 0; /* Only right padding*/
}

HTML: HTML:

<table className="padding-table-columns">
  <tr>
    <td>Cell one</td>
     <!-- There will be a 5px space here-->
    <td>Cell two</td>
     <!-- There will be an invisible 5px space here-->
  </tr>
</table>

This can be achieved by putting padding between the columns using CSS.这可以通过使用 CSS 在列之间放置填充来实现。 You can either add padding to the left of all columns except the first, or add padding to the right of all columns except the last.您可以在除第一列之外的所有列的左侧添加填充,或者在除最后一列之外的所有列的右侧添加填充。 You should avoid adding padding to the right of the last column or to the left of the first as this will insert redundant white space.您应该避免在最后一列的右侧或第一列的左侧添加填充,因为这会插入多余的空白。 You should also avoid being too prescriptive with classes to specify which columns should have the additional padding as this will make maintenance harder if you later add a new column.您还应该避免对类过于规范以指定哪些列应该具有额外的填充,因为如果您稍后添加新列,这将使维护变得更加困难。

The ' lobotomised owl selector ' allows you to select all siblings, regardless of if they are a th , td or something else. ' lobotomised owl selector '允许您选择所有兄弟姐妹,无论他们是thtd还是其他东西。

 tr > * + * { padding-left: 4em; }
 <table> <thead> <tr> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM