HTML Table Basics

We can create tables on the web browser. The HTML tagle is table
The basic structure of a HTML table is the table row tag, the table head tag and the table data tag. The table head and table data tags work just like columns in Excel.

Let's Experiment with table, table row and table data tags.

Here is a table constructed with table row and data.
ID Name Age
1 John 30
2 John 30
3 John 30
<table>
	<tr>
		<td>ID</td>
		<td>Name</td>
		<td>Age</td>
	</tr>
	<tr>
		<td>1</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr>
		<td>2</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr>
		<td>3</td>
		<td>John</td>
		<td>30</td>
	</tr>
</table>

No Borders

The problem with the vanilla HTML table is that the table does not come with borders. Let's add borders.
The border assigned to the table tag will be the outer part of the table.
ID Name Age
1 John 30
2 John 30
3 John 30
<table style="border: 1px solid;">
	<tr>
		<td>ID</td>
		<td>Name</td>
		<td>Age</td>
	</tr>
	<tr>
		<td>1</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr>
		<td>2</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr>
		<td>3</td>
		<td>John</td>
		<td>30</td>
	</tr>
</table>
The tr tag can be assigned with a border
ID Name Age
1 John 30
2 John 30
3 John 30
<table style="border: 1px solid;">
	<tbody><tr style="border: 10px solid;">
		<td>ID</td>
		<td>Name</td>
		<td>Age</td>
	</tr>
	<tr style="border: 1px solid;">
		<td>1</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr>
		<td>2</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr>
		<td>3</td>
		<td>John</td>
		<td>30</td>
	</tr>
</tbody></table>
The td tag can be assigned with a border. Here td and th tags will work the same way.
ID Name Age
1 John 30
2 John 30
3 John 30
<table style="border: 1px solid;">
	<tbody><tr>
		<td style="border: 1px solid;">ID</td>
		<td style="border: 1px solid;">Name</td>
		<td style="border: 1px solid;">Age</td>
	</tr>
	<tr style="border: 1px solid;">
		<td style="border: 1px solid;">1</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr>
		<td style="border: 1px solid;">2</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr>
		<td style="border: 1px solid;">3</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
</tbody></table>

The Table Looks Like There are More Borders Than Needed

When we assigned the table th, and td tags with a border, the table will have its own border as the td tags do. If we want to merge the outer border into the table data border, we can add a collapse CSS property to the table tag.
ID Name Age
1 John 30
2 John 30
3 John 30
<table style="border-collapse: collapse; border: 1px solid; width: 100%;">
	<tr>
		<td style="border: 1px solid;">ID</td>
		<td style="border: 1px solid;">Name</td>
		<td style="border: 1px solid;">Age</td>
	</tr>
	<tr style="border: 1px solid;">
		<td style="border: 1px solid;">1</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr>
		<td style="border: 1px solid;">2</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr>
		<td style="border: 1px solid;">3</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
</table>

Basic Table Styling

Now we have created a regular looking table, we can make it look better. We can make the table wider.
ID Name Age
1 John 30
2 John 30
3 John 30
<table style="border-collapse: collapse; border: 1px solid; width: 100%;">
	<tr>
		<td style="border: 1px solid;">ID</td>
		<td style="border: 1px solid;">Name</td>
		<td style="border: 1px solid;">Age</td>
	</tr>
	<tr style="border: 1px solid;">
		<td style="border: 1px solid;">1</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr>
		<td style="border: 1px solid;">2</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr>
		<td style="border: 1px solid;">3</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
</table>

Add Table Head

We can replay the td tags in the first column with th tags. The th tags will automatically make their text-align CSS property to center, so we can also assign the text-align:center to all td tags. Although the tr tags can not be assigned with a border, they can be assigned with text-align CSS property.
ID Name Age
1 John 30
2 John 30
3 John 30
<table style="border-collapse: collapse; border: 1px solid; width: 100%;">
	<tr style="text-align: center;">
		<th style="border: 1px solid;">ID</th>
		<th style="border: 1px solid;">Name</th>
		<th style="border: 1px solid;">Age</th>
	</tr>
	<tr style="text-align: center;">
		<td style="border: 1px solid;">1</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr style="text-align: center;">
		<td style="border: 1px solid;">2</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
	<tr style="text-align: center;">
		<td style="border: 1px solid;">3</td>
		<td style="border: 1px solid;">John</td>
		<td style="border: 1px solid;">30</td>
	</tr>
</table>

Examples

ID Name Age
1 John 30
2 John 30
3 John 30
<style>
	.myTable_1{
	 border:1px solid;
	 border-collapse:collapse;
	 width:100%;
	}
	
	.myTable_1 tr{
		text-align:center
	}

	.myTable_1 td,.myTable_1 th{
		border:1px solid
	}
</style>
<table class="myTable_1">
	<tr>
		<th>ID</th>
		<th>Name</th>
		<th>Age</th>
	</tr>
	<tr>
		<td>1</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr style="text-align: center;">
		<td>2</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr style="text-align: center;">
		<td>3</td>
		<td>John</td>
		<td>30</td>
	</tr>
</table>

ID Name Age
1 John 30
2 John 30
3 John 30
<style>
	.myTable_2{
	 border: 1.8px solid blue;
	 border-collapse:collapse;
	 width:100%;
	}
	
	.myTable_2 tr{
		text-align:center
	}

	.myTable_2 td,.myTable_2 th{
		border:1px solid blue
	}
</style>
<table class="myTable_2">
	<tr>
		<th>ID</th>
		<th>Name</th>
		<th>Age</th>
	</tr>
	<tr>
		<td>1</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr style="text-align: center;">
		<td>2</td>
		<td>John</td>
		<td>30</td>
	</tr>
	<tr style="text-align: center;">
		<td>3</td>
		<td>John</td>
		<td>30</td>
	</tr>
</table>

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser