Html 放个   好不好?在一个空的 <td> 里面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2506342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:33:30  来源:igfitidea点击:

Is it good to put a &nbsp; inside an empty <td>?

htmlw3csemantic-markup

提问by Jitendra Vyas

If this is the structure:

如果是这样的结构:

<table cellspacing="0" cellpadding="0">
   <tr>
      <td>I don't need anything here, should I always put a &nbsp; here?</td>
      <td>item </td>
   </tr>
   <tr>
      <td>model</td>
      <td>one</td>
   </tr>
   <tr>
      <td>model</td>
      <td>two</td>
   </tr>
   <tr>
      <td>model</td>
      <td>three</td>
   </tr>
</table>

How will a screen reader read a blank td? Is it semantically correct?

屏幕阅读器如何阅读空白td?它在语义上是否正确?

回答by Boldewyn

Semantically correct IMHO would be to keep an empty cell reallyempty. However, I, too, fill empty cells with &nbsp;s for pragmatic reasons.

语义上正确的恕我直言是保持一个空单元格真的是空的。但是,&nbsp;出于实用的原因,我也用s填充空单元格。

As for screen readers, I'll have to make an educated guess: Empty nodes will likely not be read, because HTML consists mostly of whitespace text nodes, which readers ignore, and I assume, that &nbsp;is collapsed to a simple space in reader applications(since non-breakingis a property of visual media).

至于屏幕阅读器,我必须做出有根据的猜测:空节点可能不会被读取,因为 HTML 主要由空白文本节点组成,读者会忽略这些节点,我认为,&nbsp;阅读器应用程序中,它会折叠为一个简单的空间(因为不间断是视觉媒体的属性)。

For rendering visually, one could rely on the CSS table property empty-cells:

对于视觉渲染,可以依赖 CSS table 属性empty-cells

table {
    empty-cells: show;
}

回答by Jukka K. Korpela

Semantically, an HTML table is really just an array (matrix) of data, and there's no reason why a no-break space character could not be treated as data. Whether it really makes sense depends on the purpose, structure, and content of the table. In most cases, there is a better solution. See some suggestion in Empty cells in HTML tables.

从语义上讲,HTML 表实际上只是一个数据数组(矩阵),没有理由不能将不间断空格字符视为数据。它是否真的有意义取决于表格的目的、结构和内容。在大多数情况下,有更好的解决方案。请参阅HTML 表格中的空单元格中的一些建议。

On the technical side, the no-break space has width and height, depending on the font, and this imposes minimum requirements on the dimensions of the cell. This may matter, though mostly in cases where the row or the column is used just as a separator, e.g. to create horizontal or vertical line.

在技​​术方面,不间断空格具有宽度和高度,具体取决于字体,这对单元格的尺寸提出了最低要求。这可能很重要,但主要是在行或列仅用作分隔符的情况下,例如创建水平或垂直线。

Another impact of using a no-break space, as opposite to using a normal space or leaving the cell completely empty, is that it may affect the appearance of borders around the cell and background color and background image inside the cell.

与使用普通空间或将单元格完全留空相反,使用不间断空间的另一个影响是,它可能会影响单元格周围边框的外观以及单元格内的背景颜色和背景图像。

Screen readers differ in the way they indicate empty cells, if at all, and this may also depend on the reading mode. In any case, the user gets no indication of what's really going on, i.e. what an empty cell (if the emptyness is conveyed to the user at all) means. In visual browsing, the meaning may (or may not) be rather obvious.

屏幕阅读器在指示空单元格的方式上有所不同(如果有的话),这也可能取决于阅读模式。在任何情况下,用户都不会得到真正发生了什么的指示,即空单元格(如果空单元完全传达给用户)意味着什么。在视觉浏览中,含义可能(也可能不)相当明显。

回答by Alex Frost

Thanks to editors like Dreamweaver this practice became somewhat of a standard, so even if it is not a perfect solution - at least you won't be alone in doing it. Plus it is more compatible with older browser than CSS's empty-cells.

多亏了 Dreamweaver 这样的编辑器,这种做法在某种程度上成为了一种标准,因此即使它不是一个完美的解决方案 - 至少您不会孤军奋战。此外,它与旧浏览器的兼容性比 CSS 的empty-cells.

回答by Marcos Placona

there isn't anything exactly saying you shouldn't use use a blank TD, and it passes when you try to validate.

没有什么确切地说您不应该使用空白 TD,并且当您尝试验证时它会通过。

Although, a more elegant approach would be to use colspan.

虽然,更优雅的方法是使用 colspan。

i.e.

IE

<tr>
   <td colspan="2">item </td>
</tr>

But this will align your content to the left, and you will have to manually (via css) apply styles so the content is aligned where you want.

但这将使您的内容向左对齐,并且您必须手动(通过 css)应用样式,以便将内容对齐到您想要的位置。

The good thing about using it with colspans, is that screen readers will read only what's there, and not the empty spaces

将它与 colspans 一起使用的好处是,屏幕阅读器只会读取那里的内容,而不是空白区域