Achieving overlapping of a <div> element with a "position: fixed" container while enabling vertical scrolling of the fixed container's content

In my single page application, I have a fixed container and I am trying to make one div inside this container overlap the boundaries of the fixed container while also vertically scrolling with its contents. Unfortunately, I have only been able to achieve either one or the other, but not both simultaneously. I am wondering if there is a CSS-only solution available for this issue?

You can view a demo of the problem here.

The Objective

[Example image removed due to missing reputation. View it here.]

My goal is to create a clickable marker next to an input control in a fixed sidebar, where users can edit various properties of a business item. The marker should stand out by overlapping the container's boundaries. To achieve this, there are five main requirements:

  • The topmost container must have position: fixed, as the background SPA can be scrolled in any direction
  • The content within the topmost container should scroll vertically when needed, without horizontal scrolling
  • A box should be positioned next to a target within the content
  • This box should overlap the topmost container's boundaries
  • The box should also scroll vertically along with the topmost container's content

The structure follows fixed container > content(s) > overlapping div. I am aware that using overflow and relative positioning together can prevent an absolute child from overlapping correctly. Therefore, I have tried various combinations of CSS rules and HTML markup, but have not yet found a solution that satisfies all requirements. Any suggestions on achieving this without backend code are greatly appreciated.

The Code

Here is a simplified version of the HTML markup:

<div class="sidebar-container">
  <div class="sidebar-content-wrapper">
    <div class="sidebar-content">
      <div class="sidebar-content-main">
        <!-- more content -->
        <div class="overlap-container">
          <div class="overlap-wrapper">
            <div class="overlap-relative-container">
              <div class="overlap-box"></div>
            </div>
            <div class="overlap-caption">Some text, the box should appear left to this element.</div>
          </div>
        </div>
        <!-- more content -->
      </div>
    </div>
  </div>
</div>

The topmost container has a fixed position:

.sidebar-container {
  position: fixed;
  // [ ... ]
}

The content wrapper enables vertical scrolling while hiding the horizontal scrollbar:

.sidebar-content-wrapper {
  height: 100%;
  // content should be scrollable vertically, while horizontal scroll is not allowed
  overflow-y: scroll;
  overflow-x: hidden;
}

The box that should overlap the container is positioned absolutely:

.overlap-box {
  position: absolute;
  left: -2em;
  width: 3em;
  height: 3em;
  z-index: 99;
  // [ ... ]
}

However, the box does not stay overlapped when the content is scrolled vertically, as it is positioned relative to the nearest positioned element, .sidebar-container. Adding position: relative to the parent element of the box allows it to scroll with the content, but the overlap is lost.

You can find the complete code in this jsfiddle.

Key Takeaways

  • A relative positioned element within a container with specified overflow rules will clip absolute positioned children
  • I have researched extensively online but have not found a solution tailored to this specific issue, apart from a mention in the comments section of a blog post titled How to make absolute positioned elements overlap their overflow hidden parent
  • Complex layout scenarios using CSS alone can be more challenging than expected

Note: Although I acknowledge that a JavaScript solution using the onscroll event is possible, I prefer a purely CSS-based approach to resolve this matter elegantly. Your insights on untangling this dilemma would be greatly appreciated!

Answer №1

As far as I know, the only method to allow child elements to be displayed outside of their parent container in this scenario is by using

overflow: visible;

or, in your specific case:

overflow-x: visible;

on the parent container.

However, if you also require vertical scrolling, this will take precedence over the visibility setting irrespective of the axis it's applied to, causing the children to be clipped to fit the parent dimensions.

Source: https://www.w3.org/TR/css-overflow-3/#valdef-overflow-scroll

"This value indicates that the content is clipped to the padding box, ..."

For further details on this conflict/issue:

https://developer.mozilla.org/en-US/docs/Web/CSS/overflow

https://www.w3.org/TR/css-overflow-3/#scrollable-overflow

If you come across a CSS-only workaround, please do share with the community! Otherwise, it seems like having a non-scrollable sidebar or exploring an alternative design might be necessary.

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

How to customize the background of radio buttons in HTML

I want the background color to stay consistent as lightgray for each <ul>. Currently, clicking the radio button causes the ul's background to change incorrectly. I am unsure of how to loop through all available ul elements using jQuery and woul ...

What is the best way to create a seamless change from a black to white invert filter?

I'm currently working on a project where I need to change my icons from black to white. After some research, I found that the best way to do this is by using filter: invert(100%). However, when I try to transition it, the change happens abruptly after ...

Webpack2 now transforms sass/scss files into JavaScript rather than CSS during compilation

