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
148
votes
27 answers

Delete all rows in an HTML table

How can I delete all rows of an HTML table except the 's using Javascript, and without looping through all the rows in the table? I have a very huge table and I don't want to freeze the UI while I'm looping through the rows to delete them
K''
  • 5,020
  • 7
  • 33
  • 43
148
votes
13 answers

Table overflowing outside of div

I'm trying to stop a table that has width explicitly declared from overflowing outside of its parent div. I presume I can do this in some way using max-width, but I can't seem to get this working. The following code (with a very small window) will…
waiwai933
  • 14,133
  • 21
  • 62
  • 86
146
votes
17 answers

Create table using Javascript

I have a JavaScript function which creates a table with 3 rows 2 cells. Could anybody tell me how I can create the table below using my function (I need to do this for my situation)? Here is my javascript and html code given below: function…
Manny Fitzgerald
  • 1,477
  • 2
  • 10
  • 5
141
votes
16 answers

How can I hide an HTML table row so that it takes up no space?

How can I hide an HTML table row so that it takes up no space? I have several 's set to style="display:none;", but they still affect the size of the table and the table's border reflects the hidden rows.
MikeN
  • 45,039
  • 49
  • 151
  • 227
139
votes
18 answers

Rounded table corners CSS only

I have searched and searched, but haven't been able to find a solution for my requirement. I have a plain ol' HTML table. I want round corners for it, without using images or JS, i.e. pure CSS only. Like this: Rounded corners for corner cells, and…
Vishal Shah
  • 3,774
  • 4
  • 23
  • 25
139
votes
11 answers

Giving a border to an HTML table row,

Is it possible to border a table row, in one go instead of giving a border to individual cells, like,
Tiny
  • 27,221
  • 105
  • 339
  • 599
131
votes
11 answers

Html table tr inside td

I am trying to create a table in HTML. I have the following design to create. I had added a
inside the
but somehow the table is not created as per the design. Can anyone suggest me how I can achieve this? I am unable to create Name1 |…
Scorpion
  • 6,831
  • 16
  • 75
  • 123
130
votes
4 answers

Convert json data to a html table

Is there any jQuery or javascript library that generates a dynamic table given json data? I don't want to define the columns, the library should read the keys in the json hash and generate columns. Of course, I can myself iterate through the json…
Manish Mulani
  • 7,125
  • 9
  • 43
  • 45
130
votes
22 answers

Table with fixed header and fixed column on pure css

I need to create a html table (or something similar looking) with a fixed header and a fixed first column. Every solution I've seen so far uses Javascript or jQuery to set scrollTop/scrollLeft, but it doesn't work smoothly on mobile browsers, so…
panfil
  • 1,489
  • 3
  • 13
  • 19
129
votes
4 answers

How can I set the max-width of a table cell using percentages?

Test A long string blah blah blah
The above does not work. How can I set the max-width of a table cell using percentages?
Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
129
votes
14 answers

How to make HTML table cell editable?

I'd like to make some cells of html table editable, simply double click a cell, input some text and the changes can be sent to server. I don't want to use some toolkits like dojo data grid. Because it provides some other features. Would you provide…
wqfeng
  • 1,461
  • 3
  • 11
  • 11
129
votes
15 answers

td widths, not working?

So I have this code here:
user979331
  • 11,039
  • 73
  • 223
  • 418
126
votes
7 answers

Padding a table row

Table Row Padding Issue
Spencer
  • 4,018
  • 10
  • 33
  • 43
126
votes
6 answers
height
I've looked through several posts on StackOverflow, but haven't been able to find an answer to this rather simple question. I have an HTML construct like this:
user191152
125
votes
18 answers

Sorting HTML table with JavaScript

I'm after a table sorting solution (in JavaScript) but I can't seem to find a suitable one yet. I just need it to sort each column alphabetically. It doesn't need to ignore any code or any numbers or to work with currency. Just a click on the column…
lukeseager
  • 2,365
  • 11
  • 37
  • 57