How to Add a Border to an HTML Table

秋墨君_9115

秋墨君_9115

2026-06-07

266人浏览

原创

How to Add a Border to an HTML Table

This tutorial explains how to properly add visible borders to HTML tables using both legacy border attributes and modern CSS techniques—with clear examples, common pitfalls, and best practices.

this tutorial explains how to properly add visible borders to html tables using both legacy `border` attributes and modern css techniques—with clear examples, common pitfalls, and best practices.

Adding a visible border to an HTML table is a fundamental styling task—but it’s also a frequent source of confusion, especially for beginners. In your original code, the

element has no border defined, so browsers render it without visible borders by default—even though the table structure is perfectly valid.

✅ Quick Fix: The Legacy border Attribute

The simplest (but outdated) way is to add the border attribute directly to the

tag:
Name Courses Marks
Suresh Btech 89
Mukesh MBA 75
Kamlesh BA 90

⚠️ Note: The border attribute is obsolete in HTML5. While it still works in most browsers for backward compatibility, it’s not recommended for new projects—it mixes presentation with structure and offers limited control (e.g., no border color, style, or individual cell customization).

CSS婚礼策划服务机构宣传网站模板
CSS婚礼策划服务机构宣传网站模板

CSS婚礼策划服务机构宣传网站模板是一款适合提供婚礼策划和婚庆服务机构宣传网站模板下载。提示:本模板调用到谷歌字体库,可能会出现页面打开比较缓慢。

下载

✅ Recommended Approach: CSS Borders

Use external or internal CSS for full control, semantic clarity, and responsiveness:

<style>
  #table {
    border-collapse: collapse; /* Ensures borders merge cleanly */
    width: 100%;
  }
  #table th,
  #table td {
    border: 1px solid #333; /* Uniform border on all cells */
    padding: 8px;
    text-align: left;
  }
  #table th {
    background-color: #f2f2f2;
  }
</style>

? Key CSS properties explained:

  • border-collapse: collapse prevents double borders between adjacent cells (the default separate causes gaps).
  • border: 1px solid #333 applies a thin, solid, dark-gray border to each and .
  • You can further customize with border-radius, box-shadow, or hover effects—impossible with the border attribute.
  • ? Pro Tips & Common Pitfalls

    • ❌ Never omit closing tags like or
    — your original code omitted them, which may cause inconsistent rendering. Always close table cell and row tags.
  • ✅ Use semantic table structure: , , and improve accessibility and SEO.
  • ? Avoid border="0" when you want no border — use CSS border: none instead for consistency.
  • ? Debug tip: Inspect the table in browser DevTools (F12) to verify whether borders are applied—and whether border-collapse is set correctly.
  • In summary: while border="1" gets you a quick visual result, embracing CSS-based borders ensures maintainability, design flexibility, and standards compliance. Start with border-collapse: collapse and build from there.

前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!

相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

css

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
html版权符号
html版权符号

html版权符号是“©”,可以在html源文件中直接输入或者从word中复制粘贴过来,php中文网还为大家带来html的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

2023.06.14

5895

7

html在线编辑器
html在线编辑器

html在线编辑器是用于在线编辑的工具,编辑的内容是基于HTML的文档。它经常被应用于留言板留言、论坛发贴、Blog编写日志或等需要用户输入普通HTML的地方,是Web应用的常用模块之一。php中文网为大家带来了html在线编辑器的相关教程、以及相关文章等内容,供大家免费下载使用。

2023.06.21

3332

4

html网页制作
html网页制作

html网页制作是指使用超文本标记语言来设计和创建网页的过程,html是一种标记语言,它使用标记来描述文档结构和语义,并定义了网页中的各种元素和内容的呈现方式。本专题为大家提供html网页制作的相关的文章、下载、课程内容,供大家免费下载体验。

2023.07.31

3010

5

html空格
html空格

html空格是一种用于在网页中添加间隔和对齐文本的特殊字符,被用于在网页中插入额外的空间,以改变元素之间的排列和对齐方式。本专题为大家提供html空格的相关的文章、下载、课程内容,供大家免费下载体验。

2023.08.01

3019

5

html是什么
html是什么

HTML是一种标准标记语言,用于创建和呈现网页的结构和内容,是互联网发展的基石,为网页开发提供了丰富的功能和灵活性。本专题为大家提供html相关的各种文章、以及下载和课程。

2023.08.11

4819

6

html字体大小怎么设置
html字体大小怎么设置

在网页设计中,字体大小的选择是至关重要的。合理的字体大小不仅可以提升网页的可读性,还能够影响用户对网页整体布局的感知。php中文网将介绍一些常用的方法和技巧,帮助您在HTML中设置合适的字体大小。

2023.08.11

2981

3

html转txt
html转txt

html转txt的方法有使用文本编辑器、使用在线转换工具和使用Python编程。本专题为大家提供html转txt相关的文章、下载、课程内容,供大家免费下载体验。

2023.08.31

2769

3

html文本框代码怎么写
html文本框代码怎么写

html文本框代码:1、单行文本框【<input type="text" style="height:..;width:..;" />】;2、多行文本框【textarea style=";height:;"></textare】。

2023.09.01

2468

6

HTML嵌入CSS样式的方法
HTML嵌入CSS样式的方法

HTML嵌入CSS样式的方法有内联样式、内部样式表和外部样式表。本专题为大家提供CSS样式相关的文章、下载、课程内容,供大家免费下载体验。

2023.09.20

2508

5

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 1.5万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 5.9万人学习

CSS教程
CSS教程

共754课时 | 96.8万人学习