【发布时间】:2015-06-10 09:20:07
【问题描述】:
我有一个包含样式的 div。我需要这个 div 来环绕图像和文本内容,而不会占用不必要的空间。
当我只有一张图片时,这很容易:http://codepen.io/anon/pen/YXQyzr
.cont {
text-align: center;
}
.cont2 {
display: inline-block;
border: 3px solid grey;
padding: 10px;
background: blue;
}
<div class="cont">
<div class="cont2">
<div class="image">
<img width="400" src="http://www.psdgraphics.com/file/colorful-triangles-background.jpg" />
</div>
</div>
</div>
但是当我有文本时,文本会扩展以占据整个宽度:http://codepen.io/anon/pen/WvOvqL
<div class="cont">
<div class="cont2">
<div class="image">
<img width="400" src="http://www.psdgraphics.com/file/colorful-triangles-background.jpg" />
</div>
<div class="text">
<p>Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text </p>
</div>
</div>
</div>
我的布局是响应式的,内容是动态的,图像的大小(决定容器的大小)会有所不同。因此,我无法在任何东西上设置固定宽度。
【问题讨论】:
-
我不太明白你需要什么。你不能只浮动你的img吗?
-
我想要文本上方的图像,这与浮动通常实现的效果相反?
-
您想将文本限制在 img 的边界内?