如何在 HTML 中显示元素的背景颜色?

htmlweb developmentfront end technology更新于 2025/8/14 16:12:17

在 HTML 中使用 bgcolor属性显示元素的背景颜色。该属性用于控制 HTML 元素的背景,特别是页面主体和表格的背景。

注意 − HTML5 不支持此属性。

示例

您可以尝试运行以下代码,了解如何在 HTML 中实现 bgcolor 属性 −

<!DOCTYPE html>
<html>
   <head>
      <title>HTML Background Colors</title>
   </head>
   <body>
      <!-- Format 1 - Use color name -->
      <table bgcolor = "yellow" width = "100%">
         <tr>
            <td>
               This background is yellow
            </td>
         </tr>
      </table>

      <!-- Format 2 - Use hex value -->
      <table bgcolor = "#6666FF" width = "100%">
         <tr>
            <td>
               This background is sky blue
            </td>
         </tr>
      </table>

      <!-- Format 3 - Use color value in RGB terms -->
      <table bgcolor = "rgb(255,0,255)" width = "100%">
         <tr>
            <td>
               This background is green
            </td>
         </tr>
      </table>
   </body>
</html>

相关文章


有用资源