Creating a table structure using div elements

Creating a table with both vertical and horizontal scrolling, along with a fixed header and an auto width for the table is proving to be quite challenging. As a workaround, I'm experimenting with using div elements instead. Below is the code snippet I've come up with: Check out my code here

body {
  background: #20262E;
  padding: 20px;
  color:#fff;
  font-family: Helvetica;
}

.tableContainer{
  border:1px solid #fff;
  
}
.tableheader{
  font-weight:bold;
  border-bottom:1px solid #fff;
  position:relative;
  
}
.tableBody{
  max-height:150px;
  overflow-y:auto;
  padding-top:63px;
}
.tableheader .tablerow{
  display:table;
  background: red;
    width: 100%;
    position:absolute;
    
}
.tableheader .tablerow > div{
  padding:5px;
  display:table-cell;
  border-right:1px solid #fff;
    
}
.tableBody .tablerow{
  display:table;
}
.tableBody .tablerow > div{
  padding:5px;
  border-right:1px solid #fff;
  display:table-cell;
}
<div class="tableContainer">
  <div class="tableheader">
     <div class="tablerow">
       <div>
       Lorem ipsum dolor sit amet
       </div>
       <div>
       Suspendisse ipsum diam, sollicitudin a tincidunt ac
       </div>
       <div>
       Lorem ipsum
       </div>
       <div>
       Lorem ipsum dolor sit amet
       </div>
       <div>
       Suspendisse ipsum diam, sollicitudin a tincidunt ac
       </div>
       <div>
       Lorem ipsum
       </div>
     </div>
  </div>
  <div class="tableBody">
     <div class="tablerow">
          <div>
       Lorem ipsum dolor sit amet
       </div>
       <div>
       Suspendisse ipsum diam, sollicitudin a tincidunt ac
       </div>

.tableContainer{ border:1px solid #fff;

}
.tableheader{
  font-weight:bold;
  border-bottom:1px solid #fff;
  position:relative;

}
.tableBody{
  max-height:150px;
  overflow-y:auto;
  padding-top:63px;
}
.tableheader .tablerow{
  display:table;
  background: red;
    width: 100%;
    position:absolute;

}
.tableheader .tablerow > div{
  padding:5px;
  display:table-cell;
  border-right:1px solid #fff;

}
.tableBody .tablerow{
  display:table;
}
.tableBody .tablerow > div{
  padding:5px;
  border-right:1px solid #fff;
  display:table-cell;
}

I'm currently struggling with achieving horizontal scrolling and managing column widths.

Alternatively, you can view my other code here. This code also presents challenges with horizontal scrolling.

Your assistance in resolving this issue would be highly appreciated.

Answer №1

.tableheader should have a margin-right of 17px;

17px represents the width of the scrollbar

Answer №2

If you're looking for the best way to create a fully responsive table, consider using a structure like this:

/*
Max width before this PARTICULAR table gets nasty. This query will take effect for any screen smaller than 760px and also iPads specifically.
*/
  @media only screen and (min-width:1024px){
    tr:nth-child(odd) {
      background: #ccc;
    }
  }    
@media
  only screen 
    and (max-width: 760px), (min-device-width: 768px) 
    and (max-device-width: 1024px)  {

/* Force table to not be like tables anymore */
table, thead, tbody, th, td, tr {
display: block;
}

/* Hide table headers (but not display: none;, for accessibility) */
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}

    tr {
      margin: 0 0 1rem 0;
    }
      
    tr:nth-child(odd) {
      background: #ccc;
    }
    
td {
/* Behave  like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}

td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 0;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
}

/*
Label the data
    You could also use a data-* attribute and content for this. That way "bloats" the HTML, this way means you need to keep HTML and CSS in sync. Lea Verou has a clever way to handle with text-shadow.
*/
td:nth-of-type(1):before { content: "First Name"; }
td:nth-of-type(2):before { content: "Last Name"; }
td:nth-of-type(3):before { content: "Job Title"; }
td:nth-of-type(4):before { content: "Favorite Color"; }
td:nth-of-type(5):before { content: "Wars of Trek?"; }
td:nth-of-type(6):before { content: "Secret Alias"; }
td:nth-of-type(7):before { content: "Date of Birth"; }
td:nth-of-type(8):before { content: "Dream Vacation City"; }
td:nth-of-type(9):before { content: "GPA"; }
td:nth-of-type(10):before { content: "Arbitrary Data"; }
}
<table role="table">
  <thead role="rowgroup">
    <tr role="row">
      <th role="columnheader">First Name</th>
      <th role="columnheader">Last Name</th>
      <th role="columnheader">Job Title</th>
      <th role="columnheader">Favorite Color</th>
      <th role="columnheader">Wars or Trek?</th>
      <th role="columnheader">Secret Alias</th>
      <th role="columnheader">Date of Birth</th>
      <th role="columnheader">Dream Vacation City</th>
      <th role="columnheader">GPA</th>
      <th role="columnheader">Arbitrary Data</th>
    </tr>
  </thead>
  <tbody role="rowgroup">
    <tr role="row">
      <td role="cell">James</td>
      <td role="cell">Matman</td>
      <td role="cell">Chief Sandwich Eater</td>
      <td role="cell">Lettuce Green</td>
      <td role="cell">Trek</td>
      <td role="cell">Digby Green</td>
      <td role="cell">January 13, 1979</td>
      <td role="cell">Gotham City</td>
      <td role="cell">3.1</td>
      <td role="cell">RBX-12</td>
    </tr>
    <tr role="row">
      <td role="cell">The</td>
      <td role="cell">Tick</td>
      <td role="cell">Crimefighter Sorta</td>
      <td role="cell">Blue</td>
      <td role="cell">Wars</td>
      <td role="cell">John Smith</td>
      <td role="cell">July 19, 1968</td>
      <td role="cell">Athens</td>
      <td role="cell">N/A</td>
      <td role="cell">Edlund, Ben (July 1996).</td>
    </tr>
