How do I create an HTML table with labels above the column separator boundary lines

Viewed 119

I'm trying to create an HTML table that displays reservations like:

   9am      10am     11am
    |--------|--------|--------|
Mon |<booked>|<booked>|        |
    |--------|--------|--------|

I can do most of that easily but I can't figure out how to get the times on the header line to appear centered above the lines between columns as shown above rather than appearing within cells in the columns, e.g.:

       9am      10am     11am
    |--------|--------|--------|
Mon |<booked>|<booked>|        |
    |--------|--------|--------|

I've been playing around with several workarounds including having 3 columns for every "real" column so I can merge them on the header line but that leaves me needing to fill that middle cell for the data lines to make it look like a simple cell boundary so it's not great.

I feel like it's got to be something simple but google and trial-and-error are failing me so far.

I'm trying to do this within a K2 article on a Joomla web site so I'd prefer a way to do this just in HTML, not defining a CSS stylesheet as that opens a whole other problem with K2 articles in Joomla.


EDIT here is what I have thanks to @Johannes' answer but avoiding a CSS stylesheet so I don't have to deal with implementing that in K2/Joomla:

<table cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td style="width: 25%; text-align: center; vertical-align: middle;">&nbsp;</td>
<td style="width: 25%; vertical-align: middle;"><span style="display: inline-block; transform: translateX(-50%);">9am</span></td>
<td style="width: 25%; vertical-align: middle;"><span style="display: inline-block; transform: translateX(-50%);">10am</span></td>
<td style="width: 25%; vertical-align: middle;"><span style="display: inline-block; transform: translateX(-50%);">11am</span></td>
</tr>
<tr>
<td style="text-align: center; vertical-align: middle;">Mon</td>
<td style="border: 1px solid #000000; text-align: center; vertical-align: middle;">&lt;booked&gt;</td>
<td style="border: 1px solid #000000; text-align: center; vertical-align: middle;">&lt;booked&gt;</td>
<td style="border: 1px solid #000000; text-align: center; vertical-align: middle;">&nbsp;</td>
</tr>
</tbody>
</table>

1 Answers

You can do it the following way: Wrap the time indications in span tags. Make those spans inline-blocks, left-align them inside their th and move them to the left by half of their own length using transform: translateX(-50%);:

table {
  border-collapse: collapse;
  margin-left: 20px;
}

td {
  border: 1px solid #ccc;
  padding: 5px 15px;
}

th {
  font-weight: normal;
  text-align: left;
}

th>span {
  display: inline-block;
  transform: translateX(-50%);
}
<table>
  <tr>
    <th><span>9:00</span></th>
    <th><span>10:00</span></th>
    <th><span>11:00</span></th>
    <th><span>12:00</span></th>
  </tr>
  <tr>
    <td>entry 1</td>
    <td>entry 2</td>
    <td>entry 3</td>
    <td>entry 4</td>
  </tr>
  <tr>
    <td>entry 5</td>
    <td>entry 6</td>
    <td>entry 7</td>
    <td>entry 8</td>
  </tr>
</table>

Related