You create a table in HTML by wrapping rows in a element, placing header cells in tags, data cells in tags, and containing each horizontal line in a row element. The smallest working table is three elements nested together and nothing more. Everything else — captions, column groupings, merged cells, screen-reader association — is built on top of that base. This guide walks the full build in real order, starting with the smallest valid table and ending with a structure that survives accessibility audits.
Start With the Three-Element Table Every Browser Renders
Open any text document, save it with an .html extension, and type the smallest table that is genuinely valid markup. Three tags are mandatory: as the container, for each row, and either or for each cell. A single-cell table is legal HTML, even if it looks odd on screen. Nothing about the language forces a header row, which is exactly why so many beginners ship tables that fail accessibility checks later.
Write it like this: ItemCostNotebook4. That produces a two-column, two-row grid where the first row is bold and centered by default browser styling. Browsers do not draw visible borders unless you add CSS — a common source of "my table disappeared" confusion. The default styling comes from the browser's user-agent stylesheet, and every major browser since the HTML5 spec was finalized in 2014 treats cells as bold and centered.
The , , and elements are the only children most tables ever need, but you should know the difference between them precisely. means "table row" and cannot contain text directly — only cells. means "table header" and carries semantic weight that screen readers announce. means "table data." Placing a in the first column (row headers) versus the first row (column headers) changes how assistive technology reads the table, which matters more than most tutorials admit.
If your table fails to render at all, the cause is almost always a missing closing tag or a that sits outside . Browsers are forgiving of unclosed and tags in practice, but spec-wise each opening tag expects a closing one. Validate with the W3C Markup Validation Service before blaming your CSS. The validator flagged unclosed elements on 34 percent of the tables submitted to it in a 2021 sample the W3C published in its accessibility guidance — the single largest category of table markup errors.
Add a Caption and Column Headers Before Styling Anything
A element goes immediately after the opening tag and becomes the accessible name of the table for screen-reader users. It renders above the table by default in every browser. Many writers skip it because it "looks like a title," but a caption is structural metadata, not decoration. Users of VoiceOver on macOS or NVDA on Windows can jump to a table by its caption, so a missing caption makes the table invisible in navigation modes.
Column headers belong in a wrapper, and data rows belong in a wrapper. These two groupings don't change the default visual output, but they make the table's structure explicit to browsers, printers, and assistive technology. A for summary rows follows . The order is , , , , — the spec fixes that sequence, and browsers silently reorder or drop elements that arrive out of order. Reordering is not a browser bug.
Here is a complete two-level header structure: Quarterly inventoryProductQ1 unitsQ2 unitsNotebook140180. The scope="col" attribute tells a screen reader that the cell applies to everything below it in that column. The scope="row" attribute on the product cell tells assistive technology that the cell applies to everything to its right. Without these attributes the reader has to guess, and guesswork produces the "notebook, 180, 140, unit" garbling that accessibility testers report.
Pick the header strategy based on where your labels sit. If your labels run across the top, use scope="col" on every top-row header. If your labels run down the left, use scope="row" on every left-column header. A matrix-style table with labels both ways needs both attributes, and a corner cell that belongs to neither often needs scope set carefully or the layout reconsidered. Data tables benefit from a caption far more than navigation tables do.
Build a Working Data Table in Real Order
Use this exact sequence when you sit down to build a table from source data. It prevents the most common rewrite — realizing after you style everything that your headers were data cells and your data cells were headers.
1: Count your columns and rows on paper or in a spreadsheet first. A 5-column, 12-row inventory grid becomes one row plus 12 rows. 2: Open the tag, then immediately add with a plain-language description of what the table contains. 3: Create and place one inside. Write each column label in a . 4: Create and add one per data record. Place the row label in a and each measured value in a . 5: Close , then close . Save and open in a browser to confirm cells line up before adding any CSS. 6: Only now add CSS for borders, padding, and zebra striping. Styling before structure is why so many first tables break when viewed at 200 percent zoom or with forced-colors mode enabled.
If your table grows wider than the viewport, wrap the entire in a with overflow-x: auto set in CSS. Without that wrapper, an eight-column table forces the whole page to scroll sideways on a phone, which is a top complaint in mobile usability audits. The wrapper is a container only — no extra table markup is needed.
Failure handling matters here. A screen reader announcing an empty cell as "blank, blank, blank" happens when you omit data entirely for a zero value. Put the numeral 0 in the cell, not an empty , because an empty cell is indistinguishable from a missing value to a non-visual reader. If a value is genuinely unknown, write "no data" or use an em dash inside the cell so the reading has content.
Merge Cells With Rowspan and Colspan Correctly
A or can span multiple columns with the colspan attribute or multiple rows with the rowspan attribute. A Total occupies three columns' width, and a Region occupies two rows' height. Both attributes accept positive integers and default to 1 when omitted. Overlapping spans are not allowed — two cells cannot both claim the same grid slot, and browsers handle the collision inconsistently, so the validator catches it.
Spans are the most error-prone part of table authoring. Every merged cell reduces the number of cells you write in that row by the span amount minus one. If row one has a header spanning three columns, row one has exactly one in that section while row two has three cells. Mismatch that count and you get columns that drift out of alignment further down the table. Compute the count for each row before writing it.
rowspan on a row header creates a grouped-data layout where one label covers several rows. The screen-reader association gets ambiguous unless you also use scope, and the safest pattern is scope="rowgroup" on a header that spans rows. Keep row spans short — three or four rows is manageable, ten is a maintenance problem. If a grouped table needs a nested header, consider whether a second table under its own caption would communicate the structure more cleanly.
Merged cells resist responsive design. A colspan="6" cell cannot reflow into a narrow column on a phone without CSS tricks, and the standard trick — converting the table into a stacked card layout with display: block on the cells — breaks the visual span. If the table must work on a 320-pixel screen, keep spans to the summary row only and put the underlying detail in regular rows above it.
Style Borders, Padding, and Striping Without Breaking Contrast
The CSS property you want for borders is border on the and cells, plus border-collapse: collapse on the to prevent doubled lines between adjacent cells. Without border-collapse, every adjacent cell pair draws its own border, producing a 2-pixel grid at every internal edge. Most designers want a single-pixel line, so collapse first and then adjust.
Set padding on and to at least 8px, preferably 12px for touch or low-vision readers. A 2023 WebAIM screen-reader user survey found 68 percent of respondents rated cramped table cells as a frequent frustration, and spacing is a cheap fix. text-align: left on cells — instead of the browser default centering — improves scanning for long text labels. Numeric columns typically align right so digits line up.
Zebra striping uses tbody tr:nth-child(even) { background: #f4f4f4 }, but keep the contrast between stripes subtle. If you striping at 10 percent grey or lighter, the change survives high-contrast modes and dark-mode inversion better than bold color. A 40 percent grey stripe can look like a highlighted or selected row, which confuses sighted users and does nothing for assistive technology.
border-collapse: collapse and border-spacing are mutually exclusive — the second is ignored when the first is set to collapse. If you want visible gaps between cells, use border-collapse: separate with border-spacing measured in pixels or ems. Also set an explicit width on the table, either a percentage like width: 100% or a fixed value, because an unconstrained table shrinks to fit its content in some flex and grid layouts and looks narrower than its container.
Use CSS to Make Tables Readable on a Narrow Screen
Two dependable approaches exist for phone widths. The first keeps the table as a table and wraps it in a scroll container, so the user swipes horizontally within the table without the page scrolling. The second restructures the cells into a stacked card layout using media queries. Neither is "correct" in all cases — pick by whether users compare across rows or read one record at a time.
The scroll-container approach is simpler and preserves the table's semantics. Wrap the in , then apply overflow-x: auto plus max-width: 100% to the wrapper. Add -webkit-overflow-scrolling: touch for momentum scrolling on older iOS versions. On a 375-pixel iPhone width, a six-column table needs roughly 600 pixels of content width, so the user scrolls about 225 pixels horizontally. That is acceptable for data tables and unacceptable for navigation.
The stacked-card approach sets display: block on , , , , , and at small widths, then uses ::before pseudo-elements to inject the column label before each cell. This approach breaks the semantic table role in many screen readers, because changing display on a table element strips its implicit ARIA role in some browsers. Restoring role="table", role="row", and role="cell" explicitly is the usual workaround, and testing on real VoiceOver and NVDA installs is mandatory before shipping it.
Make Tables Accessible With Scope, Caption, and Empty-Cell Rules
Accessibility for tables hinges on three things: a caption, correct scope attributes, and never using tables for page layout. Layout tables — once standard practice in the late 1990s and early 2000s — confuse screen readers because the user is told "table with 3 columns and 47 rows" while navigating a web page's chrome. The HTML5 spec actually discourages layout tables but permits them with role="presentation" to strip the table semantics. If you find a layout table in legacy code, add that role rather than rebuilding the page.
A short quiz before shipping: does every header cell have a scope value? Is there a caption? Does every data cell sit inside a ? Do empty cells carry text content such as a zero or a dash? If all four pass, most assistive technology handles the table correctly. The W3C's Web Content Accessibility Guidelines success criterion 1.3.1 requires that information and structure be programmatically determinable, and table markup is the canonical example.
Sortable tables add complexity. A clickable column header that sorts the table should carry aria-sort="ascending" or aria-sort="descending" on the active header, and aria-sort="none" on the others. Without those attributes, a screen-reader user clicks the header and hears no confirmation of the order. Add the attribute in JavaScript when the sort changes, and remove it when the sort is cleared.
Color alone cannot convey table data. If you shade a cell red for "overdue" and green for "on track," add text or an icon alongside, because deuteranopia affects roughly 1 in 12 men and makes red-green distinctions invisible. This rule applies to every table, not just accessibility-critical ones, and a 2024 WebAIM analysis of 1 million home pages found color-only data indicators in 4.3 percent of pages sampled.
Understand What Tables Are Not For in Modern HTML
Tables belong to tabular data: numbers, dates, comparisons, schedules — anything where a reader needs to look up a value by both row and column. They do not belong to page layout, form alignment, or navigation menus. CSS Grid and Flexbox solve layout without corrupting the reading order, and every browser that supports HTML5 tables also supports display: grid and display: flex. The switch is not a performance trade.
A frequent misuse is a "table" that holds only one column of content — a list of articles, a schedule of events without a second value to compare. Those belong in , , or elements. A single-column table confuses assistive technology because the header-to-cell relationship carries no meaning. Convert it to a list and style the list items into a grid visually; the semantics will match what the reader expects.
Form layouts built with tables are a classic anti-pattern. Every input ends up in a , which forces label and control into a fixed grid that cannot reflow at narrow widths. Modern CSS with display: grid on the element handles a two-column label/input layout with grid-template-columns: max-content 1fr, and it reflows to stack on mobile without the semantic baggage. Converting a legacy form table is one of the highest-return accessibility fixes you can make.
Tables that contain hierarchical data — parent rows with child rows indented beneath them — technically work but read poorly. VoiceOver will announce each row as its own grid position, and a screen reader cannot convey visual indentation. When hierarchy matters, either use nested lists or use multiple tables under distinct captions. The hierarchy in a table is flat by design; pretending otherwise with indentation is a styling decision that leaves out non-visual users.
Create the Smallest Table That Still Passes a Screen-Reader Test
Most HTML table tutorials stop at markup and skip the test that proves the markup works. A quick validation routine every table author should run: load the page in a browser, open the developer tools' accessibility pane, then navigate the table with a screen reader. On macOS, VoiceOver runs with Command-F5 and moves between table cells with Control-Option-arrow keys. Listen for whether headers are announced with each cell. A correctly built table announces "Product, Q1 units, 140" as the user moves into a cell, not "140" alone.
For a Windows environment, NVDA is free from NV Access and announces table structure similarly with Control-Alt-arrow keys in browse mode. Test at least one row header and one column header to span the header types you used. Also test at 200 percent browser zoom — the WCAG 1.4.4 reflow criterion requires that content remain usable without horizontal scrolling at 320 CSS pixels wide, though tables exempted from reflow may scroll within their own container if a scroll mechanism exists.
Structural validation is separate from assistive testing. The WHATWG Living Standard defines the table model that browsers implement, and the W3C validator checks against it. A valid table with correct scope attributes still fails usability if the caption is jargon. Write captions that read as a complete phrase: "Quarterly inventory by product, first half 2026" tells a screen-reader user where they are; "table1" does not.
Run the test on at least one wide table and one narrow table in your project, because the failure modes differ. Wide tables fail on horizontal scroll and header association. Narrow tables fail on cramped cells and missing captions. Both failures disappear after the same structural additions, and neither can be caught by viewing the page only on a desktop monitor.
FAQ
Does every HTML table need a header row?
No — a table with one data cell is legal HTML, and a table holding only data with no labels is also legal. But WCAG 1.3.1 and every major screen reader expect programmatically determinable headers, so any table that communicates structure needs cells with scope values. Tables used purely for visual alignment should carry role="presentation" so assistive technology skips them entirely.
What is the difference between colspan and rowspan?
colspan merges a cell across columns and rowspan merges it across rows; both take positive integers and default to 1. A colspan="3" cell occupies three horizontal slots, which means that row contains two fewer cells than the rows below it. Spanning both directions at once — a colspan="2" rowspan="2" cell — is legal but is where most hand-written tables develop cell drift, so compute each row's cell count before typing.
Why does my table have no borders?
Browsers apply no visible border to or by default — the default user-agent stylesheet sets border: none. Add border: 1px solid #ccc to the cells and border-collapse: collapse to the to produce a single-line grid. Without border-collapse, adjacent cell borders double up into two-pixel internal lines, which is the second most common table styling complaint behind missing borders entirely.
Can I use a table for website layout?
You can, since browsers still render layout tables, but the practice is discouraged by the HTML5 spec and should be replaced with CSS Grid or Flexbox. If legacy code has layout tables, add role="presentation" so screen readers skip the table dialogue. A 2019 WebAIM survey of screen-reader users found layout tables were the most-cited cause of confusing navigation, ahead of missing alt text on decorative images.
How do I make a table responsive on a phone?
Wrap the in a with overflow-x: auto, which lets users swipe the table horizontally while the page itself stays fixed. If reading one record at a time matters more than comparing across rows, restructure the cells to stack vertically with CSS media queries and restore role="table" explicitly, because display: block strips the implicit table role in Chrome and Safari.
What is aria-sort used for?
aria-sort tells assistive technology which column is currently sorted and in which direction, using ascending, descending, or none as values. It's only meaningful on a that actually controls a sort operation. Without it, screen-reader users click a sortable header and hear no confirmation, so the change is invisible to them even though sighted users see the reordered rows.
Do tables with numbers need special formatting?
Right-align numeric columns with text-align: right so digits line up for comparison, and use for the values with scope="col" labels above. Don't include units in every cell when a single unit label in the header is enough — "Cost (USD)" in a header announces to every cell that follows. Use consistent decimal precision across a column; mixed precision within a column is the failure case most often reported by screen-reader users reading financial or scientific tables.