Transform Table header orientation to vertical

I am struggling with a table structure similar to this

<table>
    <thead>
        <tr>
            <th>Numbers</th>
            <th>Alphabet</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>a</td>
        </tr>
        <tr>
            <td>2</td>
            <td>b</td>
        </tr>
    </tbody>
</table>

By default, the table displays Number 1 2(top to bottom) and the corresponding alphabet values in another column as Alphabet a b

However, I am trying to modify the display by making the <thead> elements appear vertically. Similarly, I want each individual value within the <tbody> <tr> tags to be displayed vertically so that Number 1 2 are on one horizontal line and Alphabet a b on another.

To achieve this, I have utilized the following CSS:

thead {
    float: left;   
}

thead th {
    display: block;   
}

tbody {
    float: right;   
}

While the thead appears vertical after applying the CSS, unfortunately, the content within the tbody tr remains unchanged.

If anyone has a solution or suggestion on how to make it work effectively, I would greatly appreciate it. THANKS

Answer №1

It's unclear why you're hesitant to alter the table layout, but everyone has their reasons. Regardless, this task proved to be quite challenging :)

Below is the provided CSS code:

table{
     display:block;
     padding: 0px;
     border-collapse:collapse;
     border-spacing:0;
     margin-top: 0px;
     margin-right: auto;
     margin-bottom: 0px;
     margin-left: auto;
}
thead{
    display: block;
    float: left;
    margin: 0px;
    padding: 0px;
    width: 100px;
}
tbody{
    margin: 0px;
    padding: 0px;
}
tbody tr {
    float:left;
}
th, td{
   display:block;
   padding: 5px;
   margin: 0px;
}
thead > tr th:nth-child(odd) { 
       display:block; 
       float:left;
}
thead > tr th:nth-child(even) { 
   display:block;
   float:left; 
}
tbody > tr td:nth-child(odd) {
   display:block;
}
tbody > tr td:nth-child(even) { 
   display:block; 
   float:right;
}

Additionally, here's a demo showcasing your HTML table structure: http://jsfiddle.net/darkosss/83kVc/

I hope this information proves useful for you.

Answer №2

Have you considered using a static structure like this for your table if it won't be changing dynamically?


  <table>
     <tr>
        <th>Items</th>
        <td>Item 1</td>
        <td>Item 2</td>
     </tr>
     <tr>
        <th>Category</th>
        <td>Category A</td>
        <td>Category B</td>
     </tr>
  </table>

Answer №3

In this scenario, it is advised not to utilize thead and tbody. Instead, follow this structure:

<table>
  <tr>
    <th>Numbers</th>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <th>Alphabet</th>
    <td>a</td>
    <td>b</td>
  </tr>
</table>

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

Is the PHP configuration correct for Apache on your local server?

I'm currently facing an issue with connecting to a MySQL server in order to retrieve data for a flash application using PHP. I created a test .php file to check if everything is working correctly: <html> <body> <?php $link = mysql_con ...

What steps can be taken to ensure that the popover div remains visible when clicking inside it in a "dismissible popover" using Twitter Bootstrap with the data-trigger attribute set to "

I am struggling with a dismissible popover that contains a text box. When I click inside the text box to type, it disappears due to the "data-trigger="focus". Is there a way for the div not to disappear when clicked inside intelligently? Here is the releva ...

Selenium mistakenly chooses the incorrect element by selecting the first sibling element instead of searching within the element itself

I've been trying to loop through a list of elements and display the text, but I've encountered a strange issue with Selenium. When I select an element inside another element, Selenium returns the element inside the first sibling element instead o ...

Tips for making your inner content as wide as possible

I'm currently working on developing collapsible tables, where an outer and inner table are displayed for every row that is clicked. This is the code I have implemented: HTML: <table class="table outerTbl mb-0"> <thead> <t ...

What is the best way to ensure the snackbar occupies the full width on smaller screens?

After reviewing this commit: https://github.com/callemall/material-ui/commit/11695dcfa01e802797115d42c6d3d82d7657b6ab#diff-e9014062cd8e3b4344ab619966f35ef2 In the latest update, the snackbar no longer expands to 100% width on mobile screens. Can someone p ...

