Search code examples
htmlcsscss-transformsvertical-text

How to reduce the width of tilt column header?


Below is the output table:

enter image description here

I'm looking for a solution which will reduce the width of the table header, I've tried couple of solutions but none of them are working. Below is my code:

<Style>
  /*additional CSS*/
  
  td,
  th {
    border: 1px solid black;
    font-family: Calibri (Body) !important;
  }
  
  table {
    border-collapse: collapse;
  }
  
  * {
    font-family: 'Calibri (Body) !important';
    line-height: 0.5 em;
  }
  
  .header {
    text-align: center;
    white-space: nowrap;
    transform-origin: 50% 50%;
    transform: rotate(-90deg);
  }
  
  .header:before {
    content: '';
    padding-top: 110%;
    /* takes width as reference, + 10% for faking some extra padding */
    display: inline-block;
    vertical-align: middle;
  }
  
  .header:after {
    background-color: #005CB9;
    color: white;
  }
</Style>
<table border="1">
  <thead>
    <tr style=" background-color: #005CB9;color: white;">
      <td style='text-align: center;'>

        <strong>Activity</strong>

      </td>
      <td class='header'>

        <strong>Screening</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345-1</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 1</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 2</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 3</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 4</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 5</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 6</strong>
        </p class='headerText'>
      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 7</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 8</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 9</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 10</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 11</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 12</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 13</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 14</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 15</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 16</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 17</strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 23 (Follow-Up Visit)
                        </strong>

      </td>
      <td class='header'>

        <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 Premature Discontinuation</strong>

      </td>
    </tr>
  </thead>
</table>


Solution

  • Instead of using transform for vertical text, you can try writing mode for this (its supported well in modern browsers):

    writing-mode: vertical-rl;
    transform: rotate(-180deg);
    

    And finally adjust your line-height - see demo below:

    td,
    th {
      border: 1px solid black;
      font-family: Calibri (Body) !important;
    }
    
    table {
      border-collapse: collapse;
    }
    
    * {
      font-family: 'Calibri (Body) !important';
      line-height: 1.5 /* adjusted */
    }
    
    .header {
      text-align: center;
      white-space: nowrap;
      /*transform-origin: 50% 50%;
      transform: rotate(-90deg);*/
    }
    .header strong { /* added */
       writing-mode: vertical-rl;
       transform: rotate(-180deg);
    }
    
    .header:before {
      content: '';
      padding-top: 110%;
      display: inline-block;
      vertical-align: middle;
    }
    
    .header:after {
      background-color: #005CB9;
      color: white;
    }
    <table border="1">
      <thead>
        <tr style=" background-color: #005CB9;color: white;">
          <td style='text-align: center;'>
            <strong>Activity</strong>
          </td>
          <td class='header'>
            <strong>Screening</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345-1</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 1</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 2</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 3</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 4</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 5</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 6</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 7</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 8</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 9</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 10</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 11</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 12</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 13</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 14</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 15</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 16</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 17</strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 23 (Follow-Up Visit)
                            </strong>
          </td>
          <td class='header'>
            <strong>ABCDEFGHJIJKLMNOPQRSTUVWXYZ12345 Premature Discontinuation</strong>
          </td>
        </tr>
      </thead>
    </table>