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
108
votes
16 answers

html - table row like a link

I can't set my table row as link to something. I can use only css and html. I tried different things from div in row to something another, but still can't make it works.
Max Frai
  • 61,946
  • 78
  • 197
  • 306
103
votes
11 answers

Using CSS td width absolute, position

Please see this JSFIDDLE td.rhead { width: 300px; } Why doesn't the CSS width work?
Jake
  • 11,273
  • 21
  • 90
  • 147
102
votes
8 answers

Removing unwanted table cell borders with CSS

I have a peculiar and frustrating problem. For the simple markup:
need 300px Week #0 Week #1
Bob
  • 1,080
  • 2
  • 7
  • 6
102
votes
3 answers

jQuery each loop in table row

I am having something like:
123
ab>c
ANP
  • 15,287
  • 22
  • 58
  • 79
99
votes
7 answers

Spacing between thead and tbody

I have a simple html table like this:
Column 1Column 2
Value 1Value 2
Value 3Value…
Ben
  • 2,365
  • 4
  • 22
  • 29
99
votes
8 answers

Table's border-radius does not function as expected

I want to add a border radius around the entire table. But the following code is not working in both the latest versions of Firefox and Google Chrome. table { border-spacing: 0; width: 600px; margin: 30px; border: 1px solid #CCCCCC; …
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
98
votes
11 answers

How do you use colspan and rowspan in HTML tables?

I don't know how to merge rows and columns inside HTML tables. Can you please help me with making such a table in HTML?
Max Frai
  • 61,946
  • 78
  • 197
  • 306
98
votes
12 answers

DataTables: Cannot read property 'length' of undefined

I understand this a popular issue, and I have read all the similar questions here on Stack Overflow and other sites (including the datatables website). To clarify, I am using PHP Codeigniter Materliazecss I have also made sure that I received the…
Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61
97
votes
10 answers

Get the contents of a table row with a button click

I need to extract the details of each column in my table. For example, column "Name/Nr.". The table contains a number of addresses The very last column of each row has a button that lets a user choose a listed address. Problem: My code only picks…
chuckfinley
  • 2,577
  • 10
  • 32
  • 42
97
votes
3 answers

Apply style to cells of first row

It should be very simple but I can't figure it out. I have a table like this :
blabla 1 blabla 2
blabla 3 blabla 4
I want to make td tags of first…
xperator
  • 2,743
  • 7
  • 34
  • 58
96
votes
4 answers

how to rotate text left 90 degree and cell size is adjusted according to text in html

Suppose i have table with some rows and column,so i want to rotate text in cells something like this: problem is when i rotate text using style : #rotate { -moz-transform: rotate(-90.0deg); /* FF3.5+ */ -o-transform: rotate(-90.0deg);…
lata
  • 1,575
  • 3
  • 13
  • 21
94
votes
9 answers

Remove all padding and margin table HTML and CSS

I have this table :
1 2
and…
begiPass
  • 2,124
  • 6
  • 36
  • 57
92
votes
4 answers

How can I construct a table header than spans multiple rows in HTML?

I would like to construct a table as follows: | Major Heading 1 | Major Heading 2 | | Minor1 | Minor2 | Minor3 | Minor4 | ---------------------------------------------- | col1 | col2 | col3 | col4 | rest of table…
statguy
  • 1,627
  • 3
  • 13
  • 22
90
votes
12 answers

Using jQuery to build table rows from AJAX response(json)

Possible duplicate Nested elements I'm getting from server-side ajax response (Json) and I'm trying to dynamically create table rows and append them to an existing table with id=records_table. I tried to implement the solution in possible duplicate…
Canttouchit
  • 3,149
  • 6
  • 38
  • 51
90
votes
1 answer

Using jQuery UI sortable with HTML tables

I want to output some data from the database in a HTML table, and I want the user to be able to reorder table rows. To achieve this, I used jQuery UI sortable, thus: