Html 什么取代了 HTML5 表格中的 cellpadding、cellspacing、valign 和 align?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6048913/
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 08:40:16  来源:igfitidea点击:

What replaces cellpadding, cellspacing, valign, and align in HTML5 tables?

htmlcssvisual-studiocss-tables

提问by Code Maverick

In Visual Studio, I'm seeing these warnings:

Visual Studio 中,我看到以下警告:

  • Validation (HTML 5): Attribute 'cellpadding' is not a valid attribute of element 'table'.
  • Validation (HTML 5): Attribute 'cellspacing' is not a valid attribute of element 'table'.
  • Validation (HTML 5): Attribute 'valign' is not a valid attribute of element 'td'.
  • Validation (HTML 5): Attribute 'align' is not a valid attribute of element 'td'.
  • 验证 (HTML 5):属性“cellpadding”不是元素“table”的有效属性。
  • 验证 (HTML 5):属性“cellspacing”不是元素“table”的有效属性。
  • 验证 (HTML 5):属性“valign”不是元素“td”的有效属性。
  • 验证 (HTML 5):属性“align”不是元素“td”的有效属性。

If they are not valid attributes in HTML5, what replaces them in CSS?

如果它们在HTML5中不是有效属性,那么在 CSS中用什么替换它们?

回答by drudge

/* cellpadding */
th, td { padding: 5px; }

/* cellspacing */
table { border-collapse: separate; border-spacing: 5px; } /* cellspacing="5" */
table { border-collapse: collapse; border-spacing: 0; }   /* cellspacing="0" */

/* valign */
th, td { vertical-align: top; }

/* align (center) */
table { margin: 0 auto; }

回答by Cole Johnson

This should solve your problem:

这应该可以解决您的问题:

td {
    /* <http://www.w3.org/wiki/CSS/Properties/text-align>
     * left, right, center, justify, inherit
     */
    text-align: center;
    /* <http://www.w3.org/wiki/CSS/Properties/vertical-align>
     * baseline, sub, super, top, text-top, middle,
     * bottom, text-bottom, length, or a value in percentage
     */
    vertical-align: top;
}

回答by Xtian11

On particular table

在特定的桌子上

<table style="border-collapse: separate; border-spacing: 10px;" >
    <tr>
      <td>Hi</td>
      <td>Hello</td>
    <tr/>
    <tr>
      <td>Hola</td>
      <td>Oi!</td>
    <tr/>
</table>

回答by JaiSankarN

Alternatively, can use for particular table

或者,可以用于特定的表

 <table style="width:1000px; height:100px;">
    <tr>
        <td align="center" valign="top">Text</td> //Remove it
        <td class="tableFormatter">Text></td>
    </tr>
</table>

Add this css in external file

将此css添加到外部文件中

.tableFormatter
{
width:100%;
vertical-align:top;
text-align:center;
}