
Working with HTML tables and CSS can be a bit tricky, but with some best practices, you can create tables that are not only visually appealing but also easy to maintain and access.
Using the border-collapse property, you can choose whether to collapse or separate the borders of table cells. For example, setting border-collapse to collapse can make your table look more compact, while setting it to separate can give it a more traditional look.
A good rule of thumb is to keep your table headers in the first row, as this makes it easier for screen readers and other accessibility tools to understand the table structure.
Using the th tag for table headers is not only a good practice, but it's also required for accessibility reasons, as it allows screen readers to announce the header cells correctly.
You might enjoy: Html Accessibility
CSS Styling
Styling with CSS is a game-changer for making tables look professional and appealing to the eyes.
CSS can help improve the user experience when navigating a table, making it easier to find the information you need.
To style a table with CSS, you'll need to create a separate file with a .css extension and link it to your HTML file.
Border-collapse: separate is a crucial CSS attribute that allows you to add rounded corners to a table.
The author of the article has a collection of well-designed tables for reference, and they're always looking for more examples to add to it.
A nicely styled table can make a big difference in how users interact with your data, and it's worth taking the time to get it right.
In the code playground, you can see an example of a styled table that's been created using a CSS file, with added color and border to make it more readable and beautiful.
Explore further: How to Separate Header from Body in Html
Table Layout and Design
Our table columns can be a bit of a mess if their widths are determined by the content, making them uneven.
To fix this, we can set a width on the table itself, which helps, but still leaves us with uneven column widths.
Setting the width property on each header cell apart from the first one is a good start, but we still need more control over column sizes.
Changing the table-layout property from the default auto to fixed allows us to use the widths defined on columns or cells for the first table row to resolve the column widths.
With a fixed table layout, we also need to set the width property of the affected table, which we can do using the max() function to ensure our table takes up the right amount of space in different viewport sizes.
Explore further: Fixed Header in Html
Column Sizes
Our table columns can look a bit messy if their widths are unequal. The width of each column is determined by the length of its heading, which is often longer than the content in the body cells.

Setting a width on our table can make it look a bit better, but our column widths are still uneven. This is because the browser is still using the content to determine the table layout.
We can change the algorithm the browser uses to determine the table layout using the table-layout property. Setting this to fixed causes the browser to ignore cell content and use the widths defined on columns or cells for the first table row to resolve the column widths.
For a fixed table layout to work in browsers today, we also need to set the width property of the affected table. This means our table will be at least 65rem at small viewport sizes while in larger viewports it will take up the entire width available.
Our table columns should now all have equal widths, but we might want a different size for our first column which contains the row headers. If we set a width on the first element, the remaining horizontal space will be distributed evenly between the rest of the columns.
Recommended read: Vscode Open Html in Browser
Here's an interesting read: Hide Column in Table Html
Two Axis

