`Addressing issues with table cell layout for improved horizontal scrolling`

I am currently facing a challenge involving fixing the first and last column (.headcol, .lastcol) in a horizontally scrolling table, ensuring they always align to the left & right of the visible panel.

My understanding was that position: absolute elements would be positioned relative to the first parent element with position: relative. However, in this case, they seem to position themselves relative to the scrollable element (one div up).

If you scroll right in this fiddle: http://jsfiddle.net/benkeen/hav6eLst/, you will notice the issue.

My goal is to keep the white first column and the red last column in place while only scrolling the yellow content in between.

Below is the code snippet:

#top {
  width: calc(100% - 200px);
  margin-left: 100px;
  margin-right: 100px;
  overflow-x: scroll;
  overflow-y: visible;
}

#pos-relative {
  position: relative;
}

table {
  border-collapse: separate;
  margin-left: 100px;
}

td,
th {
  margin: 0;
  border: 3px solid grey;
  border-top-width: 0px;
  white-space: nowrap;
}

.headcol {
  position: absolute;
  width: 100px;
  left: 0;
  border-right: 0px none black;
  border-top-width: 3px;
  margin-top: -3px;
  background-color: white;
}

.headcol:before {
  content: 'Row ';
}

.lastcol {
  position: absolute;
  right: 0;
  width: 100px;
  background-color: red;
}

.long {
  background: yellow;
  letter-spacing: 1em;
}
<div id="top">
  <div id="pos-relative">
    <table>
      <tr>
        <th class="headcol">1</th>
        <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
        <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
        <th class="lastcol">x</th>
      </tr>
      <tr>
        <th class="headcol">2</th>
        <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
        <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
        <th class="lastcol">x</th>
      </tr>
      ... more rows
    </table>
  </div>
</div>

Could it be that I am approaching this problem from the wrong angle? Maybe this is not achievable with tables.

Answer №1

Sorry for any confusion caused. It turns out, all I needed to do was rearrange the top-level elements to ensure that the scrollable element was within the position: relative element. This fixed the issue with the horizontal alignment of the white and red cells while retaining normal vertical scrolling functionality.

For reference, you can view an example here: http://jsfiddle.net/benkeen/4z0x297L/2/

#pos-relative {
  position: relative;
  margin-left: 100px;
  margin-right: 100px;
}

#top {
  width: 100%;
  overflow-x: scroll;
  overflow-y: visible;
}

table {
  border-collapse: separate;
  margin-left: 100px;
}

td,
th {
  margin: 0;
  border: 3px solid grey;
  border-top-width: 0px;
  white-space: nowrap;
}

.headcol {
  position: absolute;
  width: 100px;
  left: 0;
  border-right: 0px none black;
  border-top-width: 3px;
  margin-top: -3px;
  background-color: white;
}

.headcol:before {
  content: 'Row ';
}

.lastcol {
  position: absolute;
  right: 0;
  width: 100px;
  background-color: red;
}

.long {
  background: yellow;
  letter-spacing: 1em;
}
<div id="pos-relative">
  <div id="top">
    <table>
      <tr>
        <th class="headcol">1</th>
        <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
        <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
        <th class="lastcol">x</th>
      </tr>
      ... additional rows 
    </table>
  </div>
</div>

Answer №2

It seems like you are looking to have the first column remain fixed while allowing the others to be scrollable either horizontally or vertically. To achieve this, you will need to incorporate some JavaScript code that aligns with your desired functionality. There are numerous examples available online that can guide you in implementing this feature. Happy coding! ^_^

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

`Considering various factors to alter class pairings`

I have defined a few style classes in my stylesheet as shown below: .left-align{ /* some styles here */ } .right-align{ /* some styles here */ } .validate-error{ /* some styles here */ } Depending on the type of data, I need to align the content ei ...

I have just started a new project in Vue and noticed that the app div has some extra margin around it. Can anyone

