html - 将鼠标悬停在图像上并在文本上缩放图像

标签 html css

我试图在悬停时放大图像,效果很好。但是当我在图像上添加文本时,它会在底部显示一些间隙。我试过了,但这个差距根本没有消除

我尝试了 margin,padding,但是没有用

.img-hover-zoom {
    height: auto;
    overflow: hidden;
    position: relative;
    display: inline-block;
}

.img-hover-zoom img {
    transition: transform .5s ease;

}

.img-hover-zoom:hover img {
    transform: scale(1.1);

}


.img-hover-zoom .text {
    position: absolute;
    padding-top: 15px;
    padding-bottom: 15px;
    left: 0px;
    bottom: 0px;
    margin: 0px auto;
    text-align: center;
    background: rgba(0, 0, 0, 0.5);
    font-family: Quicksand;
    color: #fff;
    width: 100%;

}
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <div class="img-hover-zoom">
        <img src="https://www.dike.lib.ia.us/images/sample-1.jpg" alt="Nature" style="width:100%;">
        <p class="text">Nature<br />What a beautiful sunrise</p>
    </div>
</body>

</html>

最佳答案

只需将 display: block 添加到您的 .img-hover-zoom img

  .img-hover-zoom img {
      display: block;
    }

<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    .img-hover-zoom {
      height: auto;
      overflow: hidden;
      position: relative;
      display: inline-block;
    }
    
    .img-hover-zoom img {
      transition: transform .5s ease;
      display: block;
    }
    
    .img-hover-zoom:hover img {
      transform: scale(1.1);
    }
    
    .img-hover-zoom .text {
      position: absolute;
      padding-top: 15px;
      padding-bottom: 15px;
      left: 0px;
      bottom: 0px;
      margin: 0px auto;
      text-align: center;
      background: rgba(0, 0, 0, 0.5);
      font-family: Quicksand;
      color: #fff;
      width: 100%;
    }
  </style>
</head>

<body>


  <div class="img-hover-zoom">
    <img src="https://www.dike.lib.ia.us/images/sample-1.jpg" alt="Nature" style="width:100%;">
    <p class="text">Nature<br/>What a beautiful sunrise</p>
  </div>

</body>

</html>

关于html - 将鼠标悬停在图像上并在文本上缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56864299/

相关文章:

javascript - 花哨的自定义输入字段

javascript - 导入 Bootstrap 后文本框似乎闪烁

javascript - 页面加载时滑动 slider slickGoTo

javascript - 一旦溢出 slider 达到特定点,禁用左键点击器

javascript - 如何在 Bootstrap 中使用左右箭头按钮创建图像 slider

javascript - 分隔段落列表并为每个单词提供单独的 CSS

html - 背景图片未覆盖整个页面

javascript - 如何访问具有相同类名的标签并使用 javascript 处理 onClick?

javascript - $(window).load(function()) 和 NProgress JQuery 插件。页面已完全加载,但 NProgress 仍在运行

javascript - 在使用 bootstrap 时,如何使用 javascript 将 div 设置为其他两个 div 的高度