Table Element
The <td> (table data) element defines a standard data cell in a table. Unlike <th> (header cells), <td> elements contain the actual data values in your table. Data cells can span multiple columns or rows using the colspan and rowspan attributes, making tables flexible for various data layouts.
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Employee Contact Information</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Department</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice Johnson</td>
<td>Engineering</td>
<td>alice@example.com</td>
<td>(555) 123-4567</td>
</tr>
<tr>
<td>Bob Martinez</td>
<td>Marketing</td>
<td>bob@example.com</td>
<td>(555) 234-5678</td>
</tr>
<tr>
<td>Carol White</td>
<td>Sales</td>
<td>carol@example.com</td>
<td>(555) 345-6789</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead {
background-color: #3498db;
color: white;
}
tbody tr:nth-child(even) {
background-color: #f8f9fa;
}
</style>
< td colspan = " 2 " > Spanning two columns </ td >
< td rowspan = " 2 " > Spanning two rows </ td >
The colspan attribute specifies how many columns a cell should span horizontally.
< td colspan = " 3 " > This cell spans 3 columns </ td >
The rowspan attribute specifies how many rows a cell should span vertically.
< td rowspan = " 2 " > This cell spans 2 rows </ td >
For complex tables, the headers attribute explicitly associates the cell with header cells using their IDs.
< td headers = " header1 header2 " > Data </ td >
The <td> element supports all global HTML attributes .
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Product Pricing</caption>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Regular Price</th>
<th scope="col">Sale Price</th>
<th scope="col">Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop Pro</td>
<td>$1,299</td>
<td>$999</td>
<td>$300</td>
</tr>
<tr>
<td>Wireless Mouse</td>
<td>$59</td>
<td>$39</td>
<td>$20</td>
</tr>
<tr>
<td>USB-C Cable</td>
<td>$25</td>
<td>$19</td>
<td>$6</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
}
thead {
background-color: #27ae60;
color: white;
}
td {
text-align: right;
}
td:first-child {
text-align: left;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Task Status Dashboard</caption>
<thead>
<tr>
<th scope="col">Task</th>
<th scope="col">Status</th>
<th scope="col">Progress</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Design Homepage</td>
<td><span class="badge complete">Complete</span></td>
<td>100%</td>
<td><button>View</button></td>
</tr>
<tr>
<td>Backend API</td>
<td><span class="badge in-progress">In Progress</span></td>
<td>65%</td>
<td><button>Edit</button></td>
</tr>
<tr>
<td>Testing</td>
<td><span class="badge pending">Pending</span></td>
<td>0%</td>
<td><button>Start</button></td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead {
background-color: #34495e;
color: white;
}
.badge {
padding: 4px 8px;
border-radius: 4px;
font-size: 0.85em;
font-weight: bold;
}
.complete {
background-color: #d4edda;
color: #155724;
}
.in-progress {
background-color: #fff3cd;
color: #856404;
}
.pending {
background-color: #f8d7da;
color: #721c24;
}
button {
padding: 4px 12px;
cursor: pointer;
}
</style>
The colspan attribute makes a cell span multiple columns:
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Weekly Schedule</caption>
<thead>
<tr>
<th scope="col">Time</th>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
</tr>
</thead>
<tbody>
<tr>
<td>9:00 AM</td>
<td colspan="3" style="background-color: #e8f8f5; text-align: center;">
<strong>Team Meeting</strong>
</td>
</tr>
<tr>
<td>10:00 AM</td>
<td>Development</td>
<td>Development</td>
<td>Development</td>
</tr>
<tr>
<td>2:00 PM</td>
<td>Code Review</td>
<td colspan="2" style="background-color: #fef5e7; text-align: center;">
<strong>Sprint Planning</strong>
</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead {
background-color: #8e44ad;
color: white;
}
tbody td:first-child {
background-color: #f4ecf7;
font-weight: bold;
}
</style>
< td colspan = " 4 " > This spans all 4 columns </ td >
< td colspan = " 2 " > Spans columns 2-3 </ td >
< td colspan = " 2 " > Columns 1-2 </ td >
< td colspan = " 2 " > Columns 3-4 </ td >
The rowspan attribute makes a cell span multiple rows:
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Project Team Structure</caption>
<thead>
<tr>
<th scope="col">Department</th>
<th scope="col">Role</th>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3" style="background-color: #e3f2fd; vertical-align: top;">
<strong>Engineering</strong>
</td>
<td>Lead</td>
<td>Alice Johnson</td>
</tr>
<tr>
<td>Senior Dev</td>
<td>Bob Martinez</td>
</tr>
<tr>
<td>Junior Dev</td>
<td>Carol White</td>
</tr>
<tr>
<td rowspan="2" style="background-color: #fff3e0; vertical-align: top;">
<strong>Design</strong>
</td>
<td>UX Designer</td>
<td>David Kim</td>
</tr>
<tr>
<td>UI Designer</td>
<td>Emma Wilson</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead {
background-color: #2c3e50;
color: white;
}
</style>
< td rowspan = " 2 " > Spans 2 rows </ td >
<!-- First cell is from previous row -->
< td rowspan = " 3 " > Group A </ td >
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Complex Data Layout</caption>
<thead>
<tr>
<th scope="col">Category</th>
<th scope="col">Q1</th>
<th scope="col">Q2</th>
<th scope="col">Q3</th>
<th scope="col">Q4</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2" style="background-color: #e8f8f5; vertical-align: middle;">
<strong>Sales</strong>
</td>
<td>$100K</td>
<td>$120K</td>
<td>$115K</td>
<td>$130K</td>
</tr>
<tr>
<td colspan="4" style="background-color: #fef5e7; text-align: center;">
Total Sales: $465K
</td>
</tr>
<tr>
<td rowspan="2" style="background-color: #fce4ec; vertical-align: middle;">
<strong>Expenses</strong>
</td>
<td>$60K</td>
<td>$65K</td>
<td>$70K</td>
<td>$68K</td>
</tr>
<tr>
<td colspan="4" style="background-color: #f3e5f5; text-align: center;">
Total Expenses: $263K
</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: right;
}
th {
background-color: #34495e;
color: white;
text-align: center;
}
td:first-child {
text-align: center;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Text Alignment Examples</caption>
<thead>
<tr>
<th scope="col">Left Aligned</th>
<th scope="col">Center Aligned</th>
<th scope="col">Right Aligned</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">Left</td>
<td style="text-align: center;">Center</td>
<td style="text-align: right;">Right</td>
</tr>
<tr>
<td style="text-align: left;">Product Name</td>
<td style="text-align: center;">Status</td>
<td style="text-align: right;">$99.99</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
}
thead {
background-color: #16a085;
color: white;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Sales Performance (Color-coded)</caption>
<thead>
<tr>
<th scope="col">Salesperson</th>
<th scope="col">Target</th>
<th scope="col">Actual</th>
<th scope="col">Performance</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>$50,000</td>
<td>$65,000</td>
<td class="high">130%</td>
</tr>
<tr>
<td>Bob</td>
<td>$50,000</td>
<td>$48,000</td>
<td class="medium">96%</td>
</tr>
<tr>
<td>Carol</td>
<td>$50,000</td>
<td>$35,000</td>
<td class="low">70%</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
thead {
background-color: #2c3e50;
color: white;
}
td:nth-child(2), td:nth-child(3), td:nth-child(4) {
text-align: right;
}
.high {
background-color: #d4edda;
color: #155724;
font-weight: bold;
}
.medium {
background-color: #fff3cd;
color: #856404;
}
.low {
background-color: #f8d7da;
color: #721c24;
}
</style>
Interactive code playground requires JavaScript. Here's the code:
<table>
<caption>Project Portfolio</caption>
<thead>
<tr>
<th scope="col">Project</th>
<th scope="col">Details</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong>Website Redesign</strong><br>
<small style="color: #666;">Client: Acme Corp</small>
</td>
<td>
<ul style="margin: 0; padding-left: 20px;">
<li>UI/UX Complete</li>
<li>Development: 80%</li>
</ul>
</td>
<td>
<div style="background: #3498db; color: white; padding: 4px 8px; border-radius: 4px; text-align: center;">
Active
</div>
</td>
</tr>
<tr>
<td>
<strong>Mobile App</strong><br>
<small style="color: #666;">Client: TechStart</small>
</td>
<td>
<ul style="margin: 0; padding-left: 20px;">
<li>Research Phase</li>
<li>Wireframes: 50%</li>
</ul>
</td>
<td>
<div style="background: #f39c12; color: white; padding: 4px 8px; border-radius: 4px; text-align: center;">
Planning
</div>
</td>
</tr>
</tbody>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
vertical-align: top;
}
thead {
background-color: #8e44ad;
color: white;
}
</style>
For complex tables, explicitly associate data cells with headers:
< th id = " name " scope = " col " > Name </ th >
< th id = " math " scope = " col " > Math </ th >
< th id = " english " scope = " col " > English </ th >
< th id = " student1 " scope = " row " > Alice </ th >
< td headers = " student1 math " > 95 </ td >
< td headers = " student1 english " > 88 </ td >
This helps screen readers announce: “Alice, Math: 95, English: 88”
For empty cells, consider adding context:
< td aria-label = " No data " > — </ td >
td .align-left { text-align : left ; }
td .align-center { text-align : center ; }
td .align-right { text-align : right ; }
vertical-align : top ; /* top, middle, bottom */
font-family : ' Courier New ' , monospace ;
background-color : # d4edda ;
background-color : # f8d7da ;
background-color : # f0f0f0 ;
< td > 125 < span class = " unit " > GB </ span ></ td >
< td > $1,299 < span class = " unit " > USD </ span ></ td >
< td > 98 < span class = " unit " > % </ span ></ td >
< a href = " /product/123 " > View Details </ a >
< img src = " avatar.jpg " alt = " User avatar " width = " 40 " height = " 40 " >
< input type = " checkbox " id = " item1 " >
< label for = " item1 " > Select </ label >
Use for Data Only
Only use <td> for data cells. Use <th> for headers even in row context.
Keep Content Simple
While you can put complex HTML in cells, simpler content is more accessible.
Calculate Spans Carefully
Ensure colspan/rowspan values don’t create inconsistent column counts.
Align Appropriately
Left-align text, right-align numbers, center-align status indicators.
Provide Context
Use the headers attribute for complex relationships.
Test Responsiveness
Ensure cells work well on mobile, especially with spanning.
<!-- ❌ Bad: column count mismatch -->
<!-- ✅ Good: consistent columns -->
< td >< strong > Product </ strong ></ td >
< th scope = " row " > Product </ th >
The <td> element is supported in all browsers:
✅ Chrome (all versions)
✅ Firefox (all versions)
✅ Safari (all versions)
✅ Edge (all versions)
✅ Opera (all versions)
✅ Internet Explorer (all versions)
The colspan, rowspan, and headers attributes are also universally supported.