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 .

21152 questions
3473
votes
30 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
  • 40,828
  • 5
  • 32
  • 32
2531
votes
40 answers

Add table row in jQuery

What is the best method in jQuery to add an additional row to a table as the last row? Is this acceptable? $('#myTable').append('
'); Are there limitations to what you can add to a table like this (such as…
Darryl Hein
  • 134,677
  • 87
  • 206
  • 257
2225
votes
36 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
  • 28,356
  • 5
  • 29
  • 51
614
votes
8 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
  • 134,556
  • 218
  • 544
  • 822
611
votes
26 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
  • 35,551
  • 33
  • 95
  • 134
568
votes
22 answers
I need to set up a fixed width for
user1038814
  • 8,281
  • 14
  • 56
  • 84
486
votes
10 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
  • 41,002
  • 66
  • 200
  • 371
490
votes
26 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
  • 10,526
  • 9
  • 31
  • 45
481
votes
23 answers

How do I keep two side-by-side divs the same height?

I have two divs side by side. I'd like the height of them to be the same, and stay the same if one of them resizes. I can't figure this one out though. Ideas? To clarify my confusing question, I'd like both boxes to always be the same size, so if…
NibblyPig
  • 46,891
  • 62
  • 180
  • 311
445
votes
16 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
  • 15,287
  • 17
  • 75
  • 111
433
votes
14 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
  • 90,304
  • 29
  • 114
  • 125
340
votes
24 answers

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

Edit - Original Title: Is there an alternative way to achieve border-collapse:collapse in CSS (in order to have a collapsed, rounded corner table)? Since it turns out that simply getting the table's borders to collapse does not solve the root…
vamin
  • 2,050
  • 5
  • 26
  • 30
336
votes
29 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
  • 4,755
  • 3
  • 20
  • 20
329
votes
17 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
  • 134,677
  • 87
  • 206
  • 257
312
votes
6 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)?
One one