The z-index property does not seem to be affecting the positioning of images and buttons

I'm working on a school project creating an educational website about pumas. I want to have a set of buttons that are hidden behind an image, and when the user clicks on the image, the buttons should be revealed. However, I am facing an issue with the z-index property as it is not allowing the buttons to go behind the image.

#image {
  width: 40%;
  border-radius: 20px;
  display: block;
  margin: auto;
  opacity: 1;
  transition-duration: 0.4s;
  cursor: pointer;
  border: 4px solid black;
  z-index: 10;
  /* set higher z-index value */
}

#image:hover {
  border: 4px solid white;
  width: 42.5%;
}

#image.clicked {
  opacity: 0;
  transition-duration: 1s;
  pointer-events: none;
}

.hidden {
  display: none;
}

h3 {
  text-align: center;
  margin-bottom: 60px;
}

.image-container {
  position: relative;
}

.buttons {
  position: absolute;
  margin: auto;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  justify-content: center;
  gap: 10px;
  z-index: 5;
  /* set lower z-index value */
  top: 50%;
}

button {
  padding: 10px 20px;
  border-radius: 20px;
  border: none;
  background-color: white;
  color: black;
  font-size: 16px;
  cursor: pointer;
}

button:hover {
  background-color: black;
  color: white;
}
<div class="image-container">
  <div class="buttons">
    <button>Button 1</button>
    <button>Button 2</button>
    <button>Button 3</button>
    <button>Button 4</button>
  </div>
  <img src="https://cdn.britannica.com/20/93520-050-3E663489/Puma.jpg" id="image" onclick="this.classList.add('clicked');">
</div>

I attempted to resolve the issue by adjusting the z-index values to separate the image and buttons further apart, but unfortunately, that did not provide a solution. I am currently unsure what steps to take next to address this problem.

Answer №1

Make sure that when using z-index, the elements you wish to manipulate have a position other than static.

For instance, by adding position: relative to your image in this scenario, you can achieve the desired effect.

Best of luck on your coding journey!

#image {
  width: 40%;
  border-radius: 20px;
  display: block;
  margin: auto;
  opacity: 1;
  transition-duration: 0.4s;
  cursor: pointer;
  border: 4px solid black;
  z-index: 10; /* set higher z-index value */
  position: relative;
}

#image:hover {
  border: 4px solid white;
  width: 42.5%;
}

#image.clicked {
  opacity: 0;
  transition-duration: 1s;
  pointer-events: none;
}

.hidden {
  display: none;
}

h3 {
  text-align: center;
  margin-bottom: 60px;
}

.image-container {
  position: relative;
}

.buttons {
  position: absolute;
  margin: auto;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  justify-content: center;
  gap: 10px;
  z-index: 5; /* set lower z-index value */
  top: 50%;
}

button {
  padding: 10px 20px;
  border-radius: 20px;
  border: none;
  background-color: white;
  color: black;
  font-size: 16px;
  cursor: pointer;
}

button:hover {
  background-color: black;
  color: white;
}
<div class="image-container">
  <div class="buttons">
    <button>Button 1</button>
    <button>Button 2</button>
    <button>Button 3</button>
    <button>Button 4</button>
  </div>
  <img src="https://cdn.britannica.com/20/93520-050-3E663489/Puma.jpg" id="image" onclick="this.classList.add('clicked');">
</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

What is the best way to implement nested iterations in Jade?