...More rows here...
  </tbody>
</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

The headline box is displaying CSS code instead of the content. How can this issue be resolved?

Having an issue with a WordPress template that features a blue "headline" box that I need to change the color of to #333399. I tried inspecting the element, browsing through the code, and identified the code below as what needed modification: #headline, # ...

Exploring the world of web scraping using Scrapy, Python, and a JavaScript-based website all in one

Hello, I am attempting to scrape the website using the following script: script.py import scrapy from scrapy.crawler import CrawlerProcess from threading import Thread class CourtSpider(scrapy.Spider): name = 'full_page' allowed_domai ...

The issue with line-height causing the sliding URL effect to malfunction

Having a sliding underline URL effect (viewable here: http://codepen.io/Dingerzat/pen/XNgKmR), I encountered an issue when combining it with a resizing header. The resizing header caused the sliding underline effect to stop working. After some troubleshoot ...

How can I display a loading message in bootbox during the execution of an ajax process?

In my application, there is a page that displays status in a drop-down menu with two options - Active and Inactive. Users can change the status by simply clicking on the menu and selecting an option. To track this status change process, I am using JQuery/A ...

The .ogv video file type does not seem to be functioning properly within the HTML5 video tag on Firefox

Hello, I'm encountering some issues with playing the .ogv file in Firefox. If you could take a look at the link , that would be greatly appreciated. I believe my HTML5 video tag syntax is correct as it's working fine on other browsers. Thank you ...

Unable to manipulate dynamically added content using .load method

Currently, I am facing an issue with loading content via AJAX and displaying the HTML, which contains div elements, inside a parent ul. I have successfully written several functions using .click and .hover that work flawlessly on all elements except for t ...

What is the best way to prevent centered elements from overflowing the screen using only CSS?

I have noticed that in my code, when I add a lot of elements, some of the content overflows off the screen. It's acceptable if the content goes off the bottom of the screen as it is still accessible. However, I want to prevent it from going off the to ...

The vertical scrolling feature will come to a halt once it reaches a specific limit

Check out this website: How can I prevent the vertical scroll from continuing past the orange footer boundary? ...

Searching for the sequence "http" using regular expressions in Python

I am trying to extract text entries that match a specific pattern within a long text. The pattern I am looking for is: http******.id.txt, where * can be any entry of unknown length and the dots represent literal dots in the text. I want to compile a list o ...

Is there a way to modify the font of all dropdown items at once? How can we ensure that the dropdown content appears on a single line without any truncation

I am looking for some assistance with customizing my dropdown menu on the website Here are the modifications I have attempted, along with the corresponding code snippets. However, none of them seem to be effective... I tried increasing the font size for ...

The Angular (click) event requires two clicks in order to trigger the associated Typescript function

I'm facing an issue with a Typescript function that I've linked to (click) in my HTML. Oddly, I have to click the button twice for the function to be executed. Interestingly, if I don't provide any parameters, the function works as expected ...

Issues with PHP Form Email DeliveryIt seems that there is a

I currently have a ready-made form that is constructed using html, css, jquery, and ajax. It's basically copied and pasted onto my contact page for now. This is the code I include before the html tag on the contact.php page. <?php session_name(" ...

What are some solutions for resolving a flashing responsive table issue that occurs when loading it via ajax?

My HTML table is responsive, adapting to different screen sizes. I rely on Ajax to load the table dynamically as the data changes frequently. However, a problem arises when the table switches into responsive mode for smaller screens or mobile devices. Ever ...

To maintain the width values (maximum width) when using overflow:auto, do not make any adjustments. (CSS for scrollbars

My English isn't the best and I'm just starting out with CSS. The scroll bar is taking up space and I want to prevent that from happening. I have noticed that when the scroll bar appears, it causes the fixed navigation menu bar to shift and the ...

Revise website design to adjust dynamically according to screen dimensions

Currently, I am working on a school project with a website created by former students. Unfortunately, I have limited knowledge of HTML and CSS which has made it challenging for me to ensure the site is mobile-friendly. There are issues such as text overflo ...

Utilize a standard JavaScript function from a separate document within a function of a React component

I am facing an issue where I need to incorporate a standard JavaScript function within a React component function. The script tags are arranged in the footer in the following order: <script src="NORMAL JAVASCRIPT STUFF"></script> // function ...

Troubleshooting problems with opening and closing windows in JavaScript

I'm currently facing an issue with managing browser windows using JavaScript. In my proof of concept application, I have two pages - one for login information (username, password, login button, etc.) and the second page is a management screen. What I ...

The hamburger menu remains static and unresponsive on mobile screens, failing to expand as intended

I am currently facing an issue with my Bootstrap navbar in Google Chrome. When I reduce the size of the browser, the navbar menus collapse into a hamburger icon but clicking on it does not reveal the menus. The problem seems to be occurring in my app.comp ...

Creating your own browser extension similar to Firebug: A comprehensive guide

As a beginner dotnet developer, I am looking to create an Internet Explorer addon similar to Firebug in Firefox. Specifically, I need the HTML and CSS features of Firebug without all the extras. I'm not sure where to start or which platform to choose. ...

Is it possible to execute AJAX within a popup feature?

I am attempting to execute an ajax call within a popup function. In simple terms, I have a basic JavaScript function where I need to make an ajax request. Here is the implementation of my popup function: if(window.opener && !window.opener.closed ...