将两个 div 置于 div 中:一个固定宽度,另一个可变宽度/高度
我遇到的情况是,我有一个固定宽度的 div
,包含从 Twitter 中提取的图像,以及另一个包含可变长度的用户文本的可变宽度的 div
。我想要实现的目标如下:
我可以用单个 div
具有 background-image
和 padding-left
。但我希望能够将 border-radius
应用于 img 元素,而这对于 background-image
来说是不可能的。
如果我在外部 div
上执行 text-align: center
,它就会到达一半。 这是一个演示和屏幕截图:
但这显然不完全是我想要的。
我怎样才能做到这一点?
I have a situation where I have one div
of fixed width, containing an image pulled from Twitter, and another div
of variable width containing user text of variable length. What I want to achieve is something like the following:
I can do this well enough with a single div
that has background-image
and padding-left
. But I want to be able to apply border-radius
to the img element, which simply won't be possible with a background-image
.
If I do text-align: center
on the outer div
, it gets me halfway there. Here's a DEMO and a screenshot:
But this obviously isn't fully what I want.
How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
询问,您将收到 - 一个简化的 jsFiddle 示例:
作为额外的好处,文本也垂直居中!
HTML:
CSS:
Ask and you shall receive — a simplified jsFiddle example:
As an added bonus, the text is vertically centered too!
HTML:
CSS:
会是这样的吗?
http://jsfiddle.net/uPPTM/6/
Would it be something like this?
http://jsfiddle.net/uPPTM/6/
您可以将图像容器(或没有容器的图像本身)向左浮动,清除左侧的所有内容...然后将文本浮动到左侧,清除右侧的所有内容。
您可以使用边距调整文本的距离。
You can float the image container (or image itself without the container) to the left, clearing anything the left... and then float the text to the left, clearing anything to the right.
You can adjust the distance of the text using margins.
使用左侧位置的绝对定位将标题文本推过图像。
http://jsfiddle.net/uPPTM/9/
Use absolute positioning with a left position to push the title text past the image.
http://jsfiddle.net/uPPTM/9/