Simple html page with table

How To Create Tables in HTML

A table is a set of data organized by rows and columns. Tables are useful for displaying connections between data types, such as products and their cost, employment and dates employed, or flights and departure times. In this tutorial, you will create a table using HTML, customize it by adding a desired amount of rows and columns, and add row and column headings to make your table easier to read.

Prerequisites

  • Familiarity with HTML. If you are not familiar with HTML or need a refresher, you can review the first three tutorials of our How To Build a Website With HTML tutorial series.
  • An index.html file to practice creating HTML tables. If you do not know how to create an index.html file, please follow the instructions in our brief tutorial How To Set Up Your HTML Project.
Читайте также:  Java override with static method

Fundamentals of HTML tables

As an example, here is a table that has two rows and three columns:

table> tr> td>Column 1td> td>Column 2td> td>Column 3td> tr> tr> td>Column 1td> td>Column 2td> td>Column 3td> tr> table> 

To explore how HTML tables works in practice, paste the code snippet above into the index.html file or other html file you are using for this tutorial.

Save and reload the file in the browser to check your results. (For instructions on loading the file in your browser, please visit this step of our tutorial on HTML Elements.)

Your webpage should now have a table with three columns and two rows:

3 columns, 2 rows table

table> tr> td>Column 1td> td>Column 2td> td>Column 3td> tr> tr> td>Column 1td> td>Column 2td> td>Column 3td> tr> tr> td>Column 1td> td>Column 2td> td>Column 3td> tr> table> 

Save your results and check them in your browser. You should receive something like this:

3 Columns and 3 Rows Table

table> tr> td>Column 1td> td>Column 2td> td>Column 3td> td>Column 4td> tr> tr> td>Column 1td> td>Column 2td> td>Column 3td> td>Column 4 td> tr> tr> td>Column 1td> td>Column 2td> td>Column 3td> td>Column 4td> tr> table> 

Save your results and check them in your browser. Your webpage should display a table with three rows and four columns:

Webpage displaying table with three rows and four columns

Adding a Border To a Table

table border="1"> tr> td>Row 1td> td>Row 2td> td>Row 3td> tr> tr> td>Row 1td> td>Row 2td> td>Row 3td> tr> table> 

Add the highlighted border attribute to your table and checking your results in the browser. (You can clear your index.html file and paste in the HTML code snippet above.) Save your file and load it in the browser. Your table should now have a border surrounding each of your rows and columns like this:

Webpage displaying table with border

Adding Headings To Rows and Columns

Headings can be added to rows and columns to make tables easier to read. Table headings are automatically styled with bold and centered text to visually distinguish them from table data. Headings also make tables more accessible as they help individuals using screen readers navigate table data.

Clear the index.html file and add a row of column headings with the following code snippet:

table border="1"> tr> th>th> th>Column Header 1th> th>Column Header 2th> th>Column Header 3th> tr> table> 

Save the index.html file and reload it in your browser. You should receive something like this:

Webpage displaying HTML column headings

Your webpage should display a single row of column headers. Note that the first column header is empty. You may add a column header here if you like.

table border="1"> tr> th>th> th>Column Header 1th> th>Column Header 2th> th>Column Header 3th> tr> tr> th>Row Header 1th> td>Datatd> td>Datatd> td>Datatd> tr> tr> th>Row Header 2th> td>Datatd> td>Datatd> td>Datatd> tr> tr> th>Row Header 3th> td>Datatd> td>Datatd> td>Datatd> tr> table> 

Save the index.html file and reload it in your browser. You should receive something like this:

Webpage displaying table with column and row headings

You should now have a table with with three column headings and three row headings.

Conclusion

In this tutorial, you have created an HTML table, added additional rows and columns, and created headings for rows and columns.

If you are interested in learning more about HTML, you can check our our tutorial series How To Build a Website With HTML. To learn about how to use CSS to style HTML elements (including tables), please visit our tutorial series How To Build a Website With CSS.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

HTML Tables

HTML tables allow web developers to arrange data into rows and columns.

