Text in the vertical header of a table

Encountering some issues with a large table that I want to display differently, so I've opted for showing the table header vertically at a 45-degree angle. Everything works well when there are more registrations, but when performing a search that returns just one or two results, the table doesn't look as nice or user-friendly as in the provided image:

My question is how can I make the columns adjust dynamically instead of having a large left header and small right columns.

Here is a fiddle with my example :

HTML:

<table class="table table-striped table-header-rotated">
    <thead>
      <tr>
        <!-- First column header remains unrotated -->
        <th></th>
        <!-- The following headers are rotated -->
        <th class="rotate-45"><div><span>Column header 1</span></div></th>
        <th class="rotate-45"><div><span>Column header 2</span></div></th>
        <th class="rotate-45"><div><span>Column header 3</span></div></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th class="row-header">Row header 1</th>
        <td>33</td>
        <td>978</td>
        <td>57</td>
      </tr>
      <tr>
        <th class="row-header">Row header 2</th>
        <td>99</td>
        <td>678</td>
        <td>67</td>
      </tr>
      <tr>
        <th class="row-header">Row header 3</th>
        <td>332</td>
        <td>701</td>
        <td>877</td>
      </tr>
    </tbody>
  </table>

CSS:

.table{width:93%;}
.table-header-rotated th.row-header{
  width: auto ;
}
.table-header-rotated td{
  width: 40px;
  border-top: 1px solid #dddddd;
  border-left: 1px solid #dddddd;
  border-right: 1px solid #dddddd;
  vertical-align: middle;
  text-align: center;
}

.table-header-rotated th.rotate-45{
  height: 80px;
  position: relative;
  vertical-align: bottom;
  padding: 0;
  font-size: 12px;
  line-height: 0.8;
}

.table-header-rotated th.rotate-45 > div{
  position: relative;
  top: 0px;
  left: 40px; /* 80 * tan(45) / 2 = 40 where 80 is the cell height and 45 is the rotation angle*/
  height: 100%;
  transform:skew(-45deg,0deg);
  overflow: hidden;
  border-left: 1px solid #dddddd;
  border-right: 1px solid #dddddd;
  border-top: 1px solid #dddddd;
}

.table-header-rotated th.rotate-45 span {
  transform:skew(45deg,0deg) rotate(315deg);
  position: absolute;
  bottom: 30px; /* 40 cos(45) = 28 with an additional 2px margin*/
  left: -25px; /*Because it looked good, but there is probably a mathematical link here as well*/
  display: inline-block;
  width: 85px; /* 80 / cos(45) - 40 cos (45) = 85 where 80 is the cell height, 40 is the width of the cell, and 45 is the rotation angle*/
  text-align: left;
}

Answer №1

To modify the content of each line, excluding the rows in the "thead," switch the th to td:

View Demo Fiddle

<div class="scrollable-table">
  <table class="table table-striped table-header-rotated">
    <thead>
      <tr>
        <!-- First column header is not rotated -->
        <th></th>
        <!-- Following headers are rotated -->
        <td class="rotate-45"><div><span>Column header 1</span></div></td>
        <td class="rotate-45"><div><span>Column header 2</span></div></td>
        <td class="rotate-45"><div><span>Column header 3</span></div></td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="row-header">Row header 1</td> <!-- needs to be TD not TH-->
        <td>33</td>
        <td>978</td>
        <td>57</td>
      </tr>
      <tr>
        <td class="row-header">Row header 2</td> <!-- needs to be TD not TH -->
        <td>99</td>
        <td>678</td>
        <td>67</td>
      </tr>
      <tr>
        <td class="row-header">Row header 3</td> <!-- needs to be TD not TH -->
        <td>332</td>
        <td>701</td>
        <td>877</td>
      </tr>
    </tbody>
  </table>
</div>

To adjust the alignment of the first cell's content, utilize this rule:

.table-header-rotated th:first-of-type,.table-header-rotated td:first-of-type{
     text-align:left;   
}

View Demo fiddle

Answer №2

Here is a possible solution:

.header-row {
    set maximum width to 30%;
    set minimum width to 10%;
    auto-adjust width;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

There seems to be an issue with .children .val() returning an

I'm currently working on form validation, but encountering an issue where .val() is returning undefined. $(document).ready( function() { $('.formElem .input').each(function() { $(this).children(".inputField").on("keyup", function ...

The dropdown feature does not activate when the checkbox is selected, leading to the dropdown remaining disabled

I'm attempting to make a dropdown menu active when a checkbox is selected. However, when I try to do so... it doesn't seem to be working. Can someone provide assistance with this issue? HTML : <label class="checkbox-inline"> ...

Error encountered with the OffsetWidth in the Jq.carousel program

I am encountering an issue that I cannot seem to figure out. Unexpected error: Cannot read property 'offsetWidth' of undefined To view the code in question, click on this link - http://jsfiddle.net/2EFsd/1/ var $carousel = $(' #carouse& ...

Automated JavaScript function for controlling execution

Currently, I am developing an asp.net c# application that heavily relies on javascript, specifically jquery, for enhancing user experience. I have decided to take a namespaced object-oriented approach to my javascript code and so far, I believe I have suc ...

Sending data from JavaScript to PHP using the POST method

I recently learned that using ajax for data passing doesn't require a form or hidden value. I'm hoping to find an example to better understand how it works. index.js: function collectData(r) { // identifies the row index var i = r.pare ...

What is the best way to display a particular section of a website (<td>...</td>) within a UIWebView?

I have searched high and low for a solution to my issue, but have come up empty-handed. What I am attempting to accomplish is to only load a specific td from a webpage into a UIWebview. Currently, my code looks like this: - (void)viewDidLoad { [supe ...

Run JavaScript code when the HTML file has been modified for the second time

After browsing through this online forum, I stumbled upon a helpful solution. See the code snippet below for reference. However, I encountered a problem in my case. The script in question is essentially monitoring itself. The code resides in index.html an ...

Using jQuery with multiple selectors can become tricky when dealing with elements that may or may not be present

I decided to be more efficient by using multiple selectors instead of rewriting the same code repeatedly. Typically, if one element exists then the others do not. $('form#post, form#edit, form#quickpostform').submit( function() { ...

Prevent clicking on a div using Jquery

Imagine I have a simple click event set up for an HTML div element. When the div is clicked, it should trigger a fadeToggle effect. Inside that div, there is another nested div with its own click event. Upon clicking this inner div, it is supposed to do s ...

Adjust the appearance of a div according to the input value

When a user inputs the correct value (a number) into an input of type "number," I want a button to appear. I attempted var check=document.getElementById("buttonID").value == "1" followed by an if statement, but it seems I made a mistake somewhere. Here&ap ...

`Implementing AJAX data binding in a Flask Python Application`

I am currently laying the groundwork for my first Python Flask web application. While I have the server-side details sorted out, I have some queries regarding the client-side implementation. The application I am working on will be a single-page applicatio ...

Is it possible to hover over the furthest 10 pixels on the left and right of an element

Building upon my inquiry in this previous post, I am seeking guidance on how to change the background color of a div to red and change the cursor to a pointer when hovering over the left 10 pixels of the element. Additionally, how can I alter the backgro ...

Utilizing a series of linked jQuery functions

Is there a more efficient way to write this code snippet? $('#element').html( $('#element').data('test') ); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="el ...

Showing a PHP-generated table with drill-down rows using jQuery Datatables

I have been working on implementing drill-down functionality in a datatable using a PHP file as the data source. However, I am encountering some difficulties that I can't seem to resolve. I have been referring to the guide provided at http://datatable ...

Issue encountered while submitting form on JQuery Mobile framework

Currently, I am in the process of developing a small web application utilizing JQuery Mobile and Multi Page architecture. The issue arises on my form as the second page. Upon clicking the submit button, an error message is displayed on my console (error i ...

Concealing an element upon clicking with jQuery

Can someone help me with this issue? I'm trying to use jQuery to hide the div element with the ID 'professional-panel' when a button with the ID 'hideButton' is clicked. I know it should be simple, but I'm new to jQuery and s ...

The disappearance of the Jquery Datatable following a change in the selected index of a dropdownlist within an ASP.NET

I implemented the jQuery library into my gridview and it proved to be quite useful. Initially, the datatable displayed perfectly on page load. However, upon changing the value of the dropdown list, the jQuery datatable disappeared. In this scenario, the gr ...

How to parse JSON in JavaScript/jQuery while preserving the original order

Below is a json object that I have. var json1 = {"00" : "00", "15" : "15", "30" : "30", "45" : "45"}; I am trying to populate a select element using the above json in the following way. var selElem = $('<select>', {'name' : nam ...

A difference in the way content is displayed on Firefox compared to Chrome, Edge, and Safari

Recently, I encountered an issue with a tool I had developed for generating printable images for Cross-Stitch work. The tool was originally designed to work on Firefox but is now only functioning properly on that browser. The problem at hand is whether th ...

Encountering a problem with scrolling the modal to the top

I am currently working on a code snippet that involves scrolling the modal to the top position. The purpose of this is to display form validation messages within the modal popup. Below are the jQuery code snippets that I have attempted: //Click event t ...