Two Axis tables can be tricky to design.
In some cases, it's okay to skip the element even if the first row is all headers.
Just make one row of all elements and then each subsequent row with the first cell only as.
This approach can be useful when the top row doesn't feel like it deserves special treatment.
You can remove the differences between browsers with CSS resets.
In fact, 1px of padding that WebKit doesn't have is one of the things CSS resets can help with.
Readers also liked: Print Html One by One
As Wide As Needed or Beyond
The table element is a bit of an oddball in terms of its width. It behaves like a block-level element, meaning each table will break down onto its own line if placed one after another.
The actual width of the table is only as wide as it needs to be, which is determined by the amount of text in its widest row. If that text fits within a 100px wide container, the table will be 100px wide.
But if the text would be wider than the container, it will wrap to the next line. This is a great way to save space and make your table more readable.
Table cells also won't wrap, so if there are too many to fit within the container, the table will happily bust out of it and go wider. This means you need to be mindful of how much content you're packing into each cell.
See what others are reading: Html Container
Challenging in Fluid Designs
Tables can be difficult in fluid/responsive designs. This is because they often don't adapt well to smaller screens.
You can turn rows into blocks to make tables more manageable on small screens. This technique is useful for data tables that need to be viewed on mobile devices.
FooTable is a jQuery plugin that hides rows and makes data available via a toggle icon. This feature can be a lifesaver when dealing with large tables on small screens.
Related reading: Advanced Html Tables Examples
Hiding columns can also save room and allow users to toggle them back on. This is a simple yet effective way to make tables more fluid.
Squishing cells and allowing them to wrap is another option. However, this approach can be tricky to implement and may not work well for all types of data.
Fixing a header and allowing the body to scroll can also be a good solution. This approach can help keep the table organized and easy to read.
Table Appearance
Table Appearance is a crucial aspect of making your HTML tables both readable and visually appealing.
You can use borders to separate table cells, but an alternative approach is to colour rows and columns. Colouring rows and columns can make your table easier to read.
To subtly colour alternate rows, you can use the CSS color-mix() function to create a transparentised variant of a theme colour.
See what others are reading: Html Table Space between Rows
Colouring Rows and Columns
Colouring rows and columns can make a table more readable. This can be done by setting a theme colour as a custom property on the table.
For another approach, see: Html B Tag
You can also use the CSS color-mix() function to create a transparentised variant of the same colour to subtly colour alternate rows.
Zebra striping tables is another technique, where you set the background-color on the table rows themselves. This is a good option if you don't want to set borders on the table cells.
Setting the even rows as well can make the table more visually distinct, especially when combined with depth in the table. This can be achieved using the tbody in the selector.
If you need to support older browsers, you can use jQuery to apply the zebra striping effect.
Sticky Rows
Sticky rows can be achieved with sticky positioning, ensuring row headers stay visible as the user scrolls.
To give table cells their own background color, you'll need to style them separately from the row background. This is because the row content will be visible beneath the row headers as the user scrolls.
Readers also liked: Html Visible

