当悬停大于图像时,图像上显示的文字?

时间:2017-06-25 02:01:23

标签: html css

我可以在将图像保持居中的同时将鼠标悬停在图像上并通过How to show text on image when hovering?对缩放做出响应时显示说明。然而,当我试图向外扩展时,描述开始逃脱它应该覆盖的图像的边界。

这里是负责上述代码的代码。欢迎任何建议。

HTML

<div class= "img__wrap">
    <img src="Capture.JPG" alt= "Website"/>
    <p class= "img__description">The website you're on!</p>
</div>

CSS

 img {
    display: block;
    margin: 0 auto;
    max-width: 100%;
    height: auto;
}

.img__wrap {
    position: relative;
    max-width: 100%;
    height: auto;
}

.img__description {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: #544f50;
    color: #fff;
    visibility: hidden;
    opacity: 0;
    padding: 10%;
    transition: opacity .2s, visibility .2s;
}

.img__wrap:hover .img__description {
    visibility: visible;
    opacity: 0.75;
}

1 个答案:

答案 0 :(得分:0)

  

box-sizing:border-box使用margin:0.img__description

.img__wrap {
    position: relative;
    height: auto;
}

 img {
    display: block;
    margin: 0 auto;
    max-width: 100%;
    height: auto;
}

.img__description {
    position: absolute;
    width: 100%;
    height: 100%;
    bottom: 0;
    left: 0;
    right: 0;
    text-align: center;
    box-sizing: border-box;
    background: #544f50;
    color: #fff;
    visibility: hidden;
    opacity: 0;
    padding: 10%;
    transition: opacity .2s, visibility .2s;
    margin: 0;
}

.img__wrap:hover .img__description {
    visibility: visible;
    opacity: 0.75;
}
<div class= "img__wrap">
    <img src="http://www.mrwallpaper.com/wallpapers/cute-bunny-1600x900.jpg" alt= "Website"/>
    <p class= "img__description">The website you're on!</p>
</div>