2

I have a table like this:

+----------+----------+
| record 1 | record 2 |
+----------+----------+
| record 3 | record 4 |
+----------+----------+
| record 5 | record 6 |
+----------+----------+

Now I want to add a integrated row in the first of it, something like this structure:

+---------------------+
|     Table Name      |
+----------+----------+
| record 1 | record 2 |
+----------+----------+
| record 3 | record 4 |
+----------+----------+
| record 5 | record 6 |
+----------+----------+

Here is my codes:

HTML:

<table cellspacing="0">
    <tr>
        <td>
            Table Name
        </td>
    </tr>
    <tr>
        <td>record 1</td>
        <td>record 2</td>
    </tr>
    <tr>
        <td>record 3</td>
        <td>record 4</td>
    </tr>
    <tr>
        <td>record 5</td>
        <td>record 6</td>
    </tr>
</table>

CSS:

td{    
    padding: 5px;
    border: 1px solid;
}

Also here is a fiddle.

So, How can I do that?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89

4 Answers4

2

You need to use the colspan attribute.

td{    
    padding: 5px;
    border: 1px solid;
    text-align: center;
}
<table cellspacing="0">
    <tr>
        <td colspan="2">
            Table Name
        </td>
    </tr>
    <tr>
        <td>record 1</td>
        <td>record 2</td>
    </tr>
    <tr>
        <td>record 3</td>
        <td>record 4</td>
    </tr>
    <tr>
        <td>record 5</td>
        <td>record 6</td>
    </tr>
</table>
Florin Pop
  • 5,105
  • 3
  • 25
  • 58
2

Use a thead with colspan to differentiate the content from the label. Make the rest of your content in the tbody:

<table cellspacing="0">
    <thead>
        <tr>
            <th colspan="2">Table Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>record 1</td>
            <td>record 2</td>
        </tr>
        <tr>
            <td>record 3</td>
            <td>record 4</td>
        </tr>
        <tr>
            <td>record 5</td>
            <td>record 6</td>
        </tr>
    </tbody>
</table>

And slightly adjust the CSS:

td, th {
    padding: 5px;
    border: 1px solid;
}

See DEMO

Eric Hotinger
  • 8,957
  • 5
  • 36
  • 43
1

Use colspan.

<table cellspacing="0">
    <tr>
        <td colspan="2">
            Table Name
        </td>
    </tr>
    <tr>
        <td>record 1</td>
        <td>record 2</td>
    </tr>
    <tr>
        <td>record 3</td>
        <td>record 4</td>
    </tr>
    <tr>
        <td>record 5</td>
        <td>record 6</td>
    </tr>
</table>

Here's a fiddle.

Ryan Warner
  • 1,354
  • 1
  • 10
  • 7
1

Use caption elements for table titles:

The caption element represents the title of the table that is its parent

As explained in Tables in the visual formatting model, caption boxes are rendered as normal block boxes inside the table wrapper box, but outside the table box:

enter image description here

table {
  border-spacing: 0;
}
caption, td {    
  padding: 5px;
  border: 1px solid;
}
<table>
  <caption>Table Name</caption>
  <tr>  <td>record 1</td> <td>record 2</td>  </tr>
  <tr>  <td>record 3</td> <td>record 4</td>  </tr>
  <tr>  <td>record 5</td> <td>record 6</td>  </tr>
</table>
Oriol
  • 274,082
  • 63
  • 437
  • 513