Questions tagged [html-table]

As it relates to HTML, tables are used to display data in a tabular fashion. Questions regarding HTML tables should be asked by showing the source-code one has problem with, or, if the question is about a failed attempt to create a table according to the desires, the asker needs to show the failed try, describe the expectations and how the behavior was different.

The <table> element is used to display a grid of data. The <table> is sometimes incorrectly used to position elements on a web page, which should be avoided since tables have semantic meaning. Tables are divided into many sub-elements to make them work.

Like any HTML tag, the <table> tag's behavior can be modified via HTML attributes. The most important attributes for a <table> are the following:

  • border defines tables border thickness. ie border="1"
  • dir defines the direction of the table. ie dir="rtl" or dir="ltr"
  • cellpadding defines space around a cell's content i.e. cellpadding="10"
  • cellspacing defines space around cells ie cellspacing="10" border, cellpadding and cellspacing

Tables are divided into a number of subelements:

  • <caption> is the tile of the table. It's optional but if present it has to be the first tag after the <table> tag.
  • <thead>, <tbody>, and <tfoot> will separate the table into their respective sections of header, body, and footer. Using these elements allows better semantic meaning for certain rows of the table, which may be titles to columns (in the header) or a total row (in the footer), rather than pushing everything into the body. If none of these are specified and the table immediately goes into rows, the browser will see it as all being contained within the body.
  • <tr> starts a new row in the table. Table rows are not required to be closed by a </tr> but it is recommended to do so in most cases in order to avoid confusion. A table row will continue until a new row is opened or the header, body, or footer which contains it is closed. A table can contain any number of rows.
  • <th> defines header cell within a row, This will contains the header information/title, and can be decorated differently in the table.
  • <td> defines a Standard cell within a row, which contains some content/data. Like table rows, table cells are also not required to be closed by a </td>. A table cell will continue until a new cell is opened or the row which contains it is closed. A table row can contain any number of cells. Similarly, the <th> element is often used inside the table header. It is an alternative to a regular table cell which sets it aside from other cells as a defining point that identifies the information below or to the side of it.

Also, the cells in a <table> can be merged:

colspan and rowspan

code for rowspan:

<table width="300" border="1">
    <tbody>
        <tr>
            <td rowspan="2">Merged</td>
            <td>Table First Row</td>
        </tr>
        <tr>
            <td>Table Second Row</td>
        </tr>
    </tbody>
</table>

code for colspan:

<table width="300" border="1">
    <tbody>
        <tr>
            <td colspan="2">Merged</td>
        </tr>
        <tr>
            <td>Table First Col</td>
            <td>Table Second Col</td>
        </tr>
    </tbody>
</table>

code for no borders:

<table width="300" cellspacing="0" cellpadding="0">
    <tbody>
        <tr>
            <td colspan="2">Merged</td>
        </tr>
        <tr>
            <td>Table First Col</td>
            <td>Table Second Col</td>
        </tr>
    </tbody>
</table>

This tag is related to .

22957 questions
340
votes
9 answers

How to center the contents of an HTML table?

I am using an HTML and I want to align the text of
to the center in each cell. How do I center align the text horizontally and vertically?
Rajagopal 웃
  • 8,061
  • 7
  • 31
  • 43
327
votes
13 answers

Setting table column width

I've got a simple table that is used for an inbox as follows:
From Subject Date
How do I set the width so the From and Date are 15% of the page width…
alamodey
  • 14,320
  • 24
  • 86
  • 112
311
votes
13 answers

How to deal with page breaks when printing a large HTML table

I have a project which requires printing an HTML table with many rows. My problem is the way the table is printed over multiple page. It will sometimes cut a row in half, making it unreadable because one half is on the bleeding edge of a page and…
h3.
  • 10,688
  • 15
  • 51
  • 54
303
votes
4 answers

Form inside a table

I'm including some forms inside a HTML table to add new rows and update current rows. The problem that I'm getting is that when I inspect the forms in my dev tools, I see that the form elements are closed immediately after opening (inputs, etc are…
Alex
  • 8,353
  • 9
  • 45
  • 56
271
votes
11 answers

How to get multiple selected values of select box in php?

I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows: …
Param-Ganak
  • 5,787
  • 17
  • 50
  • 62
261
votes
19 answers

How can I truncate table cells, but fit as much as content possible?

Meet Fred. He's a table:
This cells has more content Less content here
Fred's apartment has a bizarre habit of changing size, so he's learned to…
s4y
  • 50,525
  • 12
  • 70
  • 98
251
votes
31 answers

HTML table with fixed headers?

Is there a cross-browser CSS/JavaScript technique to display a long HTML table such that the column headers stay fixed on-screen and do not scroll with the table body. Think of the "freeze panes" effect in Microsoft Excel. I want to be able to…
Cheekysoft
  • 35,194
  • 20
  • 73
  • 86
251
votes
12 answers

Create a table without a header in Markdown

Is it possible to create a table without a header in Markdown? The HTML would look like this:
Key 1 Value 1
Key 2 Value 2
adius
  • 13,685
  • 7
  • 45
  • 46
231
votes
14 answers

How to remove unwanted space between rows and columns in table?

How do I remove the extra space between the rows and columns in the table. I've tried changing the margin, padding, and various border properties on the table and tr and td. I want the pictures to all be right next to each other to look like one big…
Zach Galant
  • 2,451
  • 2
  • 15
  • 8
226
votes
5 answers

TypeError: a bytes-like object is required, not 'str' in python and CSV

TypeError: a bytes-like object is required, not 'str' I'm getting the above error while executing the below python code to save the HTML table data in a CSV file. How do I get rid of that error? import csv import requests from bs4 import…
ShivaGuntuku
  • 5,274
  • 6
  • 25
  • 37
221
votes
21 answers

How to Use slideDown (or show) function on a table row?

I'm trying to add a row to a table and have that row slide into view, however the slidedown function seems to be adding a display:block style to the table row which messes up the layout. Any ideas how to work around this? Here's the…
Greg
  • 45,306
  • 89
  • 231
  • 297
219
votes
20 answers

Add a horizontal scrollbar to an HTML table

Is there a way to add a horizontal scrollbar to an HTML table? I actually need it to be scrollable both vertically and horizontally depending on how the table grows but I cannot get either scrollbar to appear.
Tsundoku
  • 9,104
  • 29
  • 93
  • 127
214
votes
10 answers

How can I apply a border only inside a table?

I am trying to figure out how to add border only inside the table. When I do: table { border: 0; } table td, table th { border: 1px solid black; } The border is around the whole table and also between table cells. What I want to achieve is…
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
208
votes
15 answers

jQuery table sort

I have a very simple HTML table with 4 columns: Facility Name, Phone #, City, Specialty I want the user to be able to sort by Facility Name and City only. How can I code this using jQuery?
tony noriega
  • 7,523
  • 17
  • 53
  • 72
196
votes
7 answers

Fixed Table Cell Width

A lot of people still use tables to layout controls, data etc. - one example of this is the popular jqGrid. However, there is some magic happening that I cant seem to fathom (its tables for crying out loud, how much magic could there possibly…
Jimbo
  • 22,379
  • 42
  • 117
  • 159