简体   繁体   English

将鼠标悬停在图像上以使其透明并显示文本

[英]Hover over image making it transparent and showing text

Hey I'm working on a portfolio page (just using HTML and CSS) and I created a grid of images. 嘿,我正在制作一个投资组合页面(仅使用HTML和CSS),并创建了一个图像网格。 My plan is that the one image that is hovered over gets transparent (0.2 transparency or so) and a description text appears at this field. 我的计划是,将一张悬停在上面的图像变得透明(0.2透明左右),并在此字段中显示说明文字。

I created following code, but this makes my description text transparent as well. 我创建了以下代码,但这也使我的描述文本也变得透明。 How can I get the text to stay fully visible? 如何使文字保持完全可见?

HTML: HTML:

<div id="imagelist">
    <a href="">
        <img src="img/img1.jpg" width="200px" height="200px" alt="Image 1" />
        <p class="imgtext">This is the description text.
        </p>
    </a>
</div>

Can you help me with the CSS part? 您可以在CSS部分帮助我吗?

When the div is in a hover state target the Image element to change opacity and change the display property for the description. 当div处于悬停状态时,Image元素将更改不透明度并更改描述的显示属性。

 #imagelist:hover img { opacity: .2; } #imagelist:hover .imgtext { display: block; } .imgtext {display: none;} 
 <div id="imagelist"> <a href=""> <img src="img/img1.jpg" width="200px" height="200px" alt="Image 1"/> <p class="imgtext">This is the description text. </p> </a> </div> 

Do you mean the text should appear behind the transparent picture? 您是说文字应该出现在透明图片的后面吗? Also, are all thumbnails 200px by 200px? 另外,所有缩略图都是200px x 200px吗?

If yes, you could do it like this: 如果是,则可以这样:

 #imagelist a { width: 200px; height: 200px; position: relative; display: block; } #imagelist img { position: absolute; left: 0; top: 0; bottom: 100%; height: 100%; z-index: 1; } #imagelist img:hover { opacity: 0.2; } 
 <div id="imagelist"> <a href=""> <img src="http://s0.geograph.org.uk/geophotos/03/97/06/3970600_94c29e78.jpg" /> <p class="imgtext">This is the description text. </p> </a> </div> 

I just worked on it 我只是在做

Made some changes try it out 进行一些更改尝试一下

<style>
            #imagelist a {position: relative;display: inline-block;}
            #imagelist a p{display: none;}
            #imagelist a:hover p{position: absolute;width: 100%;height: 100%;background: #000;opacity: 0.8;top: 0;left: 0;margin: 0;display: block;}
            #imagelist a p span{display: inline-table;width: 100%;height: 100%;text-align: center;color:#fff;}
            #imagelist a p span span{display: table-cell;vertical-align: middle;}
        </style>


<div id="imagelist">
            <a href="">
                <img src="img/img1.jpg" width="200px" height="200px" alt="Image 1" />
                <p class="imgtext">
                <span>
                    <span>This is the description text.</span>
                </span>
                </p>
            </a>
        </div>

It looks quite simple just added some few extra tags with it. 看起来很简单,只是添加了一些额外的标签。 Try it out and let me know the feedback 试试看,让我知道反馈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM