Z-index在图像和按钮上无效。

huangapple go评论61阅读模式
英文:

Z-index isn't working for images and buttons

问题

我正在制作一个关于美洲狮的信息网站,用于学校项目。我试图在图像后面放置一组按钮,当您单击图像时,按钮会显示出来。问题在于z-index不允许按钮置于图像后面。

#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; /* 设置更高的z-index值 */
}

#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; /* 设置较低的z-index值 */
  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>

我尝试更改z-index以将图像和按钮分开,但这并没有起作用,所以我不知道该怎么办。

英文:

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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#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;
}

<!-- language: lang-html -->

&lt;div class=&quot;image-container&quot;&gt;
  &lt;div class=&quot;buttons&quot;&gt;
    &lt;button&gt;Button 1&lt;/button&gt;
    &lt;button&gt;Button 2&lt;/button&gt;
    &lt;button&gt;Button 3&lt;/button&gt;
    &lt;button&gt;Button 4&lt;/button&gt;
  &lt;/div&gt;
  &lt;img src=&quot;https://cdn.britannica.com/20/93520-050-3E663489/Puma.jpg&quot; id=&quot;image&quot; onclick=&quot;this.classList.add(&#39;clicked&#39;);&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

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.

答案1

得分: 2

在使用 z-index 时,您必须始终确保要操作的元素具有 position 属性,而不是 static

在这个示例中,我将 position: relative 应用于您的图像,这将起作用。

祝好运!

#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; /* 设置更高的 z-index 值 */
  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; /* 设置较低的 z-index 值 */
  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>
英文:

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!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#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;
}

<!-- language: lang-html -->

&lt;div class=&quot;image-container&quot;&gt;
  &lt;div class=&quot;buttons&quot;&gt;
    &lt;button&gt;Button 1&lt;/button&gt;
    &lt;button&gt;Button 2&lt;/button&gt;
    &lt;button&gt;Button 3&lt;/button&gt;
    &lt;button&gt;Button 4&lt;/button&gt;
  &lt;/div&gt;
  &lt;img src=&quot;https://cdn.britannica.com/20/93520-050-3E663489/Puma.jpg&quot; id=&quot;image&quot; onclick=&quot;this.classList.add(&#39;clicked&#39;);&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 22:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定