不推荐使用 HTML 属性 bgcolor:改用什么?

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

HTML attribute bgcolor is deprecated: What To Use Instead?

asp.nethtml

提问by Anthony K

VStudio ASP.NET gives the following message:

VStudio ASP.NET 给出以下消息:

Attribute 'bgcolor' is considered outdated. A newer construct is recommended.

What is the recommended construct?

推荐的构造是什么?

bgcoloris within a <td>element.
Another related message is :

bgcolor在一个<td>元素内。
另一个相关消息是:

Attribute 'bordercolor' is not a valid attribute of element 'table'.  

Does anyone know where I might find the newer replacements?

有谁知道我在哪里可以找到更新的替代品?

回答by George Stocker

BGColorwas deprecated in the W3C HTML 4.0 Specification.

BGColor在 W3C HTML 4.0 规范中已弃用。

Newer Web sites and web applications use CSS (Cascading Style Sheets) to render the same thing, as follows:

较新的网站和 Web 应用程序使用 CSS(级联样式表)来呈现相同的内容,如下所示:

   body {
  background-color : #ffffff;
}

For tables, do the following:

对于表,请执行以下操作:

<table>

<tr id="row1">
   <th>Header 1</th>      <td>Cell 1</td>        <td>Cell 2</td>
</tr>
<tr id="row2">
   <th>Header 2</th>      <td>Cell 3</td>        <td>Cell 4</td>
</tr>
<tr id="row3">
   <th>Header 3</th>      <td>Cell 5</td>        <td>Cell 6</td>
</tr>
</table>

And in your CSS:

在你的 CSS 中:

th { text-align: center; font-weight: bold; vertical-align: baseline }

td { vertical-align: middle  }

table  { border-collapse: collapse; background-color: #ffffff }
tr#row1 { border-top: 3px solid blue }
tr#row2 { border-top: 1px solid black }
tr#row3 { border-top: 1px solid black }

That will make it so the table will have a background color, and do different stuff with the rest of the table data/table rows.

这将使表格具有背景颜色,并对表格数据/表格行的其余部分执行不同的操作。

Simply put that in your style sheet and reference it on your web page like so:

只需将它放在您的样式表中并在您的网页上引用它,如下所示:

<link rel="stylesheet" href="style.css" TYPE="text/css" media="screen">

You can put just about whatever you like in your CSS, more information on CSS here, and here.

您可以在 CSS 中放入您喜欢的任何内容,有关 CSS 的更多信息在这里这里

回答by Jonathan Lonowski

Best guess would be CSS's background-colorand border-color:

最好的猜测是 CSSbackground-colorborder-color

<table style="border-color: #ffffff;">

<td style="background-color: #000000;">

回答by danieltalsky

It's also worth noting, that while not as elegant as a seperate style section, it is valid now to do it this way, with inline styles, if this is what you're more comfortable with:

还值得注意的是,虽然不像单独的样式部分那样优雅,但现在使用内联样式这样做是有效的,如果这是您更喜欢的方式:

<body style="background-color: #ccc;">

回答by Tmdean

The newer replacement is cascading style sheets (CSS). Any attributes or elements that control the visual appearance of an HTML document are deprecated. Visual styles should be specified using CSS.

较新的替代品是级联样式表 (CSS)。不推荐使用任何控制 HTML 文档视觉外观的属性或元素。应使用 CSS 指定视觉样式。

回答by Jeromy Irvine

The recommended way to do things like this is to use CSS. You could set up CSS classes for your table. Something like this:

执行此类操作的推荐方法是使用 CSS。您可以为您的表格设置 CSS 类。像这样的东西:

CSS:

CSS:

.MyTable {
    border: solid 2px #000;
}

.MySpecialCell {
    background-color: #F00;
}

HTML:

HTML:

<table class="MyTable">
    <tr>
        <td class="MySpecialCell">...</td>
    </tr>
</table>