这是我的挑战:我有一个不动产的图像,将以200像素宽的DIV随机加载。因此,图像将被调整为200像素宽度和可变高度(保持成比例)。我有四个单独的标题需要进入图像:
它们需要分别位于图片的顶部中心,左下角,左下角和右下角。所以我会有类似的东西:
--------------------------------
| Title |
| |
| |
|Price |
|Location Surface|
--------------------------------
我知道我可以通过将div放在相对位置并将文本放在绝对位置来将文本叠加到图像上,但我似乎只能为下面的所有文本div设置一个位置,无论我有多少类使用
有什么建议吗?
编辑:找到解决方案。作为参考,这是生成的代码:<div id="stampRandom">
<img class="stampImg" src="myphoto.jpg">
<div class="title"><span>Title</span></div>
<div class="prix"><span>Location</span><span><br><b>Price</b></span></div>
<div class="surf"><span><b>Surface</span></b></span></div>
</div>
CSS:
#stampRandom {
position: relative;
top: 0px;
left: 0px;
width: 200px;
color: #FFF;
font-size: 80%;
}
#stampRandom {
position: relative;
top: 0;
left: 0;
width:200px;
color: #ffffff;
/* text-shadow: 1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0 #000; */
font-size:80%;
}
#stampRandom img {
top:0px;
left:0px;
width:200px;
}
#stampRandom div {
position: absolute;
width: 200px;
}
#stampRandom div span {
background: rgb(0, 0, 0); /* fallback color */
background: rgba(50, 50, 50, 0.7);
}
.title {
top:2px;
width:100%;
text-align:center;
}
.prix {
bottom:0px;
left:0px;
}
.surf {
bottom:0px;
right:0px;
text-align:right;
}
感谢所有帮助过的人!我被封锁了很长时间,我的代码需要重大的重做。手臂上的新鲜射击!
答案 0 :(得分:1)
<style>
.img-cont{position:relative;width:200px;}
.img-cont div{position:absolute; background:red}
.img-title{top:0px;width:100%;text-align:center;}
.img-price{bottom:0px; left:0px;}
.img-surf{bottom:0px; right:0px;text-align:right;}
</style>
<div class="img-cont">
<img src="/MyImage.jpg" width="200"/>
<div class="img-title">
Title
</div>
<div class="img-price">
Price Location
</div>
<div class="img-surf">
Surface
</div>
</div>
认为这会对你有所帮助
答案 1 :(得分:0)
我快速举例说明了如何实现这一目标。它并不完美,但它确实有效。如果您需要其他帮助或不同的方法让我知道
http://jsfiddle.net/hg9r7/134/
<div class="imageWrapper">
<img src="http://dummyimage.com/250x200" alt="1" width="250" height="200" />
<div class="top">TOP</div>
<div class="bot">BOT</div>
<div class="botleft">BOTLEFT</div>
<div class="botright">BOTRIGHT</div>
</div>
底部和左下角文字的CSS示例:
.bot{
position:absolute;
background:red;
text-align:center;
margin:0 auto;
width:100px;
left:50%;
margin-left:-50px;
bottom:0;
}
.botleft{
position:absolute;
background:red;
text-align:center;
margin:0 auto;
width:100px;
left:0;
bottom:0;
}