I've created a webpack script to compile all .scss files into one css file. I decided to use webpack instead of gulp or grunt for simplicity, as it can be configured in a single file. However, I am encountering an issue where scss files are being com ...

Experience a seamless front/back DIV transition with a mouseover effect similar to the ones on USAT

Recently, I was tasked with creating a mouseover transition effect for a div similar to the one used on USAToday's website. The structure of the boxes on the site includes: <div class="asset"> <div class="front">'image and some t ...

Center the text within a span element in an inline-block container

I'm currently working on a website design that features a flat aesthetic. I have a header at the top and two blocks of different colors placed side by side underneath it. Initially, I attempted to use float left and right for positioning but was recom ...

What is the reason for making the position of bg-container absolute?

Discover more about the "position: absolute" attribute Remove the position property for troubleshooting After testing this page in Chrome, I encountered a confusion regarding the usage of the position property within the bg-container. To my surprise, del ...

The interaction of a horizontal scroll bar on a CSS Grid layout triggers the appearance of a vertical scroll bar

I am facing a challenge with my HTML layout. In Chrome, when the horizontal scroll bar is visible, a vertical scroll bar also appears. However, in Firefox, everything works fine. This issue seems to be related to the height of the div not auto-scaling prop ...

css content exceeds div boundaries

Hello, I am experiencing an issue with my webpage located at . When I zoom out, the content overflows from its containing div. Setting the height to auto or 100% works fine, but the left column ends up being smaller than the right one, and using clear do ...

Spacing between Div tags

Struggling with a tricky HTML cross-browser problem. The site was built by multiple people, making it quite messy, but needs to go live as soon as possible! Each of the 4 page layouts on the site are different and handled by separate classes. However, whe ...

Trouble displaying certain HTML code and its output on the browser?

I am in the process of developing a user profile page. I have designed a form with various fields, but the code within the section is not displaying correctly in the browser. I attempted to inspect the elements, but the code within that section remains inv ...

The CSS3 Transform feature kicks in only after the window is resized in Mozilla Firefox

I have created a pure CSS3 parallax webpage by following the method outlined by Keith Clark and using a sample made by Carl Henderson (I can't provide a direct link to his codepen due to my lack of reputation). You can find the code in the main page, ...

The appearance of my HTML website varies significantly across different devices used by viewers

It's frustrating to see that my HTML website appears differently on various devices. While it looks perfect on mine, my friend is facing issues such as containers being out of place and images not loading properly. I really need to figure this out as ...

What is causing the unusual behavior of z-index in this specific scenario?

I have a good understanding of z-index and decided to practice with it by writing the following code: *{ padding: 0; margin: 0; box-sizing: border-box; } body{ height: 100vh; di ...

Resolving Issues with Text Overlaid on an Image Hyperlink

Alright, so this problem might be a bit too complex for CSS/HTML to handle, but I'll give it a shot anyway. I have some images that are dynamically placed using the code from this website. What I want to do with these images is add a text overlay w ...

Tips for navigating a dynamic viewport using scroll movement

Attempting to create a responsive page with two distinct sections at this example link including: Map View Table View Both of these views (table and map divs) need to be responsive without a hard-coded height, so the size of the map div adjusts automatic ...

Fancybox 2 - CSS styles vanish when using Ajax request

I have a fancybox2 with static dummy content that has styling applied. It works fine, but now I need to load dynamic content via ajax. However, when I make the ajax call, the required content loads but loses all css styling, most likely due to changes in t ...

What's the best way to add line numbers to source code on an HTML webpage after parsing?

I am currently working with AngularJS and MongoDB. In my MongoDB data, there are some text elements that contain a \n, causing each line to be displayed on a new line based on the occurrence of \n. However, I also want to add line numbers to each ...

I am having trouble getting Google Maps to load

Why does the map on my website only load when the browser window is resized? Below is the JavaScript code being used: <div class="map-cont"> <div id="map" class="location-container map-padding" style="width:100%;height:400px;background:y ...

Is it possible for HTML/CSS to automatically adjust content size to fit or fill a container?

I'm interested in finding a CSS-only technique that can ensure content within a container scales to fit, regardless of whether it's text or images. Take a look at the example below: .container { display: inline-block; width: 2in; hei ...

How to Add a Responsive Inset Transparent Overlay to Images without Using JavaScript?

My goal is to create a unique overlay effect on images of different sizes within a Pinterest-style fluid grid layout. The outer border width and inset 'border gap' will remain consistent, while the images themselves need to dynamically adjust. C ...