简体   繁体   English

图像悬停时的文字叠加-自适应

[英]Text Overlay on Image Hover - Responsive

I am using this tutorial to create an overlay on my images with text: 我正在使用本教程在带有文字的图像上创建覆盖图:

http://codepen.io/pdelsignore/pen/uqenH http://codepen.io/pdelsignore/pen/uqenH

It works great, however I have a responsive website and if I try to enter a % as the width / height of the '.box' the image disappears. 效果很好,但是我有一个响应式网站,如果我尝试输入%作为“ .box”的宽度/高度,则图像会消失。 It appears it can only be a fixed with (ie px) which obviously doesn't scale. 看来它只能是(px)固定的,显然不能缩放。

.box {  
cursor: pointer;  
height: 250px;   
position: relative;  
overflow: hidden;  
width: 400px;
font-family: 'Open Sans', sans-serif;
}  

Does anyone have any ideas? 有人有什么想法吗? Thanks in advance! 提前致谢!

Try giving min-width and min-height a try. 尝试尝试min-widthmin-height

min-width: 100%;
min-height: 100%;

In live project usually we use any responsive framework. 在实际项目中,我们通常使用任何响应式框架。 Like bootstrap or foundation. 如引导程序或基础。 So I think you could ignore as framework will handle this properly. 因此,我认为您可以忽略框架,因为框架可以正确处理此问题。 No need to use any % to make it responsive. 无需使用任何%使其响应。 For Bootstrap we use 对于Bootstrap,我们使用

 <div class="row">
    <div class="col-md-4">
        <div class="box">  
            <img src="http://files.room1design.com/oldcity.jpg"/>  
            <div class="overbox">
                <div class="title overtext">
                    Walk This Way
                </div>
                <div class="tagline overtext">
                    Follow the path of stone, a road towards an ancient past
                </div>
            </div>
        </div>  <!-- End box -->
    </div>  <!-- End Col-4 -->
 </div>  <!-- End row -->

I believe the dimensions of .box as a percentage would be based on the height of the parent. 我相信.box的尺寸(以百分比表示)将基于父级的高度。 since no height is specified on the body it has no frame of reference. 由于没有在车身上指定高度,因此没有参考系。 try adding the following to get percentages working on .box. 尝试添加以下内容以使百分比在.box上正常运行。

html, body {
    height:100%;
}

here is an updated codepen with a few other changes to illustrate the use of percentages after giving your body dimension. 这是经过更新的代码笔,并进行了其他一些更改,以说明在给定身体尺寸后使用百分比的方式。

http://codepen.io/anon/pen/dPREBE http://codepen.io/anon/pen/dPREBE

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

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