ul li a(href='') menu1 li a(href='') menu2 ul li a(href='') sub-menu2 li a(href='') menu3 ul li a(href=&apos ...

Determining if a component is nested within itself in Angular 6 is a crucial task

My goal is to develop a custom Angular component for a nested navigation menu with multiple levels. Below is an example of how the menu structure looks: app.component.html <nav-menu> <nav-menu-item>Section 1</nav-menu-item> <nav- ...

transfer a product attribute value to a different product attribute within the Magento platform

There is an attribute called Weight : Attribute Code: weight Scope: general Catalog Input Type for Store Owner: Text Field Values Required: yes Apply To: Simple Product, Bundle Product Allow HTML Tags on Frontend: yes Also, there is a General Weight attr ...

Verify the input in text fields and checkboxes using JavaScript

I'm in the process of creating a complete "validate-form" function, but there are still a couple of things missing: Ensuring that the Name field only allows alphabetical characters and white spaces Validating that at least one checkbox is selected, ...

Enhancing the Syntax of If and If Else in Jquery Functions

I'm struggling to simplify and optimize this block of code. Would appreciate any help in making it more efficient! var status = "closed"; $(document).on('click', '[data-toggle-nav]', function(event) { if ($(this) && status = ...

Problem with escaping special characters in random string HTML

As I was in the process of creating a JavaScript tool to generate random strings, with or without special characters, I stumbled upon an inspiring snippet that caught my attention: (): function randStr(len) { let s = ''; while (len--) s += ...

Step-by-step guide on clipping a path from an image and adjusting the brightness of the remaining unclipped area

Struggling to use clip-path to create a QR code scanner effect on an image. I've tried multiple approaches but can't seem to get it right. Here's what I'm aiming for: https://i.stack.imgur.com/UFcLQ.png I want to clip a square shape f ...

Issue with React-Native FlatList's scrolling functionality

Struggling with implementing a scrolling FlatList in React Native. Despite trying various solutions found on Stack Overflow, such as adjusting flex properties and wrapping elements in views, the list still refuses to scroll. Snippet of code (issue is with ...

There is no value inputted into the file

I'm facing a small issue while trying to retrieve the value from my input of type="file". Here is the code snippet: <tr ng-repeat="imagenDatos in tableImagenesPunto | filter: busquedaDatosPunto " > <td>PNG</td> <td>{{imag ...

Issue with binding background images to DIV elements in Angular 4 CSS

Here is a template example: <div [style.background-image]="profileImage" ></div> In the TypeScript file: We declare private profileImage: any; and use DomSanitizer for security. Fetching photo from service: We set this.profileImage using b ...

Remove Specific HTML Tags Using Free Pascal

Is it possible to exclude a specific HTML tag from a string? I am looking to remove tags that start with <img>. However, all the content within <img ...> should be deleted as well. This is necessary because I need to eliminate images from the ...

What is preventing jQuery 3 from recognizing the '#' symbol in an attribute selector?

After updating my application to jQuery 3, I encountered an issue during testing. Everything seemed to be working fine until I reached a section of my code that used a '#' symbol in a selector. The jQuery snippet causing the problem looks like th ...

"Adding dots" in a slideshow using HTML, CSS, and JS

I'm currently working on a slideshow that includes navigation dots at the bottom. Although the slideshow is functioning properly, I am encountering an issue where only one dot appears instead of the intended three in a row. Despite my research and att ...

In Angular, when a component is clicked, it is selecting entire arrays instead of just a single item at a

I am currently working on implementing a rating feature using Angular. This component will be used to rate different languages based on how proficient I am with them. My issue lies in the fact that when I click on one array element, it automatically selec ...

What could be causing spacing problems with fontawesome stars in Vue/Nuxt applications?

Currently, I am working on implementing a star rating system in Nuxt.js using fontawesome icons. However, I have encountered an issue where there is a strange whitespace separating two different stars when they are placed next to each other. For instance, ...

Issue with CSS: Hover effect causing unexpected additional white space

My main goal is to implement a hover effect that covers an entire section, but I'm facing some challenges. When I hover over my products, it doesn't behave as expected and adds extra white space below while not covering the section properly. Thi ...

form field with the ability to select multiple files

From my understanding, traditional html or javascript do not have the capability to upload multiple files (images) in a singular field. Please correct me if I am mistaken. Is there a way for this to be achieved with html5 or any other method aside from us ...

Using jQuery to encapsulate HTML within a div tag

I am looking to insert some HTML markup into my webpage using jQuery. I want to wrap the highlighted yellow code below with <div class="section">...</div>. Here is an example of what I need: <div class="section"> <div>...< ...

What is the method for saving background color and font size preferences in local storage using HTML?

I want to give visitors to my site the option to customize the background color and text size. Can their preferences be saved in local storage? If so, could you share some HTML code to make this possible? ...

Tips on implementing SCSS styles in my create-react-app application along with Material UI components?

I recently developed a React app using Create-React-App, and now I am looking to incorporate Material UI with styling through SCSS. https://i.sstatic.net/JLEjX.png To manage my styles, I created a main.css file using node-sass. While attempting to apply ...