如何使图像仅在我悬停在图像上时出现

Per*_*gue 1 html css mouseover hover

我希望仅在将鼠标悬停在图像上时才显示图像,然后尝试将其放在样式部分中,但该图像不起作用。这个代码正确吗?

<style type=“text/css”>
img{opacity: 0;}
img:hover{opacity: 1;}
</style>
Run Code Online (Sandbox Code Playgroud)

Der*_*ple 8

You are using the wrong type of quotation marks.

Change the “text/css” to "text/css".

Incorrect

<style type=“text/css”>
  img {
    opacity: 0;
  }

  img:hover {
    opacity: 1;
  }
</style>
Run Code Online (Sandbox Code Playgroud)

Correct

<style type="text/css">
  img {
    opacity: 0;
  }

  img:hover {
    opacity: 1;
  }
</style>
Run Code Online (Sandbox Code Playgroud)

  • 我不知道你是怎么发现的。荣誉 (2认同)
  • 很多年前,当我复制粘贴一些代码示例时,我就碰到过这种情况,该代码在样式标签外部起作用,但在样式标签之外起作用。这是一个幸运的猜测。 (2认同)