Sticky positioning with varying column widths

How can I create HTML and CSS columns that stick together without manually specifying the "left:" parameter every time?

The current example in the fiddle achieves what I want, but requires manual setting of the "left:" value. Is there a way to make this more dynamic using JavaScript or CSS for varying column widths?

td,
th {
 position: sticky;
 top: 0;
 align-self: flex-start;
 left: 0;
 background-color: #fff;
 z-index:100;
}
.ddd {
 position: sticky;
 top: 0;
 align-self: flex-start;
 left: 0;
 background-color: #fff;
 z-index:101;
}
.dddd {
 position: sticky;
 top: 0;
 align-self: flex-start;
 left: 90px;
 background-color: #fff;
 z-index:101;
}
.ddddd {
 position: sticky;
 top: 0;
 align-self: flex-start;
 left: 190px;
 background-color: #fff;
 z-index:101;
}

<table style="width:2000px" border="1">
<tr>
<th style="width:90px" class="sticky-col">Firstname</th>
<th style="width:100px" class="sticky-to-left-elem">Lastname</th>
<th style="width:140px">Age</th>
<td style="width:40px">Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<!-- Additional table rows removed for brevity -->

Visit the JSFiddle here

Answer №1

Utilize the power of flexbox.

.container {
  display: flex;
  flex-direction: row;
  margin-bottom: 1rem;
}

.container.full-width .item {
  flex: 1 1 auto;
}

.container.auto-width .item {
  flex: 0 1 auto;
}

.item {
  color: white;
}

.item-1 {
  background: red;
}

.item-2 {
  background: purple;
}

.item-3 {
  background: blue;
}
<div class="container full-width">
  <div class="item item-1">
    row 1 with very loooooooooong text
  </div>
  <div class="item item-2">
    row 2 with short text
  </div>
  <div class="item item-3">
    row 3
  </div>
</div>

<div class="container auto-width">
  <div class="item item-1">
    row 1 with very loooooooooong text
  </div>
  <div class="item item-2">
    row 2 with short text
  </div>
  <div class="item item-3">
    row 3
  </div>
</div>

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

Looking to verify the validity of my email and phone number

Looking for some assistance in validating my email and telephone inputs for the contact form. The current code for email validation is incorrect, so I need help fixing it. It should be something like this: if(email.length == 0 || email.indexOf('@&apo ...

Implementing external JavaScript files such as Bootstrap and jQuery into a ReactJS application

Just diving into ReactJs, I've got static files like bootstrap.min.js, jquery.min.js, and more in my assets folder. Trying to incorporate them into my ReactJs App but running into issues. Added the code below to my index.html file, however it's ...

The table spills over the container and disappears underneath the navbar

After downloading a template table from the internet and customizing it to my liking, I encountered an issue where it does not adhere to the assigned settings (as shown in the image), but that's not the only problem. The dropdown navbar on my website ...

Is there a way to ensure that GIFs in jQuery Mobile always start from the beginning?

My cross-platform mobile app built with jQuery Mobile is a small quiz application. After each correct or wrong answer, I display a 3-second GIF. Currently, I have set up the code to show the GIF for 3 seconds before moving on to the next page: else if ($. ...

Tips for Dynamically Binding Data in Angular ChartsWant to learn how to dynamically bind

I have integrated angular-charts directives into my application and everything works perfectly when initializing the data. However, I encountered an issue when reading the data from a JSON file and assigning it to the chart. The x-axis and y-axis are gener ...

Using Special Fonts in Visual Studio 2013

Encountering a small issue with Visual Studio 2013. My application, developed using html and javascript, utilizes a custom font. When attempting to run the application on a different computer that does not have the font installed, it fails to display corr ...

How can I add page transitions to my simple HTML website?

Working on an internal website project for my office using vue.js has been a rewarding experience. I appreciate how easily it allows me to manage multiple pages with dynamic content and seamless transitions. Unfortunately, my IT department is hesitating to ...

Div's external dimension not being computed

I am attempting to calculate the overall height of the div content in order to expand the sticky-container and create the appearance that the image is tracking the user. Unfortunately, using outerHeight does not seem to be effective for some reason. While ...

Angular - Strategies for Handling Observables within a Component

I am new to Angular and recently started learning how to manage state centrally using ngRx. However, I am facing a challenge as I have never used an Observable before. In my code, I have a cart that holds an array of objects. My goal is to iterate over the ...

I'm currently facing an issue with toggling the visibility of a div using JavaScript

In my attempt to create a unique custom select menu using html, css, and javascript, I encountered an issue with toggling the display style of the div containing the options when clicking on the button (in this case, an arrow). HTML: <ul class="de ...

When it comes to React, an unspoken truth is that undefined could potentially cause issues

I have been attempting to iterate through an array of data, following a guide without much success. The structure of the data file is as follows: import React, {Component} from 'react'; export default [ { id: 1, lk:593458, ld:18033, status: &ap ...

Is there a way to make sure a user can't access the response from an AJAX request?

I'm eager to establish a secure AJAX call and response between the client and server. To enhance security, I encrypt my AJAX request using an encryption method and route it like so: url: "/site/web/J+CVKhtwFK9VwSZYiza8zr8YUqWK62VSkobVfgB3+1s=" This ...

A unique feature where a bar emerges from the bottom of the screen, smoothly glides upwards, and then securely fastens to

I'm working on bringing to life a concept that I've been envisioning. The design would feature a long scrolling page with a unique navigation bar behavior. The idea is for the navigation bar to initially start at the bottom of the screen and the ...

Adjusting the Connection header in a jQuery ajax request

I've been attempting to modify the Connection header using the code below, but so far, I haven't had any success jQuery.ajax({ url: URL, async: boolVariable, beforeSend: function(xhr) { xhr.setRequestHeader("Connection ...

I am attempting to create a captivating image for a "jumbotron"

My goal is to achieve a full-width image that stretches across the entire website. Furthermore, I want the image to remain centered and shrink from the sides as the window size decreases. Here's what I've attempted: HTML <div class="site-ban ...

Learn how to simultaneously play two audio files using the same identifier in JavaScript

I'm facing an issue with two audio files that have a progress bar and both have the same ID: <audio id="player" src="<?=base_url('mp3/'.$rec->file)?>"></audio> </p> ...

Verify / Decline SweetAlert will be confirmed in both instances

When you click on "Confirm" or "Cancel", they both trigger the function "isConfirm". Interestingly, the "Cancel" button does not close the alert as expected. It appears to be clashing with the SweetAlert triggered in ...

The elements in the list are too large for the designated area on Internet Explorer and Chrome browsers on Mac computers

I am facing an issue with my navigation menu on Internet Explorer and Chrome on MAC. The last element of the list is not fitting into the given width (which is fixed at 1000px). It works fine on Firefox, Opera, and Windows Chrome. I cannot test Safari at t ...

Is the navigation bar offset causing an issue?

After struggling with my navigation menu in Google Chrome, I finally found a solution. However, the nav bar is now offset on one page but not on another. For the aligned main site view, visit , and for the left-offset forum view, visit . This is the HTML ...

- "Utilizing the _underscore loop within a design framework"

I am encountering a persistent error while working on this. Specifically, I keep receiving an error related to syntax: syntax error var _p=[],print=function(){_p.push.a... ');}return __p.join(''); <script id="product" type="text/t ...