I am dealing with a specific layout challenge involving two columns, where I need the right column to remain fixed in place while scrolling through the longer left column.
These columns are built using Bootstrap, so initially, I had to eliminate floats and utilize display: inline-block
. This approach functions properly when implemented at the top of the page's DOM structure. However, as it is rendered further down, approximately 30 divs deep, the sticky positioning fails to work as desired.
I understand that an overflow
property set on a parent element can interfere with position: sticky
, but this does not seem to be the root cause in this scenario. Could the issue be related to any ancestor elements having overflow properties set that disrupt sticky positioning?
In such situations, I am uncertain about what factors to investigate to identify the problem accurately. Are there any specific considerations to keep in mind when working with sticky positioning?
EDIT: Upon closer investigation and testing, it appears that an ancestral element near the top of the DOM tree has been assigned overflow-x: hidden
. Since this code is shared, I will need to locate the reason for this setting and its necessity.
Meanwhile, is there a workaround available whereby an element further down in the DOM hierarchy can function as the containing element for maintaining sticky positioning?
In the demo below, eliminating overflow from .theproblem
restores the desired behavior (the right column sticks during page scroll).
.theproblem {
overflow-x: hidden;
}
.column {
display: inline-block;
width: 45%;
vertical-align: top;
}
.column1 {
border: 1px solid red;
height: 1000px;
}
.column2 {
border: 1px solid green;
position: sticky;
top: 1px;
}
<div class="theproblem">
<div class="columnwrapper">
<div class="column column1">
This is column 1 (the tall one)
</div>
<div class="column column2">
This is column 2 (the sticky one)
</div>
</div>
</div>