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
196
votes
13 answers

How to insert a row in an HTML table body in JavaScript

I have an HTML table with a header and a footer: …
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
192
votes
12 answers

How to completely remove borders from HTML table

My goal is to make an HTML page that is similar to a "photo frame". In other words, I want to make a blank page that is surrounded by 4 pictures. This is my code:
My Header
aaaaa
yazanpro
  • 4,512
  • 6
  • 44
  • 66
188
votes
7 answers

html tables: thead vs th

It looks like (according to the examples on this page, anyways) that if you're using THEAD, you don't need to use TH. Is that true? If so, what are the advantages/disadvantages of THEAD vs TH?
chris
  • 36,094
  • 53
  • 157
  • 237
184
votes
10 answers

HTML table headers always visible at top of window when viewing a large table

I would like to be able to "tweak" an HTML table's presentation to add a single feature: when scrolling down through the page so that the table is on the screen but the header rows are off-screen, I would like the headers to remain visible at the…
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
180
votes
2 answers

Border around tr element doesn't show?

It seems like Chrome/Firefox do not render borders on tr, but it renders the border if the selector is table tr td. How can I set a border on a tr ? My attempt, which doesn't work: table tr { border: 1px solid black; }
edi9999
  • 19,701
  • 13
  • 88
  • 127
174
votes
27 answers

Table header to stay fixed at the top when user scrolls it out of view with jQuery

I am trying to design an HTML table where the header will stay at the top of the page when AND ONLY when the user scrolls it out of view. For example, the table may be 500 pixels down from the page, how do I make it so that if the user scrolls the…
user2385136
173
votes
4 answers

Align right in a table cell with CSS

I have the old classic code like this,
which does what it says: it right aligns the content in the cell. So if I put two buttons in this cell, they will appear at the right site of the cell. I was refactoring this to CSS, but…
Michel
  • 23,085
  • 46
  • 152
  • 242
169
votes
15 answers
tag
I want to wrap some text that is added to a
element. I have tried with style="word-wrap: break-word;" width="15%". But it is not wrapping the text. Is it mandatory to give it 100% width? I have other controls to display so only 15% width is…
sagar
165
votes
9 answers

Is a DIV inside a TD a bad idea?

It seems like I heard/read somewhere that a
inside of a
was a no-no. Not that it won't work, just something about them not being really compatible based on their display type. Can't find any evidence to back up my hunch, so I may be…
jcollum
  • 43,623
  • 55
  • 191
  • 321
163
votes
6 answers

100% width table overflowing div container

I am having issues with an html table that is overflowing it's parent container. .page { width: 280px; border:solid 1px blue; } .my-table { word-wrap: break-word; } .my-table td { border:solid 1px #333; }
Victor
  • 4,989
  • 6
  • 31
  • 47
163
votes
9 answers

Hide/Show Column in a HTML Table

I have an HTML table with several columns and I need to implement a column chooser using jQuery. When a user clicks on a checkbox I want to hide/show the corresponding column in the table. I would like to do this without attaching a class to every…
Brian Fisher
  • 23,519
  • 15
  • 78
  • 82
163
votes
7 answers

CSS table-cell equal width

I have an indeterminate number of table-cell elements inside a table container.
Is there a pure CSS way to get the table-cells…
Harry
  • 52,711
  • 71
  • 177
  • 261
159
votes
11 answers
content to wrap?
Heres the entire page * wrappable is defined in a main.css file /* Wrappable cell * Add this class to make sure the text in a cell will wrap. * By default, data_table tds do not wrap. */ td.wrappable, table.data_table td.wrappable { white-space:…
Doc Holiday
  • 9,928
  • 32
  • 98
  • 151
154
votes
16 answers

Bootstrap table striped: How do I change the stripe background colour?

With Bootstrap class table-striped, every other row in my table has a background colour equal to #F9F9F9. How can I change this colour?
drake035
  • 3,955
  • 41
  • 119
  • 229
149
votes
18 answers

How can I let a table's body scroll but keep its head fixed in place?

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. Think a mini…
minty
  • 22,235
  • 40
  • 89
  • 106