Display or conceal a div element depending on the user's choice

I am looking to hide inactive divs, but whenever I reload the page, all tab contents merge into one. The screenshot below shows the issue I'm facing. Screenshot Below is the code snippet: $('.tab a').on('click', function ...

Unusual sidebar scrolling issues experienced in Firefox

Currently, I am in the process of developing an app with a scrolling sidebar. However, there seems to be some unexpected behavior when using Firefox: the content within the sidebar disappears partially below the top of the div if the mouse is positioned lo ...

Setting a CSS Variable with the Help of jQuery

I need help with changing the background color of a specific .div element when it is clicked on. I want the color to be a variable that can be changed by the user, and this change should occur when the document is ready. Currently, I am using a CSS variab ...

Identifying periods of inactivity using an embedded iframe

I have developed a website that showcases a three.js model within an iframe. My goal is to redirect users back to the homepage (index.html) after they have been inactive for a specified amount of time. While I have managed to achieve this using JavaScript, ...

"Trouble with making jQuery AJAX calls on Internet Explorer versions 8 and 9

I've been searching for the answer to this problem without any luck. I have a page with jquery ajax calls to an API service. It works well in Chrome, Safari, Firefox, and IE 10, but fails in IE 9 and 8. Here is the code: $.ajax({ ...

Generate a collection of div elements as child nodes

I'm working on a project where I have multiple rows with input fields. My goal is to create an array for each row that contains the values of the inputs. For example, if I have 3 rows and the values of 'input1' are 1, 'input2' are ...

What is the best way to integrate LESS css into a Reactjs application?

I am looking to incorporate LESS into my React app, but I have been unsuccessful in finding a solution through Google. As a newcomer to ReactJs, I lack deep knowledge and would greatly appreciate guidance on installing Less. If anyone is able to help me ...

A guide on automating the html5 color picker using input type="color" in selenium webdriver

When interacting with an HTML color input element, a color picker window pops up that prevents viewing the DOM. I am looking for a solution to either select a color and close the window or enter a hex code in the popup's text box. Can anyone provide m ...

Tips for utilizing the if-else directive in an AngularJS controller

My current challenge involves HTML coding. <li><strong>Scrub:</strong> {{insuredProfile.permanentAddress.isScrubbed}}</li> This code displays either true or false. I want to show Yes for true and No for false. Using data-ng-if, ...

The alignment of the Unicode character is off and is not centered within the button

Upon implementing a unicode character as a button, I've observed that the alignment of the character is not centered properly (both horizontally and vertically) within the button. It's puzzling why this misalignment occurs even after setting padd ...

What could be causing the data toggle feature in my navbar to not work on Bootstrap?

Here is a code snippet to create a simple collapsible navbar with Bootstrap 5 that includes a logout button. However, the data toggle feature for the logout button doesn't seem to work when clicked. Any suggestions on how to fix this? <!DOCTYPE ...

When an HTML table utilizes both rowspan and colspan attributes, it may encounter issues with being overlapped by the

I'm working on a straightforward table that showcases the properties of a data element. Each row will represent an element from the AJAX response in a list format. The table is designed with "rowspan" and "colspan" to expand specific cells. However, ...

The div background image in Vue.js is displaying a 404 not found error

I've encountered an issue while trying to set a background for a div. Strangely, when using the same relative path with an img tag everything works fine, but as soon as I try to achieve the same result with a div, I receive a 404 error. This img tag ...

A step-by-step guide on submitting a CSV file with Ajax along with additional form information

Imagine you have an HTML form with input fields for Name, Location, Address, and Family members (to be uploaded as a CSV file), along with a Submit button. However, when you click on submit, the data from the CSV file is not being passed to the PHP script. ...

The issue of the color CSS rule not being applied to the button element when transitioning from disabled to enabled on browsers like Safari 14+ is a common challenge

There seems to be an issue with frontend form validation code affecting the disabled attribute of a submit button within a form. Two different css rules are applied for the enabled and disabled states, including changes in the text color of the button ele ...