(CSS) How position text (with background color) over <img> tag without absolute positioning

落花浮王杯 提交于 2019-12-31 04:03:33

问题


As the title says

my code is something like this :

<div class=container> <img/> <div>some text with line one, line two , line three </div> </div>

the container should have overflow:hidden and my text would be in more than one line , so I need only a small part of my text to appear at the bottom of container, so when user hovers, the full text appears.

I want to position text over img WITHOUT absolute positioning. I tried negative margins, but text wouldn't have BG color there. Also tried Relative pos. => works great but not on chance on IE.

here is an image of what I want


回答1:


I see no reason not to use position: absolute.

See: http://jsfiddle.net/NeaR4/

CSS:

.container {
    border: 2px dashed #444;
    float: left;
    position: relative
}
.container img {
    display: block
}
.container > div {
    position: absolute;
    left: 0;
    bottom: 0;
    right: 0;
    height: 14px;
    background: #000;
    background: rgba(0,0,0,0.7);
    color: #fff;
    overflow: hidden;
    font: bold 14px Arial, sans-serif;
    padding: 5px;
}
.container:hover > div {
    height: auto
}

HTML:

<div class="container">
    <img src="http://dummyimage.com/230x180/f0f/fff" />
    <div>some text with line oneeee, line twoooooooo ooooooo , line three</div>
</div>



回答2:


position:relative solves your problem



来源:https://stackoverflow.com/questions/5998676/css-how-position-text-with-background-color-over-img-tag-without-absolute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!