CSS

HTML Table Font Size

Sets the size of the font for text.

Power-ups
Responsive
Josh Hartman
Josh Hartman
Last updated: Apr 04, 2024
Table of Contents

HTML tables serve as the backbone for organizing and presenting data on the web. They offer a structured layout that facilitates comprehension and analysis of information. However, while the basic structure of tables is easy to grasp, customizing their appearance, particularly the font size, requires a deeper understanding of HTML and CSS. 

In this guide, we'll explore various techniques for controlling font size within HTML tables, empowering developers to create visually appealing, responsive, and accessible data presentations. Whether you're looking to set uniform font sizes or tailor them to specific cells or rows, this comprehensive overview will equip you with the knowledge needed to master font size manipulation in HTML tables.

How to Set Font Size

The font size within HTML tables can be adjusted using CSS (Cascading Style Sheets). By targeting specific elements such as <table>, <th>, and <td>, developers can apply font size rules to achieve the desired presentation.

CSS
/* CSS for setting font size of the entire table */ table { font-size: 16px; /* Set the font size to 16 pixels */ }
HTML
<table border="1"> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </table>

In this example, we apply a font-size: 16px; to the <table> element, which will affect all text inside of the table (unless overridden by a more specific selector).

Different Font Size for <th> and <td>

Tables often comprise both header cells (<th>) and data cells (<td>), each serving distinct purposes. To differentiate between these two types of cells, developers may opt to set different font sizes. This approach enhances readability and clarifies the structure of the table.

CSS
/* CSS for setting different font sizes for TH and TD */ table { border-collapse: collapse; /* Ensure cells are flush with one another */ } th { font-size: 18px; /* Set font size for table header cells */ font-weight: bold; /* Optionally make header text bold */ } td { font-size: 14px; /* Set font size for table data cells */ text-align: center; /* Optionally center-align text in data cells */ padding: 8px; /* Add padding for better readability */ border: 1px solid #ccc; /* Add borders to cells */ }
HTML
<table> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </table>

Here, we're able to specify a larger font size for the header cells in order to provide some visual hierarchy.

Setting the Font Size of Specific Rows

In scenarios where certain rows within a table require emphasis or distinction, developers can apply font size adjustments selectively. By leveraging CSS selectors or inline styling, specific rows can stand out, drawing attention to important information.

CSS
/* CSS for setting font size for specific row */ tr.special-row td { font-size: 18px; /* Set font size for cells in the special row */ font-weight: bold; /* Optionally make the text bold */ background-color: #f0f0f0; /* Optionally highlight the special row */ }
HTML
<table> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <!-- Special row with increased font size --> <tr class="special-row"> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> <tr> <td>Data 7</td> <td>Data 8</td> <td>Data 9</td> </tr> </table>

In this example, we added a class of special-row to a row (<tr>) in the table, where we can apply specific styles, such as font size, to that row only.

Setting the Font Size of Specific Cells

Similar to rows, individual cells within a table can have unique font sizes assigned to them. This granular control allows developers to highlight specific data points or enhance the visual hierarchy within the table.

CSS
/* CSS for setting font size for specific cell */ td.special-cell { font-size: 18px; /* Set font size for the special cell */ font-weight: bold; /* Optionally make the text bold */ background-color: #f0f0f0; /* Optionally highlight the special cell */ }
HTML
<table> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <!-- Special cell with increased font size --> <td class="special-cell">Data 5</td> <td>Data 6</td> </tr> <tr> <td>Data 7</td> <td>Data 8</td> <td>Data 9</td> </tr> </table>

Here, we added a class of special-cell to a specific table data cell (<td>) in order to make its styling unique.

Setting the Font Size Without CSS

While CSS is the preferred method for styling HTML content, alternative approaches exist for setting font sizes within tables. Utilizing inline styling or HTML attributes such as the style attribute enables direct specification of font size without relying on external stylesheets.

HTML
<table> <tr> <th style="font-size: 18px;">Header 1</th> <th style="font-size: 18px;">Header 2</th> <th style="font-size: 18px;">Header 3</th> </tr> <tr> <td style="font-size: 14px;">Data 1</td> <td style="font-size: 14px;">Data 2</td> <td style="font-size: 14px;">Data 3</td> </tr> <tr> <td style="font-size: 14px;">Data 4</td> <td style="font-size: 14px;">Data 5</td> <td style="font-size: 14px;">Data 6</td> </tr> </table>

That's a lot of additional markup, but it does get the job done!

Responsiveness

In today's multi-device world, ensuring that tables remain readable and functional across various screen sizes is important. Employing responsive design principles, such as using relative units like percentages or viewport widths, allows tables to adapt fluidly to different viewing environments.

Similarly, it’s also possible to change the font size based on the viewport size. This can be helpful when creating a table that has certain cells that wrap multiple lines, allowing for you to decrease the font size to prevent this wrapping from taking place. This isn’t always avoidable, but can be corrected in some situations.

CSS
/* Default font size for table cells */ td { font-size: 16px; } /* Media query for smaller screens (e.g., mobile devices) */ @media screen and (max-width: 768px) { td { font-size: 14px; /* Reduce font size for smaller screens */ } }
HTML
<table> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </table>

In this example, we applied a media query in order to change the <td> font size from 16px on desktop to 14px on mobile landscape and smaller.

Accessibility

Accessibility is a fundamental aspect of web development, and it's crucial to consider how font size adjustments impact users with diverse needs. Developers should ensure that any changes made to font size maintain readability for all users, including those with visual impairments who may rely on screen readers or magnification tools.

As mentioned above, it’s possible to decrease the font size on smaller devices. However, keep in mind that some users will have their font size turned up by default. In other situations, it’s important to ensure that the font size remains large enough that it’s legible by your audience.

Conclusion

Mastering font size control within HTML tables empowers developers to create visually compelling and accessible data presentations. By understanding the various techniques available, from basic CSS rules to more nuanced approaches for selective styling, developers can optimize the readability and usability of their tables across different devices and user scenarios. Embracing responsiveness and accessibility principles ensures that tables not only look good but also serve their intended purpose effectively for all users.

Copy
Josh Hartman

Josh Hartman

I'm Josh, the founder of HTML Tables and eklipse Development, a Webflow Professional Partner. I've always loved seamless web experiences and take pride in merging code with creative design. Aside from this website, I'm currently building How Much Concrete, a state-of-the-art concrete calculator. Beyond the digital realm, I love the outdoors & fitness. Find me on a trail or in the gym!