简体   繁体   English

如何使div与CSS匹配图像高度

[英]How to make div to match image height with CSS

Following js fiddle http://jsfiddle.net/9pn8Z/5/ contains element I'm currently working on. 以下js小提琴http://jsfiddle.net/9pn8Z/5/包含我目前正在处理的元素。 This is part of page layout, it will be duplicated with diffrent images. 这是页面布局的一部分,它将与不同的图像重复。 While width of image is fixed, height varies and i got problem with that. 虽然图像的宽度是固定的,但高度变化,我遇到了问题。 Element will come in one of few widths, height would vary depending on size of source image. 元素将具有几个宽度中的一个,高度将根据源图像的大小而变化。

My goal is to make height of rt_tileoverlay div, that slides on when image is hovered same as height of image. 我的目标是使rt_tileoverlay div的高度在图像悬停时与图像高度相同时滑动。 I would like to avoid javascript and just stick to css 我想避免使用javascript而只是坚持使用css

Here is My code: 这是我的代码:

<div class="rt_tilewrap">
    <a href="www.google.com">
        <div class="rt_imgwrap">
            <div class="rt_tileoverlay"></div>
            <img class="rt_timg" src="http://i.imgur.com/cepjcsGb.jpg" /> 
         </div>
    </a>
    <div class="rt_tiletext">
        <p>wards are focused oni wards are focused oni
        wards ards are focused oni wards areds are focused oni
        wards are focused oni wards are focused oni wards are
        focused oni wards are focu wards are focused i .</p>
    </div>
</div>


.rt_tilewrap {
    padding: 10px;
    display: inline-block;
    text-align: left;
    vertical-align: top;
    background: rgba( 22, 255, 22, 0.3);
    width:355px;
}

.rt_tiletext {
    padding: 5px 0 5px 0;
    color: #666666;
    float:left;
    line-height: 1.1em;
    text-align: justify;
    display: block;
    background:skyblue;
}

.rt_tiletext p{
    padding: 0;
    margin: 0;
}

.rt_timg {
    width: 100%;
    height: auto;
    display:block;
    position: relative;
    float:left;
}

.rt_imgwrap{
    overflow:hidden;
}

.rt_tileoverlay {
    height: 100%;
    width: 0;
    color:white;
    overflow:hidden;
    position: absolute;
    background-color: rgba(255, 255,0,  0.5);
    transition: all 0.4s ease-in-out;
    position: margin-top: 0px;
    margin-left: 0px;
    -webkit-transition: all .4s ease;
    -moz-transition: all .4s ease;
    -ms-transition: all .4s ease;
    -o-transition: all .4s ease;
    transition: all .4s ease;
    z-index: 100;
}

.rt_imgwrap:hover > .rt_tileoverlay {
     width:355px;
}

Thanks in adavnce for your suggestions for how to solve it. 非常感谢你提出如何解决它的建议。

You need to set the relative parent of the rt_tileoverlay to take the full height: 您需要将rt_tileoverlay的相对父级设置为采用完整高度:

.rt_imgwrap{
    position:relative;
    overflow:hidden;
}

The demo http://jsfiddle.net/9pn8Z/6/ 演示http://jsfiddle.net/9pn8Z/6/

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

相关问题 CSS:如何使div与高度受限的图像大小相同 - CSS: How to make a div the same size as a height-constrained image 如何使div匹配包含td的高度? - How to make div match the height of the containing td? 如何仅使用CSS使父div的宽度等于子图像的宽度,并且使子图像的高度等于父div的高度 - How to make the width of parent div equal to the width of child image, and height of child image to height of the parent div using CSS only 如何将 flexbox div 内的 div 高度与另一个 CSS 匹配 - How to match height of div inside a flexbox div with another CSS 如何调整div高度以匹配其中图像的高度 - How to resize div height to match height of image within it 如何获取图像高度并将该高度赋予图像 div 以使其具有响应性 - How to get image height and give that height to image div to make it responsive 如何使div视口高度,而不是CSS中的页面高度? - how make div viewport height, not page height in CSS? HTML CSS:如何使Div框高度等于文本高度? - HTML CSS: How to Make Div Box Height Equal Text Height? CSS:如何在不设置固定高度的情况下使背景图像出现在div中 - CSS: How to make a background-image appear within a div without setting a fixed height 如何使用CSS将图像调整到屏幕高度? - How to make an image resize to the screens height with CSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM