What is the reason behind negative z-index causing non-statically positioned elements to lose their hovering capabilities?

I've recently discovered that when setting z-index:-1 to non-statically positioned elements, it can affect their hovering capabilities. Interestingly, the behavior of absolutely and fixed positioned elements varies under different conditions.

  • Absolutely/Fixed positioned elements lose the ability to hover partially only if there is text written after them. Hovering near the top border doesn't work in this case. However, if there is no text after them, hovering works correctly.

  • Relatively positioned elements completely lose their hovering capability, even without any text following them.

Relatively positioned:

<!DOCTYPE html>
<html>
<style>
.tooltip {
    position: relative;
    display: block; 
    border: 5px solid black;
    padding: 5px;
    z-index: -1;
}

.tooltip:hover {
    color:red; background-color: yellow;
}
</style>
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">
Hover over me
</div>



</body>
</html>

Absolutely positioned with text after it:

<!DOCTYPE html>
<html>
<style>
.tooltip {
    position: absolute;
    display: block; 
    border: 5px solid black;
    padding: 5px;
    z-index: -1;
}

.tooltip:hover {
    color:red; background-color: yellow;
}
</style>
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">
Hover over me
</div>

Random text

</body>
</html>

Fixed positioned with text after it:

<!DOCTYPE html>
<html>
<style>
.tooltip {
    position: fixed;
    display: block; 
    border: 5px solid black;
    padding: 5px;
    z-index: -1;
}

.tooltip:hover {
    color:red; background-color: yellow;
}
</style>
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">
Hover over me
</div>

Random text

</body>
</html>

Question: What is the reason behind the removal of hovering capabilities for absolutely/fixed positioned elements when setting z-index:-1, especially when there is text after them, while relative positioned elements lose their hovering capability entirely?


Addendum: Despite gaining some understanding from other users' help, I still have some lingering doubts:

  • Why does the entire viewport take on the color of the body? Even though the border indicates that the body doesn't cover the entire viewport, giving the body a color results in the whole viewport adopting that color.

  • If we hover over the inner child box with z-index:-1, why does the parent container (i.e., body) get automatically hovered as well?

Answer №1

Are you familiar with how z-index functions?

https://i.sstatic.net/mQDzQ.jpg

  1. Using a positive z-index moves the element to a higher layer.

  2. Using a negative z-index moves the element to a lower layer.

Now, let's examine the following images:

https://i.sstatic.net/yJVHw.jpg

In the first image, the document flow is normal. The div element being relatively positioned automatically increases the height of the wrapper element. With a z-index set 1 layer above the wrapper, we can hover over the element as it appears above the wrapper.

https://i.sstatic.net/tazUq.jpg

In the second image, the element's z-index is set to -1, placing it one layer below the wrapper element. The covering wrapper remains above the element, preventing us from hovering over it.

https://i.sstatic.net/xzJHS.jpg

The third image shows an abnormal document flow, known as out of flow. When the div element is fixed or absolutely positioned, the height of the wrapper does not increase. However, with a z-index set 1 layer above the wrapper, we can still hover over the element.

https://i.sstatic.net/pguXo.jpg

In the fourth image, the element's z-index is again set to -1, positioning it one layer below the wrapper element. Despite the covering wrapper being above the element, its height isn't increased to cover the element, allowing for hovering even when the element is fixed or absolutely positioned.

This explanation should clarify any uncertainties you had about z-index.

Answer №2

When an element is transparent and there is another element with a z-index:-1; positioned beneath it, the hover effects are hindered.

Think of z-index like the different elevations in a building, seen from a bird's eye view. You cannot access the basement if there is a floor above it, even if it is made of glass.

If there are no elements stacked on top of it, then hovering over the transparent element is possible, allowing you to metaphorically 'reach the basement'.

Answer №3

I managed to figure out the solution on my own, with special thanks to users BoltClock and Bhojendra for their input. It's a mystery why BoltClock decided to delete their answer, as they correctly pointed out that the parent container was obscuring the child box based on the amount of text in the parent container. To better visualize the issue, I simply added a border to the body element, giving it a red dotted border in the provided code snippets:

Relatively positioned:

<!DOCTYPE html>
<html>
<style>
.tooltip {
    position: relative;
    display: block; 
    border: 5px solid black;
    padding: 5px;
    z-index: -1;
}

.tooltip:hover {
    color:red; background-color: yellow;
}

body {
        border: 2px dotted red; 
}
</style>
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">
Hover over me; I'm underneath the body.
</div>



</body>
</html>

Absolutely positioned with text after it:

<!DOCTYPE html>
<html>
<style>
.tooltip {
    position: absolute;
    display: block; 
    border: 5px solid black;
    padding: 5px;
    z-index: -1;
}

.tooltip:hover {
    color:red; background-color: yellow;
}

body { 
       border: 2px dotted red;
}
</style>
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">
Hover over me
</div>

RAndom text

</body>
</html>

Fixed positioned with text after it:

<!DOCTYPE html>
<html>
<style>
.tooltip {
    position: fixed;
    display: block; 
    border: 5px solid black;
    padding: 5px;
    z-index: -1;
}

.tooltip:hover {
    color:red; background-color: yellow;
}

body { 
       border: 2px dotted red;
}
</style>
<body style="text-align:center;">

<p>Move the mouse over the text below:</p>

<div class="tooltip">
Hover over me
</div>

RAndom text

</body>
</html>

One can observe that the body is overlapping the Hover over me box.


While I have identified the issue with hovering, there are still some uncertainties that have been included in the initial question.

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

