Contents
HTML tables are a powerful tool for organizing and displaying data on a web page. Whether you're building a personal blog, an online store, or a business website, tables can help you present information in a clear and structured way.
In this tutorial, we'll cover everything you need to know to create and style HTML tables. We'll start by going over the basic structure of an HTML table and how to add rows and columns to it. From there, we'll dive into formatting table cells using HTML attributes and styling the table with CSS.
By the end of this tutorial, you'll have a solid understanding of how to design, structure, and style tables in HTML. Whether you're a beginner looking to enhance your web development skills or an experienced developer looking to refresh your knowledge, this tutorial will provide you with the necessary tools to create professional-looking tables for your website.
So, let's dive in and explore the world of HTML tables together!
Before we start designing and styling HTML tables, it's essential to understand their basic structure. HTML tables are made up of three main components: the table element, the table row element, and the table cell element.
The table element is used to define the entire table, and it is denoted by the opening <table> tag and the closing </table> tag. The table row element, denoted by the <tr> tag, is used to define each row in the table. Finally, the table cell element, denoted by the <td> tag, is used to define each cell within a row.
Let's take a closer look at the basic structure of an HTML table using an example:
<table>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
In this example, we have a table with two rows and two cells per row. Notice that the table is defined using the <table> element, and each row is defined using the <tr> element. The <td> element is used to define each cell within a row.
Understanding the basic structure of an HTML table is crucial to creating tables that are easy to read and navigate. In the next section, we'll explore how to add rows and columns to your table.
Now that we understand the basic structure of an HTML table, let's explore how to add rows and columns to it.
To add a new row to your table, you can use the <tr> element. For example:
<table>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
<tr>
<td>Row 3, Cell 1</td>
<td>Row 3, Cell 2</td>
</tr>
</table>
In this example, we added a third row to our table. Notice that the <tr> element is used to define the new row, and each cell in the row is defined using the <td> element.
To add a new column to your table, you can use the <th> or <td> element, depending on whether the column represents a header or a regular cell. For example:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
In this example, we added a new header column to our table. Notice that we used the <th> element to define the new header column, and each regular cell in the table is defined using the <td> element.
Now that we know how to add rows and columns to our table, let's explore how to format the cells using HTML attributes in the next section.
HTML provides a variety of attributes that you can use to format and style the cells in your table. Let's explore some of the most commonly used attributes:
<table>
<tr>
<td rowspan="2">Cell 1, Spanning 2 Rows</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 2</td>
</tr>
</table>
In this example, we used the rowspan attribute to make the first cell span two rows.
<table>
<tr>
<td colspan="2">Cell 1, Spanning 2 Columns</td>
<td>Row 1, Cell 3</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
<td>Row 2, Cell 3</td>
</tr>
</table>
In this example, we used the colspan attribute to make the first cell span two columns.
<table>
<tr>
<td align="left">Left-Aligned Cell</td>
<td align="center">Center-Aligned Cell</td>
<td align="right">Right-Aligned Cell</td>
</tr>
</table>
In this example, we used the align attribute to specify the horizontal alignment of each cell.
These are just a few examples of the many attributes that you can use to format and style the cells in your table. In the next section, we'll explore how to style the entire table using CSS.
While HTML attributes are great for formatting individual cells in your table, CSS provides a more comprehensive way to style the entire table. Let's explore how to use CSS to style your HTML table.
To style your table with CSS, you can use the "table" selector. For example:
<style>
table {
border-collapse: collapse;
width: 100%;
}
table td, table th {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
In this example, we use the "table" selector to apply styling to the entire table. We set the "border-collapse" property to "collapse" to remove the default spacing between cells, and we set the "width" property to "100%" to make the table width expand to fill the available space.
We also use the "table td, table th" selector to apply styling to all the cells in the table. We set the "border" property to "1px solid black" to add a border around each cell, and we set the "padding" property to "8px" to add some space between the cell content and the cell border. Finally, we set the "text-align" property to "left" to align the cell content to the left.
By using CSS to style your HTML table, you can create tables that are both functional and visually appealing. In the next section, we'll explore how to create nested tables.
Sometimes you may need to include a table within a table. This is known as a nested table. Let's explore how to create a nested table in HTML.
To create a nested table, you can simply include another <table> element within a table cell (<td>). For example:
<table>
<tr>
<td>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
In this example, we have a main table that contains a nested table within the first cell of the first row. The nested table has its own headers and rows, and it is indented within the main table.
Nested tables can be a useful way to organize complex data in a structured way. However, it's essential to use them sparingly, as they can make your HTML code more difficult to read and maintain.
In the next section, we'll explore some best practices for using tables in HTML.
While HTML tables are a powerful tool for organizing and displaying data on a web page, it's essential to use them correctly to ensure that your website is accessible and user-friendly. Here are some best practices to keep in mind when using tables in HTML:
Use tables for tabular data only - Tables should be used to display tabular data, such as pricing information or product specifications. They should not be used for layout purposes, as this can make your HTML code more difficult to read and maintain.
Use table headers - When using tables for tabular data, it's essential to use the <th> element to define the table headers. This helps screen readers and other assistive technologies understand the structure of the table and make it more accessible to users with disabilities.
Use appropriate table markup - Make sure to use appropriate HTML markup when creating tables. Use the <thead>, <tbody>, and <tfoot> elements to organize the table headers, body, and footer, respectively.
Avoid nested tables - While nested tables can be useful in some situations, they can also make your HTML code more difficult to read and maintain. Use them sparingly and only when necessary.
Style your tables with CSS - Use CSS to style your tables, instead of relying on HTML attributes. This makes your HTML code more modular and easier to maintain.
By following these best practices, you can create tables that are both accessible and user-friendly, enhancing the overall user experience of your website.
In this tutorial, we covered everything you need to know to create and style HTML tables. We explored the basic structure of an HTML table, how to add rows and columns, how to format cells using HTML attributes, and how to style the entire table with CSS. We also explored how to create nested tables and best practices for using tables in HTML.
Tables are a powerful tool for organizing and displaying data on a web page, and by using them correctly, you can create professional-looking tables that are both accessible and user-friendly. With the skills you've learned in this tutorial, you can take your web development skills to the next level and create beautiful tables for your website.
The Web Design : An Introduction is a beginner level PDF e-book tutorial or course with 20 pages. It was added on December 14, 2015 and has been downloaded 13387 times. The file size is 504.58 KB. It was created by csus.edu.
The HTML a Crash Course is a beginner level PDF e-book tutorial or course with 41 pages. It was added on December 9, 2012 and has been downloaded 18620 times. The file size is 925.15 KB. It was created by Marty Hall.
The Easy Web Design is a beginner level PDF e-book tutorial or course with 54 pages. It was added on December 2, 2017 and has been downloaded 22222 times. The file size is 1.72 MB. It was created by Jerry Stratton.
The Carnival of HTML is a beginner level PDF e-book tutorial or course with 34 pages. It was added on February 3, 2017 and has been downloaded 12095 times. The file size is 1.45 MB. It was created by Jerry Stratton.
The HTML, CSS, Bootstrap, Javascript and jQuery is a beginner level PDF e-book tutorial or course with 72 pages. It was added on November 12, 2018 and has been downloaded 61185 times. The file size is 652.78 KB. It was created by Meher Krishna Patel.
The Cascading style sheets (CSS) is a beginner level PDF e-book tutorial or course with 62 pages. It was added on December 9, 2012 and has been downloaded 12329 times. The file size is 739.41 KB. It was created by Oxford.
The A Guide to HTML5 and CSS3 is a beginner level PDF e-book tutorial or course with 73 pages. It was added on October 14, 2014 and has been downloaded 44896 times. The file size is 779.08 KB. It was created by Ashley Menhennett, Pablo Farias Navarro.
The A beginner's guide to computer programming is level PDF e-book tutorial or course with 352 pages. It was added on September 7, 2013 and has been downloaded 14273 times. The file size is 1.13 MB.
The Adobe Photoshop Tutorial is a beginner level PDF e-book tutorial or course with 37 pages. It was added on September 4, 2013 and has been downloaded 341133 times. The file size is 616.34 KB. It was created by Unknown.
The Java for small teams is a beginner level PDF e-book tutorial or course with 143 pages. It was added on December 18, 2016 and has been downloaded 924 times. The file size is 894.99 KB. It was created by Henry Coles.
The Creating a website using Dreamweaver MX is a beginner level PDF e-book tutorial or course with 41 pages. It was added on June 22, 2016 and has been downloaded 8763 times. The file size is 405.84 KB. It was created by university bristol.
The CSS Cascading Style Sheets is a beginner level PDF e-book tutorial or course with 40 pages. It was added on December 2, 2017 and has been downloaded 8321 times. The file size is 1.85 MB. It was created by Jerry Stratton.
The Access Database Design is a beginner level PDF e-book tutorial or course with 22 pages. It was added on December 20, 2013 and has been downloaded 6230 times. The file size is 322.26 KB. It was created by West Virginia University.
The Learning HTML is a beginner level PDF e-book tutorial or course with 163 pages. It was added on May 2, 2019 and has been downloaded 55654 times. The file size is 862.98 KB. It was created by Stack Overflow Documentation.
The Creating web pages in XHTML is level PDF e-book tutorial or course with 36 pages. It was added on December 9, 2012 and has been downloaded 14044 times. The file size is 470.09 KB.
The Microsoft Word 2013 Introduction to Styles is an intermediate level PDF e-book tutorial or course with 23 pages. It was added on July 14, 2014 and has been downloaded 5983 times. The file size is 742.04 KB. It was created by The University of Queensland Library.
The Responsive Web Design is a beginner level PDF e-book tutorial or course with 30 pages. It was added on October 14, 2014 and has been downloaded 21151 times. The file size is 420.52 KB. It was created by Tim Davison.
The Advanced Computer Architecture is an advanced level PDF e-book tutorial or course with 195 pages. It was added on September 27, 2017 and has been downloaded 7270 times. The file size is 1.36 MB. It was created by Rai Technology University.
The An Introduction to Web Design is a beginner level PDF e-book tutorial or course with 20 pages. It was added on December 5, 2013 and has been downloaded 9490 times. The file size is 504.58 KB. It was created by California State University.
The Designing your database is a beginner level PDF e-book tutorial or course with 11 pages. It was added on August 13, 2014 and has been downloaded 6913 times. The file size is 157.68 KB. It was created by University of Bristol Information Services.
The LibreOffice 4.0 Writer Guide is level PDF e-book tutorial or course with 468 pages. It was added on December 10, 2013 and has been downloaded 4192 times. The file size is 12.22 MB.
The Word 2013: Creating Tables is a beginner level PDF e-book tutorial or course with 3 pages. It was added on November 16, 2015 and has been downloaded 6663 times. The file size is 94.26 KB. It was created by Montclaire state univesity.
The Building an E-Commerce Website with Bootstrap is a beginner level PDF e-book tutorial or course with 36 pages. It was added on January 19, 2016 and has been downloaded 14242 times. The file size is 432.61 KB. It was created by unknown.
The Adobe Dreamweaver CS6 Tutorial is a beginner level PDF e-book tutorial or course with 18 pages. It was added on February 21, 2014 and has been downloaded 18061 times. The file size is 374.04 KB. It was created by unknown.
The Access 2016 - Relational Databases & Subforms is an intermediate level PDF e-book tutorial or course with 21 pages. It was added on September 30, 2016 and has been downloaded 3535 times. The file size is 589.62 KB. It was created by Kennesaw State University.
The Accessibility Features In Microsoft Excel 2010 is an advanced level PDF e-book tutorial or course with 21 pages. It was added on October 19, 2015 and has been downloaded 2268 times. The file size is 700.28 KB. It was created by Kennesaw State University.
The Advanced Microsoft Word 2011 for MAC is an advanced level PDF e-book tutorial or course with 24 pages. It was added on July 14, 2014 and has been downloaded 1596 times. The file size is 4.49 MB. It was created by University of Queensland Library 2010.
The Introduction to Cascading Style Sheets is level PDF e-book tutorial or course with 58 pages. It was added on December 9, 2012 and has been downloaded 10027 times. The file size is 521.64 KB.
The AES The Advanced Encryption Standard is a beginner level PDF e-book tutorial or course with 88 pages. It was added on November 22, 2017 and has been downloaded 790 times. The file size is 443.81 KB. It was created by Avinash Kak.
The Basic CSS is level PDF e-book tutorial or course with 24 pages. It was added on December 9, 2012 and has been downloaded 9012 times. The file size is 50.99 KB.