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
121
votes
10 answers

Jquery insert new row into table at a certain index

I know how to append or prepend a new row into a table using jquery: $('#my_table > tbody:last').append(html); How to I insert the row (given in the html variable) into a specific "row index" i. So if i=3, for instance, the row will be inserted as…
aeq
  • 10,377
  • 15
  • 45
  • 46
121
votes
6 answers

Tooltips for cells in HTML table (no Javascript)

Is it possible to have tooltips for table cells with no JavaScript. Can't use it.
dublintech
  • 16,815
  • 29
  • 84
  • 115
119
votes
7 answers

How do I set the table cell widths to minimum except last column?

I have a table with 100% width. If I put s in it, they get spread out with equal length columns. However, I want all the columns except last to have as small a width as possible, without wrapping text. What I did first was that I set width="1px"…
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
119
votes
6 answers

HTML table td meaning

In HTML table, what does td stand for? I mean literally, what is it an acronym for? Table division? Table data?
ilija veselica
  • 9,414
  • 39
  • 93
  • 147
118
votes
10 answers

clear table jquery

I have an HTML table filled with a number of rows. How can I remove all the rows from the table?
learning
  • 11,415
  • 35
  • 87
  • 154
115
votes
7 answers

Table column sizing

In Bootstrap 3, I could apply col-sm-xx to the th tags in the thead and resize table columns at will. However this doesn't work in bootstrap 4. How can I achieve something like this in bootstrap 4? 3 columns…
Killerpixler
  • 4,200
  • 11
  • 42
  • 82
114
votes
10 answers

How to display scroll bar onto a html table

I am writing a page where I need an html table to maintain a set size. I need the headers at the top of the table to stay there at all times but I also need the body of the table to scroll no matter how many rows are added to the table. I want it to…
BruceyBandit
  • 3,978
  • 19
  • 72
  • 144
114
votes
14 answers

Internet Explorer 9 not rendering table cells properly

My website has always run smoothly with IE8, IE7, FF, Chrome and Safari. Now I'm testing it on IE9 and I'm experiencing a strange problem: in some pages, some tabular data renders incorrectly. The HTML source is correct and all, and the row giving…
fgpx78
  • 1,270
  • 2
  • 10
  • 8
114
votes
5 answers

Using Position Relative/Absolute within a TD?

I have the following code: Contents of table cell, variable height, could be more than 60px;
Notice …
Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78
114
votes
4 answers

Equal sized table cells to fill the entire width of the containing table

Is there a way using HTML/CSS (with relative sizing) to make a row of cells stretch the entire width of the table within which it is contained? The cells should be equal widths and the outer table size is also dynamic with
yogibear
  • 14,487
  • 9
  • 32
  • 31
113
votes
7 answers

How to fix height of TR?

Is it possible to fix the height of a row (tr) on a table? The problem appears when I shrink the window of the browser, some rows start playing around, and I can not fix the height of the row. I tried several ways: tr width="20" / tr…
Amra
  • 24,780
  • 27
  • 82
  • 92
113
votes
9 answers

Set min-width in HTML table's

My table has several columns. Each column should have dynamic width that depends on the browser window size. On the other hand, each column must not be too tiny. So I tried to set min-width for those columns but it's not a valid property. Tried…
Thanhma San
  • 1,347
  • 2
  • 8
  • 12
111
votes
14 answers

HTML input textbox with a width of 100% overflows table cells

Does anyone know why the input elements with a width of 100% go over the table's cells border. In the simple example below input box go over the table's cells border, the result is horrible. This was tested and it happens in the same way on:…
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
109
votes
10 answers

How to hide columns in HTML table?

I have created a table in ASPX. I want to hide one of the columns based on the requirement but there is no attribute like visible in the HTML table building. How can I solve my problem?
user601367
  • 2,308
  • 12
  • 34
  • 44
109
votes
14 answers

HTML/CSS: Making two floating divs the same height

I have a little peculiar problem that I currently solve using a table, see below. Basically, I want to have two divs take up 100% of the available width, but only take up as much vertical space as needed (which isn't really that obvious from the…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162