A side effect of this behavior is that the right-hand border of row headers disappears under the header content when the table is scrolled. You can fix this by removing the right-hand border of header cells using the border-inline-end logical property.
This property is equivalent to border-right in left-to-right writing mode. You can use the :where() pseudo-class to reduce the number of lines of code and select the second that is a child of either or.
A unique perspective: Html Value Property
Table Interactivity
Table interactivity is a feature that allows users to engage with tables in meaningful ways. Sorting tables by clicking headers is a common need.
Imagine a table with two columns, one for Employee ID's and another for Employee Email Address. It would be handy to be able to click those headers and sort the table by the data inside. For instance, numerical order, alternating between ascending and descending, for the ID's and alphabetical for the email addresses.
Suggestion: Beautiful Html Tables
There are several third-party options available for table sorting, including tablesorter, sorttable, and tablesort. These scripts can be used to add interactivity to tables without requiring native browser support.
Here are some popular table sorting scripts:
- tablesorter – jQuery-based “Flexible client-side table sorting”
- sorttable – raw javaScript
- tablesort – “a small & simple sorting component for tables. Written in Javascript and dependency free”
Sorting
Sorting is a common need in tables, especially when it comes to data that needs to be organized in a specific way. You can sort tables by clicking on the headers, which will arrange the data in numerical or alphabetical order.
There's actually a specification for table sorting, and all you need to do is add the sortable attribute to the table. However, at the time of writing, no browsers support table sorting natively.
Luckily, there are plenty of third-party options available. For instance, you can use tablesorter, a jQuery-based solution that's flexible and easy to use. Or, you can try sorttable, a raw JavaScript solution that's also worth considering.
If you're not satisfied with those options, there are many more to choose from. In fact, Codrops has rounded up 33 different table sorting scripts, so you're bound to find one that suits your needs.
It's worth noting that you can also sort data on the back-end and display the table already sorted in the HTML. This might be necessary for paginated tables where not all the data is available in the DOM.
See what others are reading: Do I Need Php for Submission Form Html
Interactive Scrim
An interactive scrim is a great way to practice your HTML skills, and that's exactly what we're going to do - try an interactive scrim about HTML tables.
In this scrim, you'll get to create a table and see how it looks in action. But before we dive in, let's talk about what a table is in HTML.
A table in HTML is a way to display information in a tabular format, with rows and columns.
To create a table, you'll need to use a combination of HTML tags, including table, tr, td, and th.
Accessibility
Accessibility is crucial for making your HTML table and CSS styling accessible to everyone, regardless of their abilities. Adrian Roselli pointed out that the current solution isn't accessible to keyboard users and screen readers.
To improve this, give your scrollable area a role and an accessible name for screen readers to announce. This can be done using aria-labelledby, with the table's caption ID as its value. Giving the wrapper element a tabindex of 0 also makes it focusable for keyboard users.
Discover more: Full Screen Background Image Html
You can use CSS to provide a visually distinct style when the scroll container is focused. Adrian's solution of using attribute selectors to style the table is a good one. This ensures the table will not be clipped unless the HTML is properly marked up to be accessible.
For tables used for tabular data, there are still some accessibility concerns to consider. To learn more about creating accessible tables, check out these resources: WebAIM: Creating Accessible TablesPortland Community College: Examples of Good and Bad Table Layout for Screen ReadersWeb Usability: Accessible Data Tables
If this caught your attention, see: Html Good Practices
Table Creation and Markup
Emmet is a game-changer for creating table markup, allowing you to write HTML abbreviations that expand into real HTML.
Using Emmet's abbreviations, you can create repetitive table structures with ease, such as a simple four rows and four columns table, which is perfect for when you need a basic table layout.
Emmet also works on CodePen, making it a versatile tool for developers.
TD and TH cells
TD and TH cells are the building blocks of a table. They can contain any content, but they must be one of the two elements: or.
The element is used for tabular headers, and it's not limited to being within the section. It can be used anywhere in the table to indicate header information.
The main difference between and is that elements are typically used for titles, while elements are used for data.
Here's a quick rundown of the two elements:
Note that the UA stylesheet for tables can differ between browsers, but the basic structure of using and elements remains the same.
Emmet for Markup Creation
Emmet is a game-changer for creating table markup. It's perfect for writing HTML abbreviations that expand into real HTML, making it a great tool for repetitive and verbose tasks like creating tables.
Emmet works on CodePen too, which is a bonus. I've used it to create tables on CodePen and it's saved me a lot of time.
For another approach, see: Creating Horizontal List with Bullets in Html
A simple four rows and four columns table can be created using Emmet. It's as easy as typing a few characters and Emmet does the rest.
You can also use Emmet to create tables with more complex structures, like a table with five rows and the header on the left. This is especially useful when you need to create a table with a specific layout.
Emmet can even handle tables with headers, footers, and content. This is a huge time-saver when you need to create a table with multiple sections.
Creating tables with cell content in each cell is also a breeze with Emmet. You can simply type the abbreviations and Emmet will expand them into the complete HTML code.
Intriguing read: Html Table Center Text in Cell
Table Overflow and Scrolling
Adding many more columns to our table can cause horizontal overflow of our webpage, making it hard for users to view all the data.
We can avoid this issue by wrapping the table in an element with overflow: auto or overflow: scroll applied, allowing users to scroll the table itself.
This solves the problem of horizontal overflow, but it's not a perfect solution, as users still need to scroll all the way back to the start to see the row header names.
This can be annoying and may cause users to lose their place in the table.
Related reading: Html Overflow
Table Tips and Tricks
Table styling can be a bit tricky, but don't worry, I've got some quick tips to make your tables look neat and organized.
Make your table markup as simple as possible, and keep things flexible. This will make it easier to style and layout your table.
Use table-layout: fixed to create a more predictable table layout. This allows you to easily set column widths by setting width on their headings ().
The,, and elements are your friends. They help break up your table into logical chunks, making it easier to apply CSS styles.
Use border-collapse: collapse to make table elements' borders collapse into each other. This produces a neater and easier to control look.
Here are some quick table styling tips in a nutshell:
- Use zebra striping to make alternative rows easier to read.
- Use text-align to line up your
and text, to make things neater and easier to follow. Table Search and Filter
Adding a search input to a table can be a simple way to filter results.
You can use jQuery to make it easy, but another option is to use a regular expression, which can be a bit more powerful.
For example, you can use the following JavaScript code to search for text anywhere in a row:
Take a look at this: How to Add Search Bar in Html
Table Syntax and Rules
Credit: youtube.com, Learn HTML tables in 3 minutes 📊 HTML tables are created using a specific syntax, which can be broken down into several key elements.
The tag is used to define the body of the table, containing one or more rows. A table row is defined by the tag, and a table data cell is defined by the tag.
Here's a quick rundown of the basic tags used in HTML table syntax:
- defines the body of the table
defines a table row defines a table data cell You can also use the tag to define table header cells, which are typically displayed in a bold font.
Negating a Statement
Negating a Statement can be a tricky business, especially when it comes to tables. It's like trying to un-table a table, which can be done by resetting the display property of the table cells.
We can pretty quickly un-table a table by doing so. This is primarily useful in responsive design where the traditional table layout makes sense on large screens but needs significant shifts to make sense on smaller screens.
Worth a look: Aumentar El Tamaño De Un Checkbox Html
Syntax

