Hovering over an image causes the text to be displayed even when z-Index is

I am facing an issue with my code where, on hovering over the H2 element, an image is supposed to be revealed. However, even though I have set the z-index of the text higher than that of the image, the text still moves down when the image appears.

I want the text to remain fixed in its position while only the background image should appear upon hover without affecting the position of the H2.

You can test it out here:

h2 {
  line-height: 68px !important;
  text-transform: uppercase;
  letter-spacing: 2px;
  font-size: 80px;
  font-weight: 0 !important;
  color: #ffffff;
  z-index: 12;
}

.artisthover {
  display: none
}

h2.two:hover img {
  display: block;
  z-index: -1;
  position: relative;
  margin-top: -200px;
  margin-left: -250px
}

h2.two a {
  color: #ffffff;
}

h2.three:hover img {
  display: block;
  z-index: -1;
  position: relative;
  margin-top: -200px;
  margin-right: -250px
}

h2.three a {
  color: #ffffff;
}
<center>
  <h2 class="two">
    <a href="http://lawnyavawnya.com/2018/artists/absolutelyfree">ABSOLUTELY FREE</a>
    <img src="http://lawnyavawnya.com/2018/2019artists/absolutelyfree.jpg" class="artisthover" width="500px">
  </h2>
</center>

<center>
  <h2 class="three">
    <a href="http://lawnyavawnya.com/2018/artists/badgeepoqueensemble">BADGE EPOQUE ensemble</a>
    <img src="http://lawnyavawnya.com/2018/2019artists/badgeepoque.jpg" class="artisthover" width="500px">
  </h2>
</center>

Answer №1

Let's explore the scenario where the text has z-index:2 and the image has z-index:1, assuming that the H2 text will remain fixed. However, this assumption is incorrect when it comes to understanding how z-index functions.

The z-index property determines the stacking order of elements on a webpage. Simply having a higher or lower z-index value does not automatically ensure that elements with higher values will always appear on top. To control the layering of elements, you need to incorporate the position property as well.

For example, if two elements possess different z-index values but share a relative position, their positioning will be influenced by each other - regardless of which element has a higher or lower z-index.

Illustrated below is a basic demonstration:

.top {
  height: 200px;
  background: purple;
  position: relative;
  z-index: 1;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, .5);
}

.top:hover {
  z-index: 3;
}

.bottom {
  height: 300px;
  background: yellow;
  position: relative;
  z-index: 2;
}
<div class="top">

</div>

<div class="bottom">

</div>

In this case, despite the varying z-index values of the elements, their positions interact due to both being relatively positioned. Hovering over the top div reveals the box-shadow overlapping on the bottom div, yet the spatial arrangement remains constant in terms of top, right, bottom, left - solely altering the stacking order.

Now, focusing on your specific instance:

We utilize position: absolute to detach the img element from the document flow, granting us the ability to leverage the z-index property. With the image outside the document flow, its presence no longer influences surrounding elements.

The subsequent code snippet offers a simplistic portrayal with modified position for the image. While it may not align precisely with your vision, it serves as a starting point for customization.

Additional considerations include:

  1. The <center> tag is considered obsolete; opt for text-align: center targeting the h2 instead.
  2. To refine visual aesthetics, contemplate relocating the image outside the h2 tags and enclosing both the h2 and img within a container element (e.g., div). This tactic enhances precision in positioning the image upon display.

h2 {
  line-height: 68px !important;
  text-transform: uppercase;
  letter-spacing: 2px;
  font-size: 80px;
  font-weight: 0 !important;
  color: #000;
  z-index: 12;
  text-align: center;
}

.artisthover {
  display: none
}

h2.two:hover img {
  display: block;
  z-index: -1;
  position: absolute;
  margin-top: -200px;
  margin-left: -250px
}

h2.two a {
  color: #000;
}

h2.three:hover img {
  display: block;
  z-index: -1;
  position: absolute;
  margin-top: -200px;
  margin-right: -250px
}

h2.three a {
  color: #000;
}
<h2 class="two">
  <a href="http://lawnyavawnya.com/2018/artists/absolutelyfree">ABSOLUTELY FREE</a>
  <img src="http://lawnyavawnya.com/2018/2019artists/absolutelyfree.jpg" class="artisthover" width="">
</h2>

<h2 class="three">
  <a href="http://lawnyavawnya.com/2018/artists/badgeepoqueensemble">BADGE EPOQUE ensemble</a>
  <img src="http://lawnyavawnya.com/2018/2019artists/badgeepoque.jpg" class="artisthover" width="500px">
</h2>

Answer №2

<!DOCTYPE html>
<html>
<head>
<style>
.artisthover {
  display: none
}
img {
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: -1;
}
h2.two:hover img {
  display:inline ;

}
h2.three:hover img {
display:inline ;

}
</style>
</head>
<body>

<center>
  <h2 class="two">
    <a href="http://lawnyavawnya.com/2018/artists/absolutelyfree">ABSOLUTELY FREE</a>
    <img src="http://lawnyavawnya.com/2018/2019artists/absolutelyfree.jpg" class="artisthover" width="500px">
  </h2>
</center>

<center>
  <h2 class="three">
    <a href="http://lawnyavawnya.com/2018/artists/badgeepoqueensemble">BADGE EPOQUE ensemble</a>
    <img src="http://lawnyavawnya.com/2018/2019artists/badgeepoque.jpg" class="artisthover" width="500px">
  </h2>
</center>
</body>
</html>

When adjusting the z-index on an image, it's important for the image to have a position set to absolute so that it remains in place, and the display of the parent element must be set to inline as well....

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

Option list malfunctioning in Safari, Mozilla, as well as Chrome browsers

I encountered some issues while working on the front-end of my web app. Here are a few problems: I need to truncate text if it's too long, so I use the following CSS: .suggestion-box-text { white-space: nowrap; overflow: hidden; text-overfl ...

What is the best way to style HTML strings in C#?

Wondering how to create HTML helpers in ASP.net? Looking for the simplest way to generate HTML strings or templates without having to change all double quotes to single quotes manually? You can streamline your process by using a syntax similar to ES6 templ ...

Retrieve information from a cell containing HTML formatting

Is there a way for me to save a specific text from a cell in Google Sheets into a variable, and then use it to send an email with the text formatted within an HTML table? For example: Cell in Google Sheets: "Lorem ipsum dolor sit amet. Ut tenetur a ...

Issue with blur event functionality on custom multi-select Vue component not functioning as expected

The blur event seems to be malfunctioning. It works perfectly when I click anywhere within the component, except for when I click on the input field. If I click inside the input field and then outside the component, the blur event fails to trigger, preve ...

How to align Bootstrap navbar in the center both horizontally and vertically

I am new to using Bootstrap and I am trying to center the navbar both horizontally and vertically. I want it to look like this https://i.sstatic.net/gqwg9.png, but I am having trouble figuring it out. This is my HTML code: <html> <body> ...

Efficient segmentation of text using CSS and JavaScript

Currently, I'm involved in a project that allows users to create their own timelines with events. However, there seems to be an issue with the event titles. Users are able to create events with extremely long titles, such as: `1231231231231231231231 ...

CSS fluid layout with three columns

I am attempting to create a 3-column layout where each column has a fluid width of 33% and height of 50%. However, when I add padding to the columns, it causes the last div to move to the next line. How can I resolve this issue? Example HTML: <div id= ...

Guide to emphasize the active navigation tab in a particular scenario

Utilizing this template for JavaScript to choose the appropriate navigation using JQuery, with reference to this API. Snippet of my current code: index.php <html> <head> <title>ChillSpot Alpha 1.2.3</title> &l ...

Show the output of a JavaScript script in a styled color box using Bootstrap

Is there a way to incorporate JS calculation into a CSS Bootstrap colored box to display random numbers? The code below shows how the structure can be set up. Here is an example of code for a colored box in Bootstrap: <div class="row align-items-c ...

What is the best way to ensure an element stops at the edge of its container?

I am trying to create a stylish slanted sidebar that aligns perfectly with the red section of my webpage, without overlapping into the blue area. Can anyone guide me on how to achieve this? Even though my current code doesn't seem to work here, it fu ...

What is the best way to create interactive canvases that respond to multiple clicks using JavaScript?

Within the canvas, the code snippet below aims to generate a grid of 10x10 colored boxes with alternating reddish and bluish shades on a gray background. The intention is for each canvas to only respond to mouse interactions when the mouse is within its bo ...

Enable the simultaneous opening of multiple accordions

I implemented accordions based on the code in this example. My goal is to allow users to open multiple accordions simultaneously. Currently, when one accordion is open and another is clicked, the first one closes. I want them all to remain open, while sti ...

I seem to be encountering a recurring issue with a jinja variable when trying to create an onclick function

Why do I keep encountering this error message: Uncaught SyntaxError: missing ) after argument list (at bestellen:60:105) This is the HTML code causing the issue: <div class="gerechten"> {% for gerecht in gerechten %} <div ...

Ways to dynamically conceal an HTML element when another element is devoid of content

How can I dynamically hide the heading above a div if that div is empty? The content of the div will change based on user input. I would like the heading1 to appear immediately after a button click event. I am inclined towards a CSS solution, although the ...

Is there a syntax problem with the jQuery (this).next() statement that is causing it to not

Struggling with the implementation of a .next() selector. It seemed straightforward, but I must be missing something. Here's the current script that is not functioning as expected: $('.ITEM').hover(function (){ $(this).next('.ITEM ...

Conditions must fail for window.location to execute

<a href="www.google.com" class="social" id="social">Click here for Link one</a> <a href="www.cnn.com" class="social" id="social">Visit Link two now!</a> <a href="www.facebook.com" class="social" id="social">This is Link thre ...

Scroll dynamically to a div upon clicking and looping through its content

When the user clicks on the first paragraph with the class "targetgo", I want the page to scroll to the element with the class "size1". The same goes for the second paragraph with the class "size2" and so on. As I will have more than 25 similar instances ...

How to use JavaScript to remove line breaks from an h1 tag

Is there a way to remove the line break that occurs after an H1 element? Also, I am searching for a method to add a backspace functionality using JavaScript. ...

Parsing text files with Highcharts

As a beginner in JavaScript and HighCharts, I am facing a simple problem that has me completely lost. My goal is to generate a scatter chart with three lines by reading data from a text file formatted like this: x y1 y2 y3 1.02 1.00 6.70 ...

Guide on displaying a modal from a separate file onto my HTML page with Laravel and Ajax

In my project, I am utilizing Laravel and AJAX. Inside the index.blade.php file, I have the following code: <a onclick="addForm()" class="btn btn-success " style="float:right;">Tambah</a> Additionally, I have the following script: <script ...