Example

Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Centro comercial Moctezuma Francisco Chang Mexico
Ernst Handel Roland Mendel Austria
Island Trading Helen Bennett UK
Laughing Bacchus Winecellars Yoshi Tannamuri Canada
Magazzini Alimentari Riuniti Giovanni Rovelli Italy

Define an HTML Table

A table in HTML consists of table cells inside rows and columns.

Example

Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Centro comercial Moctezuma Francisco Chang Mexico

Table Cells

Each table cell is defined by a

and a

tag.

Everything between

and

are the content of the table cell.

Example

Note: A table cell can contain all sorts of HTML elements: text, images, lists, links, other tables, etc.

Table Rows

Each table row starts with a

and ends with a

tag.

Example

You can have as many rows as you like in a table; just make sure that the number of cells are the same in each row.

Note: There are times when a row can have less or more cells than another. You will learn about that in a later chapter.

Table Headers

th stands for table header.

Example

Let the first row be table header cells:

By default, the text in elements are bold and centered, but you can change that with CSS.

HTML Exercises

HTML Table Tags

Tag Description
Defines a table
Defines a header cell in a table
Defines a row in a table
Defines a cell in a table
Defines a table caption
Specifies a group of one or more columns in a table for formatting
Specifies column properties for each column within a element
Groups the header content in a table
Groups the body content in a table
Groups the footer content in a table

For a complete list of all available HTML tags, visit our HTML Tag Reference.

LEARN MORE

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

How To Create HTML Tables (Very Simple Examples)

Welcome to a quick tutorial and examples on how to create HTML tables, and I suppose that everyone has seen tables everywhere online. But what is actually happening behind the scenes? How are tables created in HTML?

That covers the quick basics, but there are more to HTML tables – Read on for more examples!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

QUICK SLIDES

How To Create HTML Tables (Simple Examples)

TABLE OF CONTENTS

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

HTML TABLE EXAMPLES

All right, let us now get into the HTML table examples in this section.

1) CREATING A BASIC HTML TABLE

 
Name Email
Jon Doe jon@doe.com
Joy Doe joy@doe.com

Yes, these 4 tags are all you need to create a basic HTML table… Although is kind of optional.

2) TABLE SECTIONING – HEAD, BODY, FOOT

 tbody < background-color:lightyellow >tfoot 
Head 1 Head 2
Cell 1 Cell 2
Cell 3 Cell 4
Cell 5 Cell 6
Cell 7 Cell 8

3) COLUMN GROUPING

      First Column Second Column Third Column Fourth Column  First Cell Second Cell Third Cell Fourth Cell  

4) SPANNING ACROSS ROWS & COLUMNS

 First Column Span 2 columns  Span 2 rows A B  C D  
  • To span a cell across multiple columns, we add a colspan attribute.
  • To span a cell across multiple rows, we add a rowspan attribute.

5) TABLE CAPTION

 
Animals
Name Description
Doge Good boy.
Cate Evil one.

The tag adds a description to the table, and it must be right under the main tag. Not very useful though… Most people will just use a header tag.

6) TABLE-IN-TABLE

 
First Cell Second Cell
Third Cell
TABLE-IN-TABLE
ANOTHER ROW

Finally, for you guys who are curious – Yes, we can put a table inside a table.

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

SUMMARY – TABLE TAGS

SUMMARY – ATTRIBUTES

INFOGRAPHIC CHEAT SHEET

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment Cancel Reply

Breakthrough Javascript

Take pictures with the webcam, voice commands, video calls, GPS, NFC. Yes, all possible with Javascript — Check out Breakthrough Javascript!

Socials

About Me

W.S. Toh is a senior web developer and SEO practitioner with over 20 years of experience. Graduated from the University of London. When not secretly being an evil tech ninja, he enjoys photography and working on DIY projects.

Code Boxx participates in the eBay Partner Network, an affiliate program designed for sites to earn commission fees by linking to ebay.com. We also participate in affiliate programs with Bluehost, ShareASale, Clickbank, and other sites. We are compensated for referring traffic.

Источник

Оцените статью