Is there a way for a CSS absolute element to overlap its parent's sibling without interfering with scrolling or zooming functionality?

I have a unique scenario where I want to create a sidebar with navigation items in a list that expand on hover. The challenge is to make the hovered item from the left div overlap the right div.

This functionality can be achieved by setting the list items as absolute. However, an issue arises when zooming or scrolling in the overflow as their position gets disrupted. To address this, placing them within a parent container set to relative helps maintain their original positions, but it also affects the stacking order and prevents overlapping the right div.

Is there any method to allow the left items to overlap the right div without interfering with their position during zooming or scrolling?

.container {
  display:flex;
}

.left {
  width:49vw;
  height:50vh;
  background-color:yellow;
  overflow-y:auto;
  overflow-x:hidden;
}
.right {
  width:49vw;
  height:50vh;
  background-color:green;
  position:relative;
  
  //position:absolute;
  //left:49vw;
  z-index:1
}

li {
  position:relative; //affects scrolling/zooming behaviour if not used
}
    
.item-text:hover {
        background-color:red;
        width:80vw;
        position:absolute;
        z-index:2;
    }
<div class="container">

  <div class="left">
    <div class="list-container">
      <ul>
        <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
      </ul>
    </div>
  </div>
  <div class="right">
 
  </div>


</div>

.container {
  display:flex;
}

.left {
  width:49vw;
  height:50vh;
  background-color:yellow;
  overflow-y:auto;
  overflow-x:hidden;
}
.right {
  width:49vw;
  height:50vh;
  background-color:green;
  position:relative;
  
  //position:absolute;
  //left:49vw;
  z-index:1
}

li {
  //position:relative; //affects scrolling/zooming behaviour if not used
}
    
.item-text:hover {
        background-color:red;
        width:80vw;
        position:absolute;
        z-index:2;
    }
<div class="container">

  <div class="left">
    <div class="list-container">
      <ul>
        <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
                <li><div class="item-text">
        1
        </div></li>
      </ul>
    </div>
  </div>
  <div class="right">
 
  </div>


</div>

Check out jsfiddle for more details.

Answer №1

Understanding z-index is crucial for controlling the stacking order of elements on a web page. It's important to note that z-index only affects the stacking order within the parent element and works only for elements with a position property other than static.

In your case, the z-index of the .right div is 1 while the z-index of the .item-text is 2. However, since the .item-text is contained within the .left div which defaults to an auto z-index, it remains below the .right div.

To overcome this issue, setting the z-index on the parent elements can be a solution. In the example provided below, the z-index of the yellow div is set higher to ensure its content overlaps its sibling, resolving the stacking order problem.

For further insights on z-index, you can refer to:

  .container {
    /* Achieve layout using flex positioning or a combination of float and relative positioning */ 
     display:flex; 
  }

  .left, .right {
    display:inline-block;
    width:50%;
    height:200px;

    /* An alternative method to arrange elements explicitly defining the position property */

    /*
    float:left;
    position:relative; 
    */
  }

  .left {
    background: yellow;
    z-index:2;
    overflow:visible;
  }

  .right {
    background: green;
    z-index:1;
  }

  li:hover {
    background: red;
    width:120%;
  }
<div class="container">
  <div class="left">
    <ul>
      <li>item 1</li>
      <li>item 2</li>
      <li>item 3</li>
      <li>item 4</li>
    </ul>
  </div>
  <div class="right">
  </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

CSS Horizontal Timeline Design

https://i.sstatic.net/zBXre.png Looking for an image like this one, with horizontal scrolling navigation buttons to dynamically update the list. I have included CSS and HTML code below. The task should be accomplished using only CSS, but JavaScript can be ...

Display a single image at the center of the screen, regardless of resolution

I have been attempting to replace the default OHS index.html with an image measuring 1640px x 248px, and I want it to be centered both horizontally and vertically on any screen it is displayed on. For example, whether it's a 4K screen, 1920x1080, 102 ...

Spotting repeated terms within an HTML document

I have a table with results generated using v-html (meaning the text inside the table does not appear until the page is rendered). I want to compare two rows and highlight any duplicate words they may contain. While looking for examples, I came across a p ...