Automatically refresh HTML table updates

I'm working with an HTML table and I want to use jQuery to customize it in the following way: If I input a future date, I want jQuery to change the date's color to green. For past dates, I want jQuery to change the date's color to red. Ca ...

Add stylish stripes to your tables with ng-repeat

I need to display a large set of data in a table using ng-repeat. Each row has only one <tr> element. I want to alternate the color of rows, with odd rows in one color and even rows in another. However, I'm struggling to achieve this using ng-re ...

Resolving the Issue of CSS Image Overlapping Text on Mobile Devices

I'm facing a CSS issue regarding the positioning of an image on small screens. The image is not responsive and I've tried using position:relative; and adjusting margins manually, but it hasn't resolved the problem. Below is my HTML Code: & ...

Tips for creating a custom Angular Material modal window that is fully responsive to its parent modal window

Hey there! I've put together a custom modal window in Angular Material by combining the custom modal window example with the Angular Material slider demo. Everything is working well except for the fact that the sliders inside the modal window are not ...

Error message indicating that Element <> cannot be scrolled into view persisting despite attempting common troubleshooting methods

Currently, I am utilizing Selenium to create a web scraper for downloading multiple podcast episodes from Spreaker. # https://www.spreaker.com/show/alabamas-morning-news-with-jt for i in range(3): print("Click number: {}".format(str(i))) see_mor ...

In what situations should the `#` symbol be used to select a DOM element?

There are times when I have to retrieve elements in the following ways: var object = document.getElementById('ObjectName'); Other times, it's done like this: var object = document.getElementById('#ObjectName'); What distinguishes ...

Themes in Next.js with a splash of color

Is there a way to implement theming with color picking for elements? Check out the example here. I've explored using CSS variables and changing the document classname, but I'm uncertain if this is the best approach. @tailwind base; @tailwind com ...

Maintain mouse attention through CSS

Currently, I am facing an issue with my login screen involving the mouseenter and mouseleave events. I have a gif image that disappears when the mouse hovers over it and reappears when the cursor moves to a different part of the page to allow users to ente ...

"Explore the functionalities of Drupal's innovative Menu module: How sub-sub menus can take precedence over sub-menus

When visiting , I noticed that the Nice Menu module for Drupal is not displaying sub-sub menus properly. If you navigate to the first item in the menu and then select the first one in the sub-menu (Facilities->Spring->Calendar), you'll see that ...

Optimizing CSS Breakpoints for Col-sm-X Classes: A Comprehensive Guide

My HTML code appears as follows: <Row> <Col class="col-xs-12 col-sm-12 col-md-4"> //Content here <Col> <Col class="col-xs-12 col-sm-12 col-md-4"> //Content here <Col> <Col class="col-xs-12 col-sm-1 ...

Center the div and make it position fixed

Is there a way to position a div in the center of the screen or its parent div, and have it fixed so that it does not shift when its width changes? I have a div containing a table, as shown below: https://i.sstatic.net/OVh2y.png I'm unsure if the oute ...

When the content of input fields (specified by type="number") is modified, the two-way binding property of angularjs is lost

My issue seems simple but is quite tricky to tackle. When accessing the index.php (locally using xampp), you will encounter a basic form. The form contains multiple elements and is a work in progress, hence the likelihood of bugs. However, there is one par ...

Achieving the extraction of a particular string from an HTML element using JavaScript

<input id="WD01B3" ct="CB" lsdata="{2:'WD01B4',4:'Any',20:'\x7b\x22WDA_TYPE\x22\x3a\x22DROPDOWN_BY_KEY\x22,\x22WDA_ID\x22\x3a\x22ABCA950297D2C0C432BAB9BB ...

Assistance with Parallax Mouse Movement, Using JQuery, HTML, and CSS

As a newcomer to JQuery, I decided to experiment with creating a parallax background effect using the mousemove function. By utilizing event.pageX to generate a variable for adjusting the background position, I was able to achieve the desired result. Alth ...

Uniform design across various phone screen sizes achieved using JQuery Mobile framework

When I use phone with different screen resolutions, the layout appears differently. Desired Outcome Result when resolution is increased Is there a simple way to make it scale proportionally using JQuery Mobile? ...

Learn how to customize the background color of the DropdownToggle when a DropdownItem in Bootstrap 4 is clicked

Is it possible to modify the background color of DropdownToggle when selecting DropdownItem? Additionally, is there a way to adjust the top and bottom blue borders when clicking DropdownToggle? Picture 1: The image depicts the initial appearance before se ...

Ways to style CSS for inactive checkboxes?

How can I easily change the CSS for disabled checkboxes in a grid to make them more visible for users? Technologies I prefer to use, ranked from most preferred to least: CSS JavaScript jQuery Other ...

Incorporating jQuery functions in the Angular app's main component file, app

I've been working on an Angular application and recently added the jQuery package with the following code: $ npm install jquery --save Now, I am attempting to transfer data from my HTML web page's .js file to the app.component.ts in Angular. I ...

Accessing a specific child div within a parent div using JavaScript and CSS

Struggling to target and modify the style of the child div within id="videoContainer" <div id="videoContainer"> <div style="width: 640px; height: 360px"> <------- this is the target <video src ...

Is it possible to assign the string variable to "</a>" in PHP without it being interpreted as a command?

I am currently working on a simple web page project, where I want to include a clickable link using HTML. While I have figured out the initial steps, I'm facing an issue with the following code snippet: $URLString = "<"; $URLString .= "/a"; $URLS ...