HTML Table Generator
Generate HTML table markup with custom rows, columns, and styling options.
Cell Content
HTML Output
Table Preview
| Header 1 | Header 2 | Header 3 |
|---|---|---|
| Cell 1,1 | Cell 1,2 | Cell 1,3 |
| Cell 2,1 | Cell 2,2 | Cell 2,3 |
| Cell 3,1 | Cell 3,2 | Cell 3,3 |
Frequently Asked Questions
Code Implementation
# Generate an HTML table from a list of dicts
def generate_html_table(headers, rows, border=True, stripe=False):
border_attr = ' border="1" style="border-collapse:collapse"' if border else ""
lines = [f"<table{border_attr}>", " <thead><tr>"]
for h in headers:
lines.append(f" <th>{h}</th>")
lines.append(" </tr></thead>", " <tbody>")
for i, row in enumerate(rows):
bg = ' style="background:#f2f2f2"' if stripe and i % 2 == 0 else ""
lines.append(f" <tr{bg}>")
for cell in row:
lines.append(f" <td>{cell}</td>")
lines.append(" </tr>")
lines.append(" </tbody>", "</table>")
return "\n".join(lines)
headers = ["Name", "Age", "City"]
rows = [["Alice", 30, "New York"], ["Bob", 25, "London"]]
print(generate_html_table(headers, rows, border=True, stripe=True))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.