Navigate the conversation within the dialog without affecting the content below

Is it possible to have a dialog with scrollable content on top of a page that is also scrollable, but when trying to scroll inside the dialog using the mouse wheel, only the dialog body scrolls and not the page below it? What approach can be used to accom ...

Position: fixed CSS child element ignoring parent's layout constraints

When a parent element is positioned (using position: relative or position: absolute) and contains a child element with position: absolute, the child element will be positioned absolutely inside the parent. A child element with position: sticky will behave ...

Connecting radio buttons to data in Vue using render/createElement

How do you connect the value of a selected radio button to a specific variable in the data object within a Vue application when using the render/createElement function? ...

Error occurs when loading page due to issue with Django's javascript translation

Here is the structure of my urls.py: js_info_dict = { 'domain': 'djangojs', 'packages': ('project',), } urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include(' ...

A guide on transforming HTML into a variable using JavaScript

Having trouble converting HTML to a JavaScript variable. This is my HTML code: <div onclick="openfullanswer('2','Discription part','This is the code part');"> I want to create this HTML code dynamically, but I&apo ...

Displaying a vertical arrangement of collapsed menu items with a centered navbar and logo positioned at the center of it (using bootstrap)

I'm currently working on a website that features a centered navbar with a logo also centered inside it. The tech I'm using is Bootstrap and LESS. Issue When the navbar collapses on mobile/tablet view, the menu items are displayed horizontall ...

CSS: float issues occurring at unpredictable intervals

In the creation of my website, I have incorporated small login and logout buttons that are positioned in the top right corner. Within a "header_content" division, there is another div with a style of "float: right" to keep it aligned in the top right corne ...

Retrieve the Current Time with an Ajax Request

Hi there, I am facing an issue with calling code from an ajax request for getting the current time. I have set up an HTML file with a working clock in it, but when loading the text from $container1, the clock and date are not displaying on the page. Can an ...

I'm encountering difficulties trying to align my images in the center on a mobile device using foundation's XY grid system

I recently created a practice website using the new Foundation xy grid. However, I noticed that when I resize the browser to a smaller screen size, the images are no longer centered and instead hug the left side of the page. It's only when I make the ...

The sticky navigation bar isn't functioning properly, even though the code worked perfectly on W3School

I recently encountered an issue while trying to create a sticky navbar that would remain fixed after scrolling past a certain point. I found and customized some code from w3schools that included an animation effect, and it worked perfectly in their demo. H ...

Tapestry creates the shape attribute for the <a> element

Currently, I am working with Tapestry 5.2.6 and I am attempting to remove the shape attributes on anchor tags. The tag generated by Tapestry appears like this: <a shape="rect"...> I have searched online without any luck, so if you have any insight ...

Changing the parent div element based on the child div element

Is there a way to dynamically apply style properties, such as 'float', to the parent div based on its child div element, preferably using JavaScript without the need for additional div elements? <div class="chat-message"> <span clas ...

producing complex HTML content using xquery output

I'm currently facing an issue with returning a multi-element HTML result (not sure of the technical term for this). To provide more clarity, take a look at the code snippet below. <body> <h1>Nobel laureates</h1>{ for $subject in ...

Tips for resizing a Shiny Dashboard to accommodate various screens

I created a shiny app with a specific layout on my desktop computer that fits perfectly on the screen. However, when I run the app on my notebook, only the top left boxes are visible, making the app too large for the screen. Even after resizing with Ctrl ...

The placement of my footer is off and not aligned with the bottom of the page

I am struggling to make my footer align with the bottom of the page rather than sticking to the bottom of the browser window and moving along with the scroll. How can I fix this issue? The goal is to have the footer remain out of sight at the bottom of th ...

Utilize PHP to dynamically generate an HTML table with grouped data

Here is the structure of my MySQL database: productid | productname | category 1 XYZ News 2 ZYX News 3 vcb Info I would like to display this data in a table grouped by category as show ...

Removing values when disabling a button using JavaScript

Within my code, I have two tables - one for non-clickable teams and another for clickable matches. Check out the image of the tables View the code on my GitHub repository let teams = [ {name: 'Roma', points: 0, wins: 0, draws: 0, loses: ...