vw
) to adapt to different screens.Controlling the size of HTML tables ensures they fit well within your webpage and are readable across devices. Table sizes, including width and height, are best managed with CSS properties like width, height, and max-width, though the deprecated HTML width and height attributes are still supported in older code. This tutorial explains how to set table sizes with examples and previews, helping beginners create well-proportioned tables.
Table sizes refer to the dimensions of a table and its cells, defined by width and height. Proper sizing makes tables visually appealing, ensures they fit within the page layout, and enhances usability on different devices. CSS is the modern approach for controlling sizes, offering flexibility for responsive design.
Let’s explore how to control table sizes using both the deprecated HTML attributes (for legacy understanding) and CSS (the modern approach), with previews.
The HTML width attribute sets the table’s width in pixels or percentage, but it’s outdated and lacks flexibility.
<table width="500" border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
</tr>
</table>
Name | Age |
---|---|
Alice | 25 |
CSS width property allows precise control, using pixels, percentages, or viewport units for responsiveness.
<table style="width: 100%; border: 1px solid black; border-collapse: collapse;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Product</th>
<th style="border: 1px solid black; padding: 8px;">Price</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Pen</td>
<td style="border: 1px solid black; padding: 8px;">$2</td>
</tr>
</table>
Product | Price |
---|---|
Pen | $2 |
Individual cells (<th> or <td>) can have specific widths or heights using CSS.
<table style="border: 1px solid black; border-collapse: collapse;">
<tr>
<th style="width: 200px; border: 1px solid black; padding: 8px;">Item</th>
<th style="width: 100px; border: 1px solid black; padding: 8px;">Stock</th>
</tr>
<tr>
<td style="width: 200px; border: 1px solid black; padding: 8px;">Notebook</td>
<td style="width: 100px; border: 1px solid black; padding: 8px;">50</td>
</tr>
</table>
Item | Stock |
---|---|
Notebook | 50 |
Use max-width or percentage-based widths for responsive tables that adapt to screen sizes.
<table style="max-width: 600px; width: 100%; border: 1px solid black; border-collapse: collapse;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Fruit</th>
<th style="border: 1px solid black; padding: 8px;">Color</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Apple</td>
<td style="border: 1px solid black; padding: 8px;">Red</td>
</tr>
</table>
Fruit | Color |
---|---|
Apple | Red |
Here are the main CSS properties for controlling table sizes:
width: 500px
or width: 100%
).height: 200px
).max-width: 600px
).min-width: 300px
).max-width
, or media queries to ensure adaptability across devices.
To make your table sizes effective and user-friendly:
Beginners often make these errors:
max-width: 100%
with overflow-x: auto
in a parent <div>
to create scrollable tables for mobile devices.
Create a simple HTML file and experiment with table sizes using CSS. Try fixed, percentage, and responsive widths.
<!DOCTYPE html>
<html lang="en">
<body>
<h3>Employee Data</h3>
<table style="width: 80%; max-width: 500px; border: 1px solid navy; border-collapse: collapse;">
<tr>
<th style="border: 1px solid navy; padding: 8px;">Employee</th>
<th style="border: 1px solid navy; padding: 8px;">Role</th>
</tr>
<tr>
<td style="border: 1px solid navy; padding: 8px;">Bob</td>
<td style="border: 1px solid navy; padding: 8px;">Developer</td>
</tr>
</table>
</body>
</html>
Employee | Role |
---|---|
Bob | Developer |
By mastering CSS properties for table sizes, you can create well-proportioned, responsive tables that enhance your website’s usability. Practice regularly to get comfortable!