In my fresh Vue project, I have the following code: App.vue: <template> <div id="app"> <p>hello</p> </div> </template> <script> export default { name: 'App', components: { } ...

The anticipated data originates from the 'style' attribute, which is formally noted within the 'IntrinsicAttributes & TextInputProps & RefAttributes<TextInput>' type

I have been working on developing a text form using typescript within the React Native framework. To accomplish this, I created a TextInput component specifically designed for email and password inputs. Below is the code I have been working with: TextInpu ...

Guide to positioning elements in CSS

I'm struggling to get all the elements to align in a single row. I've tried using display: inline-block, but it's not working as expected. I'm working with DataTables and I want the button images to be aligned with the page number box. ...

Unable to prevent ordered lists from overlapping with other content I attempt to place beneath them in HTML

function filterImages() { let input = document.getElementById('searchbar').value; input = input.toLowerCase(); let images = document.getElementsByClassName('gallery'); for (let i = 0; i < images.length; i++) { ...

Tailored alignment of checkboxes and labels

I've designed a custom checkbox where the actual checkbox is hidden and a label is styled to look like a checkbox. Everything works fine, however, when I add a label, it doesn't align properly. Here's the link to the fiddle: http://jsfiddle ...

The Bootstrap v5 dropdown feature is malfunctioning as the drop-down menu fails to appear

I've been struggling to create a dropdown menu using Bootstrap, but it doesn't seem to be working as expected. Despite that, I have no issues utilizing the framework for styling purposes. The Bootstrap js link is placed at the bottom of the body ...

Display or conceal a division underneath a dropdown menu based on selections retrieved from a SQL Server database

Presented here is my JavaScript code. function appendItemforPurchaseOrder() { debugger var rowNumber = parseInt($(".itemmapContainer").attr("data-rownumber")); rowNumber = isNaN(rowNumber) ? 1 : rowNumber + 1; var addNewItemDetailHtml = ...

Choose items that do not contain ::before or ::after pseudo-elements

I am looking to apply a specific font style to all text on a page except for Font Icons (specifically Font Awesome) which do not share a common class. In order to achieve this, I need to target elements in one of the following ways: Elements that do not ...

Dynamic Display Picture Banner - Adjustable Layout

My home page features a full screen image header that I'm working to make responsive. The width of the image is being cut off on various screen sizes. I've attempted adjusting the height and width settings, but I want the image itself to resize ...

Various CSS libraries offer a range of design options, including DataTables and Bootstrap

Incorporating both Bootstrap and DataTable into my design has caused conflicts with CSS styles. This issue extends beyond just these libraries, making me wonder if there is a way to prioritize my own styles over library styles. Can anyone provide guidanc ...

Issue with Bootstrap 4 column width when using Angular 2 *ngFor

I have a container that displays search results. The layout is set up using Bootstrap columns, but there seems to be excessive padding or margin causing the results to appear very narrow. Interestingly, if I manually input each result instead of using *ngF ...

Tips for managing the transparency level of a container's backdrop using CSS?

Is there a way to adjust the transparency of an image, change the background color of a container, button, or form using CSS? Which specific property would be appropriate for this task? ...

How can the spacing between Angular Material's mat-card-content and mat-card-actions be adjusted?

I am creating a mat card layout where there are two separate cards within the main card's content section. However, when I add a button for the actions section of the main card, there is no spacing between these two sections. How can I create a vertic ...

What is the best way to apply styling to an image that is contained within the document.write function?

I'm looking to customize the design of this cat image, but I'm struggling to locate where to incorporate the div class. It's likely a basic step, but as a beginner in JavaScript, I'm hoping that someone can assist me. document.write(&ap ...

Tips for resizing a 100% div without displaying vertical scrollbars in the browser

I am facing an issue with a table that has three rows. I have a main #container div with a height of 100%, and inside it is a table with 3 rows. The first and last row contain fixed size content. However, in the second row, there is a #content div with a h ...

Create two grid components for Material-UI and use Flexbox to ensure that both items have equal height

I'm currently working on ensuring that two grid items have the same height using flex in Material UI, but I've hit a roadblock. It seems like my approach with the Grid element might be incorrect. I've experimented with adjusting the height a ...

The presence of a Bootstrap addon is resulting in horizontal scrolling on mobile devices within the webpage

I am encountering a peculiar issue with an input-group in my cshtml file which features a Bootstrap addon. The problem arises on mobile devices, where upon focusing on the input field, the page scrolls horizontally to the right, revealing the right margin ...

Retrieve CSS height using Jquery, based on the declared value rather than the computed value

In a previous version of jQuery, the default behavior was different. When I use .css("height") and .height(), it gives me the computed height instead of what I actually want. I only need the height value if it is explicitly declared in the CSS for that ele ...

The jQuery click event to change the color function is functioning properly, but there seems to be an issue with

I'm having trouble changing the font-family using the code below, even though the color is changing properly. What am I missing here and how can I correct it? $(document).ready(function(){ $("body").on("click",".selectableColor", function(){ ...