Are there any CSS hacks available to address the combination of position: sticky and overflow within the parent element?

I've encountered a sticky position issue when the overflow property is set to auto on a parent element. I've tried various solutions like adding an extra wrapper or using clip instead of auto, but none have worked. While I did find a solution using JavaScript, I'd prefer a pure CSS alternative if possible.

     <div className="wrapper">
      <div className="left">
        <div className="header" />
      </div>
      <div className="right">
        <div className="header">Blablabla blablabla blabla</div>
        <div className="table"/>
      </div>
    </div>

Here's the corresponding CSS:

.wrapper {
  margin-top: 30px;
  display: grid;
  grid-template-columns: 200px auto;
  width: 100%;
  max-height: 30vh;
  overflow-y: auto;
  position: relative;
}

.left {
  background-color: chocolate;
}

.right {
  background-color: chartreuse;
  height: 50vh;
  overflow-x: auto;
  position: relative;
}

.header {
  height: 20px;
  width: 100%;
  background-color: black;
  position: sticky;
  top: 0;
  color: white;
}

.table {
  width: 2000px;
}

I've created a small example to demonstrate: https://stackblitz.com/edit/react-starter-typescript-yrmmde

My goal is to make the right header sticky while allowing the right container to scroll horizontally simultaneously.

Answer №1

import React from 'react';
import { ButtonCounter } from './components/ButtonCounter';

export const App = () => {
  return (
    <div className="wrapper">
      <div className="left">
        <div className="header" />
      </div>
      <div>
        <div className="header">Blablabla blablabla blabla</div>
        <div className="right"></div>
        <div className="table" />
      </div>
    </div>
  );
};

.wrapper {
  margin-top: 30px;
  display: grid;
  grid-template-columns: 200px auto;
  width: 100%;
  max-height: 30vh;
  overflow-y: auto;
  position: relative;
}

.left {
  background-color: chocolate;
}

.right {
  background-color: chartreuse;
  height: 50vh;
  overflow-x: auto;
  position: relative;
  z-index: 1;
}

.header {
  height: 20px;
  width: 100%;
  background-color: black;
  position: sticky;
  top: 0px;
  color: white;
  z-index: 2;
}

.table {
  width: 2000px;
}

You placed the header incorrectly in the right div. To resolve this, I removed it from inside and adjusted their z-index based on priority. This should be the solution you're looking for.

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

Exploring the concept of generator functions in ES6

I'm grappling with understanding JS generators and why the asynchronous operations in the example below are returning undefined. I thought that using yield was supposed to wait for asynchronous calls to finish. function getUsers(){ var users; $.aj ...

Flot seems to be having difficulty uploading JSON files

I am relatively new to working with json and flot, but have been tasked with creating a chart. Can someone please help me troubleshoot why my code is not functioning as expected? $.getJSON('chart.json', function(graphData){ alert(graphData); ...

Find the specific element that is visible within a customized viewport, such as a div or ul

Exploring the capabilities of the Viewport Selectors for jQuery plugin, here is a snippet of its source code: (function($) { $.belowthefold = function(element, settings) { var fold = $(window).height() + $(window).scrollTop(); return fold <= $ ...

What occurs when a function component is passed as a state variable?

Can a function component be passed as a state variable? Is it possible for the code below to work correctly? I attempted it but encountered an error stating props.children isn't a function. Do you know if this approach can be successful? const App = ...

Creating a Duplicate of the Parent Element and its Child Elements using jQuery

Here is a code snippet I have that adds a new paragraph when a button is clicked. Currently, the script clones the "sub" div and appends it to the "main" div. However, the issue is that it only copies the content of the "inner" div within the "sub" div ins ...

Safari is not functioning properly with the css display:none property

My HTML code looks like this: <div id="loadinganimationsearch"> <div style="color: #28ABE3;font-size: 14px;float: left;">Fetching Textbooks</div> <div id="block_1" class="barlittle"></div> <div id="block_2" ...

Plugin for controlling volume with a reverse slider functionality

I have been customizing the range slider plugin found at to work vertically instead of horizontally for a volume control. I have successfully arranged it to position the fill and handle in the correct reverse order. For instance, if the value is set to 7 ...

initiating AngularJS ng-model pipeline on blur event

My $parser function restricts the number of characters a user can enter: var maxLength = attrs['limit'] ? parseInt(attrs['limit']) : 11; function fromUser(inputText) { if (inputText) { if (inputText.length > max ...

Incorporate a picture using just a portion of the character at the start of the file name in

I have a collection of images in my React assets folder, each with a unique code number: 10000_clear_large.png <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="57666767676708343b323625083b3625303217652f79273930">[emai ...

``Error message "TypeError: handler is not a function" appears in Amplify deployed application, although the function works correctly when running locally

I am currently working on an app developed in Nexjs (13.4, Node 18.16) that communicates with a Lambda function through API Gateway. While the app runs smoothly locally using npm run dev, I encountered an error when trying to access the website via the Am ...

change the css back to its original state when a key is pressed

Is there a way to retrieve the original CSS of an element that changes on hover without rewriting it all? Below is my code: $(document).keydown(function(e) { if (e.keyCode == 27) { $(NBSmegamenu).css('display', 'none');} ...

Is there a way to ensure the alignment of items remains consistent, especially for the rightmost column, by enclosing them within a div element?

I'm currently working with Vue.js and using a UI component framework designed for it. I'm facing an issue where aligning numbers like 500 or 5000, as well as icons, is proving to be difficult. The numbers in the rightmost column need to be aligne ...

Deactivate user input in Knockout Kendo TimePicker

Is it possible to prevent user input in the Kendo UI TimePicker while using knockout-kendo binding? In a previous project without knockout-kendo, I was able to achieve this by using the following code (see jsfiddle example): $('#timepicker').at ...

javascript - disable screen capture of specific parts of a webpage

Recently, I've come across certain web pages that have a unique feature preventing screen capture of specific regions. When attempting to take a screenshot using Chrome, some areas that are visible on the live page appear as black frames in the captur ...

React's Material-UI AppBar is failing to register click events

I'm experimenting with React, incorporating the AppBar and Drawer components from v0 Material-UI as functional components. I've defined the handleDrawerClick function within the class and passed it as a prop to be used as a click event in the fun ...

Using HTML with Django Reportlab

Hey there! I'm currently experimenting with creating PDFs using Python Django and the reportlab library. While I've been successful in generating PDFs with simple text, I'm facing challenges incorporating HTML elements like <h1>Hello&l ...

What is the best way to convert modal window functionality from jQuery to native Javascript?

How can I convert this jQuery code for modal windows into native JavaScript while using data attributes? I am having trouble integrating forEach and getAttribute in my code. I usually rely on jQuery, but now I need to switch to native JavaScript. I apprec ...

Anticipate that the function will remain inactive when the screen width is less than 480 pixels

Implementing Functionality in Responsive Navigation <script> document.addEventListener('DOMContentLoaded', () => { let windowWidth = window.Width; let withinMobile = 480; let close = document.getElementById('close&ap ...

"Customize the font style of columns in a WordPress table created with TablePress by setting them to it

Recently, I designed a table in a WordPress blog using the TablePress plugin. My intention now is to style the left column's font in italics, excluding the table header. ...

Error in identifying child element using class name

I need help accessing the element with the class "hs-input" within this structure: <div class = "hbspt-form".......> <form ....class="hs-form stacked"> <div class = "hs_firstname field hs-form-field"...> <div class = ...