1

I'm making an informational site about pumas for a school project. I'm trying to put a group of buttons that will lead to other pages behind the image so that when you click it the buttons are revealed. The problem is that the z-index doesn't allow 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 tried changing the z-index to put the image and buttons farther apart, but that didn't work so I don't know what to do.

j08691
  • 204,283
  • 31
  • 260
  • 272
ewph
  • 11
  • 1
  • https://developer.mozilla.org/en-US/docs/Web/CSS/z-index#formal_definition: _"Applies to: positioned elements"_ – CBroe Mar 09 '23 at 14:06

1 Answers1

2

When working with z-index, you must always make sure that the elements you want to manipulate has a position other than static.

In this example, I applied position: relative to your image and that will do the trick.

Good luck!

#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>
j08691
  • 204,283
  • 31
  • 260
  • 272
Jake
  • 393
  • 1
  • 11