html - “悬停图像显示图像”CSS 无法正常工作

标签 html css image hover display

我在 Internet 上搜索并找到了这个:How to display image over image on hover with css .
我尝试了 this image 的代码在 jsfiddle 和博客文章中使用 Chrome 和 Firefox。
他们都没有显示像 this image 这样的结果.
我的代码中有什么不正确的地方?谢谢。

a {
  position: relative;
  display: inline-block;
}

a:hover:before {
  content: '';
  display: block;
  background-image:url('http://i.imgur.com/fGAbTOj.png');
  width: 50px;
  height: 50px;
  position: absolute;
  top: 0;
  left: 0;
}
<a href="#"><img src="http://cdn.akamai.steamstatic.com/steam/apps/271590/ss_80b965d66b13d6eb5e1468151a371e12fe159663.600x338.jpg" /></a>

最佳答案

使用 background-size 属性,因为您正在将 background-image 属性设置为 a:before 元素和背景的大小图片是 256x256。但是您已经将 a:before 元素的 widthheight 定义为 50px,因此背景图像也应为 50px

只需使用:

a:hover:before {
  background-size: 50px; /* as you have 'width' and 'height' defined as 50px each */
}

看看下面的片段:

a {
  position: relative;
  display: inline-block;
}

a:hover:before {
  content: '';
  display: block;
  background-image:url('http://i.imgur.com/fGAbTOj.png');
  width: 50px;
  height: 50px;
  background-size: 50px;
  position: absolute;
  top: 0;
  left: 0;
}
<a href="#"><img src="http://cdn.akamai.steamstatic.com/steam/apps/271590/ss_80b965d66b13d6eb5e1468151a371e12fe159663.600x338.jpg" /></a>

希望这对您有所帮助!

关于html - “悬停图像显示图像”CSS 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40826037/

相关文章:

php - file_get_contents() 的条件语句

html - (HTML) 如何让两张图片并排对齐并居中放置在页面上?

html - Flexbox:水平和垂直居中

html - 将整个 SVG 完美地夹在容器 div 中?

html - 我如何划分此处附加的图像部分以支持 Bootstrap ?

php - 如何在帖子中显示相册? [仅使用 PHP 和 MYSQL]

javascript - 删除非 SVG slider

html - 需要一行中的文本,溢出隐藏在 LI 元素中

html - 嵌入用户输入的 YouTube 视频

html - 卡上的单击事件未在克隆元素 owl-carousel 2 上触发