The table will automatically create a new line when there are too many columns

In our dynamic tables, columns are populated with different data for each table, and the number of columns is not predetermined.

If there are more than 4 columns, the table becomes too long. In this case, it should start a new line while maintaining the title of each row.

Refer to the image below for visual clarity:

https://i.sstatic.net/8J5D6.png

Please note: The table does not have a fixed width; its percentage-based width adjusts based on the device's screen size.

All columns share the same height and width that are responsive to the device's dimensions.

When the red block moves to a new line, all rows stack vertically to maintain continuity. Think of the three rows as one cohesive unit when wrapping to a new line.

Answer №1

There are still some bugs that need to be fixed Carefully

To address overflow, you can utilize JS/jQuery to dynamically create a new header element when necessary

Here is an example of how this can be implemented:

// Ensure the function runs on document ready
checktable();
$(window).on('resize', function() {
  checktable();
});


function checktable() {
  if ($(window).width() < $(document).width()) {
    if ($('tr.extended').length < 1) {
      $('tr').not('.extended').each(function(i, e) {
        $(document.createElement('tr'))
          .addClass('extended')
          .append($(e).html())
          .appendTo('#table');
        $(e).children('td').each(function(j, ele) {
          var eot = +$(ele).offset().left +
            +$(ele).width();
          if ($(window).width() < eot) {
            $(ele).hide();
            $('tr.extended').eq(i)
              .children('td').eq(j)
              .show();
          } else {
            $(ele).show();
            $('tr.extended').eq(i)
              .children('td').eq(j)
              .hide();
          }
        });
      });
    }
  } else {
    $('td').not('.extended').show();
    $('.extended').remove();
  }
}
td,
th {
  white-space: nowrap;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="table" border=1>
  <tr>
    <th>
      head
    </th>
    <td>
      so long data 1
    </td>
    <td>
      so long data 2
    </td>
    <td>
      so long data 3
    </td>
    <td>
      so long data 4
    </td>
    <td>
      so long data 5
    </td>
    <td>
      so long data 6
    </td>
  </tr>
  <tr>
    <th>
      head2
    </th>
    <td>
      much long data 1
    </td>
    <td>
      much long data 2
    </td>
    <td>
      much long data 3
    </td>
    <td>
      much long data 4
    </td>
    <td>
      much long data 5
    </td>
  </tr>
</table>

Answer №2

If you're looking to achieve the desired behavior, using display: inline-block; on the table cells is a straightforward option.

For further improvements in usability, I suggest checking out this article on responsive tables from CSS Tricks, especially tailored for better performance on smaller screens.

table {
  max-width: 100%; /* ensuring responsiveness */
}

td {
  display: inline-block;
}
<table border="1">
  <tr>
    <td>aaaaaaaaaaaaaaaaaaaaaaaaa</td>
    <td>bbbbbbbbbbbbbbbbbbbbbbbbb</td>
    <td>ccccccccccccccccccccccccc</td>
    <td>ddddddddddddddddddddddddd</td>
    <td>eeeeeeeeeeeeeeeeeeeeeeeee</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

Verify the presence of plain ASCII text

I need to verify if the file uploaded is in ascii plain text format. Can you suggest a method? $("#my_file").change(function(){ //alert if file is not ascii plain text }); <input type="file" name="my_file" id="my_file" /> ...

Bring in styles from the API within Angular

My goal is to retrieve styles from an API and dynamically render components based on those styles. import { Component } from '@angular/core'; import { StyleService } from "./style.service"; import { Style } from "./models/style"; @Component({ ...

Creating a personalized notification box that is compatible with various screen sizes and web browsers on android devices with the help of

After successfully implementing a custom alert box to hide the header with JavaScript, I encountered an issue when trying to use it on different browsers and Android devices. The alert box appeared misplaced on the page and was too big for certain Android ...

Tips for repairing a side bar and header with CSS when using a JS & jQuery scroller

I am working on a layout design and my goal is to have the left sidebar, right sidebar, and header remain fixed in place. Here is the CSS code I am using : #container { padding-left: 200px; /* Left Sidebar fullwidth */ padding-ri ...

The detailView feature in wenzhixin bootstrap-table is failing to initialize the child row

I am currently utilizing the wenzhixin bootstrap table and attempting to initialize another bootstrap table within a child row known as 'detailView' in the code. Previously, I was using datatables but found this method to be simpler and more dir ...

HTML5 input type Color displays individual RGB values

This code snippet opens up a variety of options for the user to manipulate different color values such as R, G, B, HEX VALUE, HUE, etc. However, the specific requirement is to only extract the Red value. <input id="color_pick"type="color" value="#ff0 ...

embedding an Image onto a webpage without requiring a source

How can I incorporate an image into my HTML page and then send it via fax without using the "src" attribute? Any suggestions on converting the image to a byte array and embedding it in the html document? ...

What could be causing the incorrect display of updates when my Grails checkbox onclick remote function Ajax call is triggered

Hi, I have a page named taskReminder that renders two templates: one for tasks and another for reminders. The task template includes a checkbox that, when checked, should update the task list. Everything is functioning correctly, but there seems to be an i ...

Performance Issues Arise with Rapid Clicking [jQuery]

Having trouble with a gallery script I created that includes thumbnails, a large image, and navigation arrows. When you rapidly click on thumbnails or arrows during the transition, it causes delays in the process. The more clicks you make, the more noticea ...

Adjusting CSS to realign images on various screen types

I am facing an issue with displaying a background image on a static web page. The height appears differently on various LCD screens. How can I ensure that the height adjusts to approximately 80% of the area? I have tried giving a percentage in the style fo ...

Ensure that the images in the final row of the flexbox gallery are consistent in size with the rest

I'm working on creating a CSS flex image gallery that adjusts the width of the images based on the container size: HTML: <div class="grid-container"> <div class="grid-item"> <img src="small-thumb-dpi.jpg" /> < ...

Exploring the challenges of applying styles to buttons using the :first-child and :last-child selectors, especially when

I successfully applied rounded corners to the first and last elements of a styled button using CSS. Here is how it looks: https://i.sstatic.net/A62Kl.png The issue arises when I dynamically insert new buttons using Angular's ng-if. The new buttons ...

What is the best way to ensure all my borders are uniform in size?

As I work on a JavaScript project that organizes words into blocks based on their size, I encountered an issue with inconsistent border thickness in certain areas. I'm using spans to contain each word, but I can't figure out how to make the borde ...

I'm noticing my table collapsing as I move around the div elements - any idea why this is happening

A challenge I am facing is creating a dynamic table where users can drag and drop colored boxes with animated transitions. While it mostly works well, sometimes the table collapses inexplicably. Here are steps to reproduce the issue: Move 100 - 400 Move 1 ...

Iterating over a map object using ngFor

I'm having some trouble grasping the concept of how ngFor interacts with map. Here is the HTML code I am working with: <div *ngFor="let country of wars.getCountrys()"> And this is the TypeScript code: wars: Map < number, Array < count ...

Issues with jQuery Ajax sending Post data to PHP script

I've been attempting to transfer variables from my JavaScript to a PHP file using AJAX, but for some reason, it's not functioning as expected. Despite researching numerous similar queries, I have yet to come across a solution. Here is an excerpt ...

Unable to wrap the strings from the database in a span element

I have a challenge where I need to enclose the first and last strings that represent the title and price of a product within a div using a span tag. These strings are dynamic, sourced from a database, and differ for each product. I have attempted to use th ...

Jquery starts functioning properly after the second click and continues to work flawlessly

Despite being a functional script, it only starts working on the second click instead of the first as intended. function change_text() { var button = document.getElementById('toggle_button'); if (button.innerHTML === "Edit") { button.inn ...

Styling your Kendo grid in ASP.NET MVC with custom CSS styling

I am facing a challenge with displaying two kendo grids side by side on a single view page. To modify the CSS style of a kendo grid using ASP.NET, I typically use: .HtmlAttributes(new { style="width:50%"}) However, when attempting to change more than one ...

The utilization of `ngSwitch` in Angular for managing and displaying

I am brand new to Angular and I'm attempting to implement Form Validation within a SwitchCase scenario. In the SwitchCase 0, there is a form that I want to submit while simultaneously transitioning the view to SwitchCase 1. The Form Validation is fun ...