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
3671
votes
19 answers

Set cellpadding and cellspacing in CSS?

In an HTML table, the cellpadding and cellspacing can be set like this: How can the same be accomplished using CSS?
kokos
  • 43,096
  • 5
  • 36
  • 32
2696
votes
42 answers

Make a div fill the height of the remaining screen space

I am working on a web application where I want the content to fill the height of the entire screen. The page has a header, which contains a logo, and account information. This could be an arbitrary height. I want the content div to fill the rest of…
Vincent McNabb
  • 33,327
  • 7
  • 31
  • 53
2650
votes
42 answers

Adding a table row in jQuery

I'm using jQuery to add an additional row to a table as the last row. I have done it this way: $('#myTable').append('
'); Are there limitations to what you can add to a table like this (such as inputs,…
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
657
votes
28 answers

Word-wrap in an HTML table

I've been using word-wrap: break-word to wrap text in divs and spans. However, it doesn't seem to work in table cells. I have a table set to width:100%, with one row and two columns. Text in columns, although styled with the above word-wrap, doesn't…
psychotik
  • 38,153
  • 34
  • 100
  • 135
642
votes
7 answers
in same
my datamore data
?
Can we have multiple
tags in same
? If yes then in what scenarios should we use multiple tags?
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
640
votes
25 answers
I need to set up a fixed width for
user1038814
  • 9,337
  • 18
  • 65
  • 86
549
votes
11 answers

Alternate table row color using CSS?

I am using a table with alternate row color with this. tr.d0 td { background-color: #CC9999; color: black; } tr.d1 td { background-color: #9999CC; color: black; }
?
Simple scheme:
A B C D
. I've tried: tr.something { td { width: 90px; } } Also td.something { width: 90px; } for…
user984621
  • 46,344
  • 73
  • 224
  • 412
573
votes
28 answers

how to make a whole row in a table clickable as a link?

I'm using Bootstrap and the following doesn't work:
Blah Blah 1234567 £158,000
Kali Charan Rajput
  • 12,046
  • 10
  • 35
  • 46
539
votes
18 answers
I have a table of 3 by 3. I need a way to add a border for the bottom of every row tr and give it a specific color. First I tried the direct way, i.e.:
But that didn't work. So I added CSS like this: tr…
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
518
votes
15 answers

Colspan all columns

How can I specify a td tag should span all columns (when the exact amount of columns in the table will be variable/difficult to determine when the HTML is being rendered)? w3schools mentions you can use colspan="0", but it doesn't say exactly what…
Bob
  • 97,670
  • 29
  • 122
  • 130
440
votes
30 answers

Table fixed header and scrollable body

I am trying to make a table with fixed header and a scrollable content using the bootstrap 3 table. Unfortunately the solutions I have found does not work with bootstrap or mess up the style. Here there is a simple bootstrap table, but for some…
giulio
  • 5,865
  • 3
  • 22
  • 20
403
votes
27 answers

The border-radius property and border-collapse:collapse don't mix. How can I use border-radius to create a collapsed table with rounded corners?

I am trying to make a table with rounded corners using the CSS border-radius property. The table styles I'm using look something like this: table { -moz-border-radius:10px; -webkit-border-radius:10px; border-radius:10px } Here's the…
vamin
  • 2,178
  • 6
  • 26
  • 30
394
votes
8 answers

Fit cell width to content

Given the following markup, how could I use CSS to force one cell (all cells in column) to fit to the width of the content within it rather than stretch (which is the default behaviour)? td.block { border: 1px solid black; }
One one
Mansfield
  • 14,445
  • 18
  • 76
  • 112
391
votes
27 answers

How do I create an HTML table with a fixed/frozen left column and a scrollable body?

I need a simple solution. I know it's similar to some other questions, like: HTML table with fixed headers and a fixed column? How can I lock the first row and first column of a table when scrolling, possibly using JavaScript and CSS? But I need…
agsamek
  • 8,734
  • 11
  • 36
  • 43
342
votes
18 answers

What is the best way to remove a table row with jQuery?

What is the best method for removing a table row with jQuery?
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
1
2 3
99 100