Welcome to the hands-on lab for creating tables in HTML! Tables are a great way to organize and display data on a webpage. In this lab, we will learn how to create and style tables using HTML. We will be using the JDoodle HTML editor for this exercise.
By the end of this lab, you will be able to:
In the editor, type the following code to create the basic structure of an HTML document:
<!DOCTYPE html>
<html>
<head>
<title>HTML Tables</title>
</head>
<body>
<!-- Table content will go here -->
</body>
</html>
This code sets up a basic HTML document with a <head>
and <body>
section.
Inside the <body>
tags, add the following code to create a simple table:
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>John Doe</td>
<td>25</td>
<td>New York</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>30</td>
<td>Los Angeles</td>
</tr>
</table>
This code creates a table with a border and three columns:
<table>
: The container for the table.<tr>
: Defines a table row.<th>
: Defines a header cell in a table.<td>
: Defines a standard data cell in a table.Add more rows to your table by inserting additional <tr>
tags with <td>
elements:
<tr>
<td>Emily Davis</td>
<td>22</td>
<td>Chicago</td>
</tr>
<tr>
<td>Michael Brown</td>
<td>28</td>
<td>Houston</td>
</tr>
Add this code inside the <table>
tags after the existing rows.
Add a caption to your table to provide a title:
<table border="1">
<caption>Student Information</caption>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<!-- Existing rows go here -->
</table>
Add some inline CSS to improve the table's appearance:
<style>
table {
width: 50%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
Insert this <style>
block inside the <style>
tags in the Jdoodle Editor.
Congratulations! You have successfully created a table in HTML and enhanced it with additional rows and basic styling. You now know how to set up a table structure, add data, and apply simple styles to make it more visually appealing. Keep experimenting with different table properties and styles to further improve your skills. Happy coding!