Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. HTML/CSS
  4. Attribute of table in HTML

Attribute of table in HTML

Date- Dec 09,2023 Updated Jan 2026 3060
html css

Overview of the <table> Tag

The <table> tag in HTML is a powerful tool for displaying data in a structured format, making it easier for users to comprehend complex information. Tables are widely used in web pages for various purposes, such as displaying financial reports, schedules, and other structured data. By utilizing the attributes of the <table> tag, developers can enhance the visual appeal and functionality of tables, ensuring they meet both aesthetic and usability standards.

In real-world applications, tables can represent anything from a simple list of items to intricate datasets that require sorting and filtering. For instance, e-commerce websites often use tables to display product listings with prices, descriptions, and ratings. Understanding how to effectively use the <table> tag and its attributes is vital for any web developer aiming to create user-friendly and accessible web applications.

Commonly Used Attributes of the <table> Tag

Here are some commonly used attributes of the <table> tag:

  1. border: The border attribute is used to set the border width around the table and its cells. This attribute is often deprecated in favor of CSS, but it can still be found in legacy code.
<table border="1"></table>

In modern web development, a border can be styled using CSS:

<style>
  table {
    border-collapse: collapse;
    width: 100%;
  }
  th, td {
    border: 1px solid #000;
  }
</style>
<table></table>
  1. cellpadding: This attribute specifies the gap between the cell content and the cell border. It can enhance readability by adding space around text.
<table cellpadding="5"></table>

Again, CSS is the preferred way to achieve this:

<style>
  td {
    padding: 10px;
  }
</style>
<table></table>
  1. cellspacing: This attribute specifies the space between table cells. Similar to cellpadding, it is better handled with CSS.
<table cellspacing="5"></table>

Use CSS for more control:

<style>
  table {
    border-spacing: 5px;
  }
</style>
<table></table>
  1. width: The width attribute specifies the table's width, which can be set in pixels or percentages.
<table width="100%"></table>

CSS provides more flexibility:

<style>
  table {
    width: 80%;
  }
</style>
<table></table>
  1. height: The height attribute sets the height of the table. Like width, it is better handled with CSS.
<table height="200"></table>

CSS example:

<style>
  table {
    height: 300px;
  }
</style>
<table></table>
  1. align: The align attribute aligns the table in relation to surrounding content (left, center, right).
<table align="center"></table>

CSS can also achieve alignment:

<style>
  .center {
    margin: auto;
  }
</style>
<table class="center"></table>
  1. summary: The summary attribute provides a brief description of the table's content for accessibility purposes, helping screen readers convey information to visually impaired users.
<table summary="Monthly sales data"></table>
  1. bordercolor: This attribute sets the color of the table border. However, modern best practices recommend using CSS for styling.
<table bordercolor="#CCCCCC"></table>

CSS example:

<style>
  table {
    border: 1px solid #CCCCCC;
  }
</style>
<table></table>
  1. bgcolor: The bgcolor attribute sets the background color of the table.
<table bgcolor="#F5F5F5"></table>

CSS example:

<style>
  table {
    background-color: #F5F5F5;
  }
</style>
<table></table>

Using CSS for Table Styling

While HTML attributes provide basic formatting options, CSS is the recommended approach for styling tables. CSS allows for more granular control over the appearance of tables, including properties such as border, padding, margin, and background color. By separating style from content, CSS enhances maintainability and scalability.

For example, consider a table displaying user information. Instead of using HTML attributes, a CSS stylesheet can define styles that apply globally, ensuring consistency across all tables on a website:

<style>
  table {
    border-collapse: collapse;
    width: 100%;
  }
  th, td {
    border: 1px solid #ddd;
    padding: 8px;
  }
  th {
    background-color: #f2f2f2;
    text-align: left;
  }
</style>
<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
  </tr>
  <tr>
    <td>John Doe</td>
    <td>john@example.com</td>
  </tr>
</table>

Edge Cases & Gotchas

When working with tables in HTML, there are several edge cases and common pitfalls to be aware of:

  • Deprecated Attributes: Many of the attributes mentioned are considered deprecated in HTML5. Relying on them may lead to compatibility issues with modern browsers.
  • Accessibility: Always ensure that tables are accessible. Use the summary attribute and consider using scope attributes in <th> elements to define header relationships.
  • Responsive Design: Tables can be challenging to make responsive. Consider using CSS media queries or frameworks like Bootstrap to ensure tables are displayed correctly on various screen sizes.

Performance & Best Practices

To optimize the performance and maintainability of tables in HTML, follow these best practices:

  • Use CSS for Styling: As previously mentioned, avoid using HTML attributes for styling. Use CSS for a cleaner separation of content and design.
  • Semantic HTML: Use semantic elements such as <thead>, <tbody>, and <tfoot> to enhance the semantic structure of tables, making them more accessible and easier to style.
  • Minimize Complexity: Keep tables simple. Avoid nesting tables within tables unless absolutely necessary, as this can complicate layout and styling.
  • Test Across Browsers: Always test your tables across different browsers and devices to ensure consistent rendering and functionality.

Conclusion

In conclusion, the <table> tag in HTML is a fundamental element for displaying structured data effectively. By understanding its attributes and leveraging CSS for styling, developers can create visually appealing and accessible tables. Here are the key takeaways:

  • Utilize the <table> tag to display data in a structured format.
  • Prefer CSS for styling over deprecated HTML attributes.
  • Ensure accessibility by providing summaries and using semantic HTML elements.
  • Be mindful of responsive design when working with tables.
  • Follow best practices to enhance performance and maintainability.
Attribute of table in HTMLAttribute of table in HTML 2

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Complete Guide to CSS Assignment 1: Step-by-Step Explained
Dec 09, 2023
Mastering Chapter 1 CSS: A Complete Guide with Examples in HTML/CSS
Dec 09, 2023
Anchor and Image tags in HTML
Dec 09, 2023
Countown Timer in Javascript
Sep 17, 2022
Previous in HTML/CSS
Complete Guide to Creating Tables in HTML/CSS with Examples
Next in HTML/CSS
Assignment To Merge Column And Rows In a table
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your HTML/CSS interview with curated Q&As for all levels.

View HTML/CSS Interview Q&As

More in HTML/CSS

  • Responsive Slick Slider 23082 views
  • How to add a WhatsApp share button on a website 19366 views
  • Slick Slider with single slide 18067 views
  • Code syntax higlighter using Prism js 13669 views
  • Create a Music Player in HTML/CSS: Step-by-Step Guide 9399 views
View all HTML/CSS posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor