如何实现底部对齐浮动div,使其大小适合其容器
我怎样才能实现以下布局?特别是 Image
和 DIV
的定位,
我发现除非我为 Div 设置特定的宽度,否则它只会继续到下一行并占据整个空间容器的宽度。另外,将其相对于图像底部对齐也给我带来了麻烦。目前它们都是 float:left
编辑:到目前为止,这两个解决方案都有效图像的宽度是恒定的,我想我可以使用它,但它会出现在作者个人资料页面的 WordPress 主题中,并且图像的宽度可能会略有变化。是否有一种解决方案可以让 Div 位于图像旁边(减去填充),无论图像有多宽或多窄?基本上让 div 调整其宽度以适应图像宽度。
How can I achieve the following layout? Specifically the positioning of Image
and DIV
I've found that unless I set a specific width for the Div, it will just go on to the next line and take up the full width of the container. Additionally aligning it relative to the bottom of the image is giving me trouble. Currently they're both float:left
Edit: The two solutions so far work if the image is a constant width which I guess I could work with, but it's going in a Wordpress theme for an author's profile page and it's possible that images would have slightly variable widths. Is there a solution that would have the Div right next to the image (minus padding) regardless of how wide or narrow the image is? Basically having the div adjust its width to accommodate the image width.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 IE7/8、Firefox、Chrome 中测试。
现场演示#2
CSS :
JS/jQuery:
(您可能需要更改
parseInt($('#top img').css(' 的
)+ 10
右边距'), 10)HTML:
Tested in IE7/8, Firefox, Chrome.
Live Demo #2
CSS:
JS/jQuery:
(you might want to change the
+ 10
forparseInt($('#top img').css('margin-right'), 10)
)HTML:
我将标题图像和标题 div 放入其自己的容器中,并使用绝对定位将项目放置在其中。
我在这里整理了一个快速示例: http://jsfiddle.net/JjxYj/1/
这里请注意,如果删除标题中 Div 的宽度,它将变成其内容的宽度。
更新
要回答问题的更新部分,这里有另一个解决方案,该解决方案允许图像具有任意宽度,同时仍将标题文本放置在其包含项目的底部:http://jsfiddle.net/JjxYj/5/
I'd put the header image and header div inside its own container and position the items within it using absolute positioning.
I've put together a quick sample here: http://jsfiddle.net/JjxYj/1/
Notice here that if you remove the width of the Div in the header, it will become the width of its content.
Update
To answer the updated part of the question, here's another solution that'll allow the image to be of any width whilst still positioning the header text at the bottom of its containing item: http://jsfiddle.net/JjxYj/5/