Credit: pexels.com, Geometric pattern featuring blue and white design elements. Perfect for modern graphic projects. To create an HTML table, you start with the opening tag. The tag is used to define the body of the table. You can think of it as the container for all the rows and cells in your table.
A table row is defined by the tag, which stands for table row. This tag is used to group a set of cells together. Each cell is defined by the tag, which stands for table data.
Here's a breakdown of the basic table syntax:
* defines the body of the table
* defines a table row
* defines a table cell
The example below shows how to create a simple table using these tags:Cell 1Cell 2Cell 3Cell 4Cell 5Cell 6
You can also use the tag to define table headers. This is useful for creating tables with columns that have specific names, like a table of user information.
Recommended read: Is Html Still Used
Table Best Practices
To create a well-structured table, start with a clear table header that defines the purpose and content of each column. This will make it easier for users to understand the table's context.
Use the element to define the header row, which should contain a brief description of each column. In the example table, the element is used to define the header row with column names like "Name" and "Age".
Group related columns together to improve readability. For instance, in the example table, the "Name" and "Age" columns are grouped together to provide a clear and concise view of the data.
Use the element to define table headers, which should be short and descriptive. In the example table, the element is used to define headers like "Name" and "Age", which are short and descriptive.
Avoid using unnecessary columns that don't add value to the table's content. In the example table, only relevant columns like "Name" and "Age" are included to provide a clear and concise view of the data.
Use CSS to style the table and make it more visually appealing. For example, you can use the border-collapse property to define the border style and spacing between cells.
Additional reading: The Html Canvas Element Is Used to
Featured Images: pexels.com
Related Reads

Mastering CSS: Essential Techniques for Web Design
Master CSS fundamentals and best practices for effective web design, learn the essentials of CSS for a more efficient coding experience.
Read More

Bootstrap CSS vs Tailwind CSS: Ultimate Design Frameworks
Compare Bootstrap CSS vs Tailwind CSS, two popular front-end frameworks, in our comprehensive guide to their features, syntax, and use cases.
Read More

css tailwind: Start Building Beautiful Sites Easily
Discover CSS Tailwind: A beginner's comprehensive guide to getting started with this popular utility-first CSS framework.
Read More
- defines the body of the table