What is the relationship between `overflow:hidden` and the inability of `position:sticky` to function properly?

Within the code snippet provided, there is a sticky div positioned inside a container. It adheres to the top of the scrolling panel while remaining within its designated container at all times. This mimics the behavior seen in iOS' UITableView headers, where headers stay visible until the next header reaches the top.

In the second example, everything remains consistent except for the fact that the container has an overflow:hidden CSS rule. This seemingly hinders the proper function of the position:sticky property.

.parent {
  position: relative;
  background: #ccc;
  width: 500px;
  height: 150px;
  overflow: auto;
  margin-bottom: 20px;
}

.hidden-overflow {
  overflow: hidden;
}

.sticky {
  position: sticky;
  background: #333;
  text-align: center;
  color: #fff;
  top: 10px;
}
<div class="parent">
  <div>
    <div class = "sticky">
      Hi, I am a sticky element inside the container which contains the first paragraph.
    </div>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit...

(Extract taken from @Daniel's post on here)

What causes position:sticky to malfunction when applied in a container with overflow:hidden?

Answer №2

overflow: hidden does not inhibit the functionality of position: sticky. However, if you apply overflow: hidden to any parent element of your sticky element, that parent element will serve as the scrolling container for your sticky element. If you change the overflow property of the parent element from hidden to scroll and scroll this parent (not the window), you will observe that the sticky behavior still persists.

For more information, visit https://github.com/wilddeer/stickyfill#pro-tips:

Any non-default value (other than visible) for overflow, overflow-x, or overflow-y on any of the ancestor elements anchors the sticky element to the overflow context of that ancestor. In simple terms, scrolling the ancestor will trigger the sticky behavior, while scrolling the window will not. This behavior is expected with overflow: auto and overflow: scroll, but it may lead to issues and confusion with overflow: hidden.

Alternatively, refer to :

The positioning of the box depends on its containing block (defined as for position:static) as well as its scrolling container, determined by the closest ancestor in the same document with a computed value for 'overflow-x' or 'overflow-y' other than 'visible', or the viewport if no such ancestor exists.

Or consult the CSS Positioned Layout Module Level 3 W3C Working Draft:

A stickily positioned box is placed similarly to a relatively positioned box, but the offset is calculated relative to the nearest ancestor with a scrolling box, or the viewport if no ancestor contains a scrolling box.

Answer №3

While troubleshooting an issue, I came across a different approach that worked for me. Instead of using overflow: hidden, I tried using overflow: clip. It seems like they function similarly, but with clip, the anchor point of the sticky element remains unaffected. The only downside is that browser support for overflow: clip is limited to around 80% as of November 2022: https://caniuse.com/mdn-css_properties_overflow_clip

Answer №4

I encountered a similar problem recently and found a solution by substituting overflow: hidden; with the use of clip-paths.

.container {
    /*overflow: hidden; removed */
    position: absolute; /*this is necessary for clip-paths to function properly*/
    -webkit-clip-path: inset(0); /*for Safari compatibility*/
    clip-path: inset(0);
    clip: rect(0px, auto, auto, 0px); /* For IE11/Edge browsers (although IE11 does not support sticky positioning anyway) */
}

To address the need for adding position absolute, you can enclose the overflow:hidden element within another element with position: relative, and then define top, bottom, left, and right as 0 to ensure it fills its parent container completely.

Answer №5

Due to the fact that setting overflow: hidden actually establishes a scroll container (even though the scrollbar is invisible and inaccessible to the user), your position: sticky element becomes anchored to it, resulting in it not functioning as intended.

Interestingly, it does work, but not in the manner you might have anticipated.

.overflow-hidden-container {
  overflow: hidden;
  height: 100px;
  border: 1px solid grey
}
<div class="overflow-hidden-container">
  <div style="margin-top: -10px"></div>
  <div style="position: sticky; top: 0;">
    sticky
  </div>
</div>
<div class="overflow-hidden-container">
  <div style="margin-top: -10px"></div>
  <div>
    non sticky
  </div>
</div>

In this scenario, we are utilizing negative margin on the preceding element to shift the text outside of the viewport. Text without sticky functionality is evidently cropped out due to its displacement. However, text with sticky remains unaffected.

The suggestion of using overflow: clip, as mentioned in another response, serves as a modern substitute for overflow: hidden. The key distinction lies in the fact that overflow: clip does not generate a scroll container, enabling it to potentially resolve the issue of position sticky being affixed to an incorrect element.

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 background color in the HTML file remains unchanged

Hello everyone, this is my debut post here. I can't seem to figure out why the background color isn't changing for me. Even though I have applied the bg color property, it's not taking effect as expected. <body bgcolor="#f0ffff"> T ...

Comparing `height: calc(100vh);` to `height: 100vh;` in CSS can reveal a few

Currently tackling a project where the previous developer utilized: .main-sidebar { height: calc(100vh); } Unfortunately, I can no longer reach out to them, and I am curious about the variance (if any) between the two approaches. (Am I in the approp ...

utilize makeStyles to modify button text color

Initially, my button was styled like this: style={{ background: '#6c74cc', borderRadius: 3, border: 0, color: 'white', height: 48, padding: '0 30px', }}> It worke ...

Adsense ad unit is not responsive and fails to display properly at dimensions of 320x50 px

I have a unique banner ad that I want to display as the footer on my responsive website. Using media queries recommended in the advertising documentation, I am adjusting the size based on different screen widths. Check out the CSS code below: <style&g ...

What is the best way to spin an element around its center?

I'm faced with a challenge where I have an element that needs to be rotated using JavaScript. The rotation is currently functional, but it's rotating around points other than its center. My goal is to rotate the element around its own center. ...

Tips for adjusting the height of a Safari extension during runtime

After successfully developing a Safari extension, I am facing an issue with adjusting the width and height of the popover dynamically at runtime. Can anyone provide insights on how to modify the dimensions of the popover based on its content? ...

The React Vite application encountered an issue: There is no loader configured for ".html" files at ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

**Encountered errors in a React Vite web app** ** ✘ [ERROR] No loader is configured for ".html" files: ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html ../server/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:86 ...

I am looking to implement a required alert for a radio button that mimics HTML5. How can

When using the input type text, adding the required attribute prevents the form from submitting and prompts the browser to focus on the required field with an alert instructing to fill it out. On the other hand, when applying the required attribute to t ...

"Is it possible to apply CSS to all HTML elements except for a

I am looking to apply the specified styles to all elements except for a particular class and its descendants. html * { box-shadow: none !important; border: none !important; color: white; } I attempted the following but it did not work as intended: ht ...

What steps can be taken to resolve this issue?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ut ...

Is it possible to execute this animation with a single click for repetitive playback?

CODEPEN const btt = document.querySelector('.btt'); btt.addEventListener('click', function(){ this.classList.toggle('anime'); }); Is there a way to achieve the desired effect with just one click? ...

What is the best way to line up a checkbox and its labels alongside a dropdown menu?

My index.html file has a header like this: https://i.sstatic.net/s0hAt.png I'm trying to align the checkbox and its label with the dropdown menu "In ordine". I'm using only bootstrap (no custom classes) and my current page layout is as follows. ...

Discover the position of a div relative to another div using ng-drag-drop

I'm currently working on a web application that allows users to create their own identity cards. For this project, I am incorporating the ng-drag-drop library from https://www.npmjs.com/package/ng-drag-drop. The main goal is to obtain the coordinate ...

What is the best way to change a dynamically centered text alignment?

Is it possible to add a transition for a center-aligned text that changes due to an action? (codepen) HTML : <div id="container">Lorem ipsum dolor sit amet consectetur adipiscing elit</div> CSS : #container { width: 400px; height: 20px ...

The functionality of jQuery is not properly integrating with Materialize for hiding select options

For a project I'm working on, I have implemented two Select elements in the HTML code. The second select should only be enabled when the first select meets certain conditions. You can check out the JSfiddle link for reference. $(document).ready(f ...

Several treeviews for selecting data

I need some help with a specific assignment. The back end code is all set, but I'm struggling with the UI component. The task requires two tree views, one on the left and one on the right. The left tree view will contain states with multiple children, ...

Top and bottom fieldset legends in Bootstrap 4

Looking for suggestions on how to include legend text on the bottom border in bootstrap 4. I attempted to use position and margin, but it didn't work. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.mi ...

The dropdown menu with Bootstrap's <li> sub menu is not expanding as expected

<li class="dropdown"> <a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">Utilitas<span class="c ...

Mozilla Firefox does not support the use of an input text box

I have developed a stopwatch using JavaScript and customized it with a table using HTML and CSS. Upon testing in Chrome, the UI looked great. However, when I tested it in Mozilla Firefox, I noticed that the input text box appeared smaller than expected. ...

The page only appears properly after clearing the cache

My floating list menu is behaving oddly in Firefox. It only displays correctly after clearing the cache, but as soon as I refresh the page, the menu breaks again. The links inside the list elements appear wider than the text, even though I haven't se ...