ICT Lab 7
ICT Lab 7
Website Development
Tables in HTML :
Tables in HTML are a great way to organize information into rows and columns.
Here, we’ll build a basic table for a hypothetical monthly budget.
<table>
<tr>
<th>Month</th>
<th>Rent</th>
<th>Utilities</th>
<th>Groceries</th>
<th>Eating Out</th>
<th>Entertainment</th>
</tr>
</table>
Add Data Cells (<td>)
For the next row, add data values using the <td> tag. For example, here’s how the
data for the month of August might look:
<table>
<tr>
<th>Month</th>
<th>Rent</th>
<th>Utilities</th>
<th>Groceries</th>
<th>Eating Out</th>
<th>Entertainment</th>
</tr>
<tr>
<td>August</td>
<td>$1500</td>
<td>$150</td>
<td>$350</td>
<td>$100</td>
<td>$50</td>
</tr>
</table>
Let us see how this looks. And here is our table:
We can enhance the appearance of the table with a few basic attributes:
1. Border
The border attribute creates a line around the entire table and between each
cell. Adding border="1" to the <table> tag sets this border to a 1-pixel
thickness.
2. Cell Padding
The cellpadding attribute controls the space between the cell content and the
cell border. Adding cellpadding="10" makes the text less cramped by adding
padding inside each cell.
3. Cell Spacing
The cellspacing attribute adjusts the space between cells. Setting
cellspacing="0" removes any space between cells, creating a more seamless
look.
Here's an example:
<tr>
<th>Month</th>
<th>Rent</th>
<th>Utilities</th>
<th>Groceries</th>
<th>Eating Out</th>
<th>Entertainment</th>
</tr>
<tr>
<td>August</td>
<td>$1500</td>
<td>$150</td>
<td>$350</td>
<td>$100</td>
<td>$50</td>
</tr>
</table>
When creating tables in HTML, it’s important to ensure each row has the same
number of columns. If a row has fewer or extra cells than others, the table’s
alignment can become disorganized. For example, if we remove a cell from the
column headers, the content below might not align correctly.
Here's what a misaligned table might look like if the "Groceries" column header is
removed:
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>Month</th>
<th>Rent</th>
<th>Utilities</th>
<th>Eating Out</th>
<th>Entertainment</th>
</tr>
<tr>
<td>August</td>
<td>$1500</td>
<td>$150</td>
<td>$350</td>
<td>$100</td>
<td>$50</td>
</tr>
</table>
With this setup, the columns will no longer align properly. Always double-check that
each row has the same number of cells to keep your table organized and readable.
If you look at the table in the browser, you can see how the headers now have shifted
over one and there is a weird blank space at the end because there isn’t a table cell
there.
To make a table cell span across multiple columns or rows, we use the colspan and
rowspan attributes. Let’s look at how to apply colspan to keep our table organized
when adding extra data for utilities like "Electricity" and "Water."
In the example, we add an extra cell for "Water" under Utilities, but to maintain
proper alignment, we’ll use colspan="2" on the Utilities header.
<tr>
<th>Month</th>
<th>Rent</th>
<th>Groceries</th>
<th>Eating Out</th>
<th>Entertainment</th>
</tr>
<tr>
<td>August</td>
<td>$1500</td>
<td>$350</td>
<td>$100</td>
<td>$50</td>
</tr>
</table>
Explanation
• colspan="2" in the header row tells the Utilities cell to span across two
columns, allowing us to insert two separate data cells for "Electricity" and
"Water" below it.
When using colspan or rowspan, it’s essential to ensure that the rest of the cells in
the table are adjusted accordingly to maintain proper alignment.
Colspan Attribute
When we want a header or data cell to stretch across multiple columns in a table, we
can use the colspan attribute. In our case, we want the Utilities header to span across
both Electricity and Water cells.
To do this, we add the colspan attribute to the Utilities cell in the header row and set
it to 2, which will make it cover two columns instead of one:
<th colspan="2">Utilities</th>
Rowspan Attribute
The rowspan attribute lets you extend a cell to cover multiple rows. For instance, if
you have rows for June, July, and August and want them all grouped under the
season "Fall," you can set a cell with "Fall" to span three rows.
To do this:
1. Add a new cell containing "Fall" in the first column of the June row.
2. Set the rowspan attribute of this cell to 3 to cover June, July, and August.
3. To keep the table neat, add a blank cell (<th></th>) in the header row above
"Fall."
<tr>
<th></th>
<th>Month</th>
<th>Rent</th>
<th>Groceries</th>
<th>Eating Out</th>
<th>Entertainment</th>
</tr>
<tr>
<td>June</td>
<td>$1500</td>
<td>$100</td>
<td>$50</td>
<td>$350</td>
<td>$100</td>
<td>$50</td>
</tr>
<tr>
<td>July</td>
<td>$1500</td>
<td>$100</td>
<td>$50</td>
<td>$350</td>
<td>$100</td>
<td>$50</td>
</tr>
<tr>
<td>August</td>
<td>$1500</td>
<td>$100</td>
<td>$50</td>
<td>$350</td>
<td>$100</td>
<td>$50</td>
</tr>
</table>
With this setup, the Fall cell in the first column now neatly spans June, July, and
August, creating a clear, organized table structure.
Tasks
1. Build a Basic Table
Task: Create a table to display a weekly study schedule. Include headers for each
day of the week (Monday through Friday) and fill in two study subjects per day.
Objective: Practice basic table structure (<table>, <tr>, <th>, <td>).
Task: Design a monthly budget table with columns for Expense Category, Amount,
and Notes. Add styling attributes like border, cellpadding, and cellspacing.
Task: Create a table to show daily temperature readings (morning and evening) for
a week. Use colspan to group the "Temperature" heading above two sub-columns:
Morning and Evening.
Task: Build a table displaying a semester schedule, with subjects and timings for
each month (e.g., September, October, November). Group the three months under
"Fall Semester" using rowspan.
Objective: Practice using rowspan to span multiple rows with a single label.