如何实现底部对齐浮动div,使其大小适合其容器

发布于 2024-10-11 17:27:16 字数 391 浏览 2 评论 0原文

我怎样才能实现以下布局?特别是 ImageDIV 的定位,

我发现除非我为 Div 设置特定的宽度,否则它只会继续到下一行并占据整个空间容器的宽度。另外,将其相对于图像底部对齐也给我带来了麻烦。目前它们都是 float:left

alt text

编辑:到目前为止,这两个解决方案都有效图像的宽度是恒定的,我想我可以使用它,但它会出现在作者个人资料页面的 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

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2024-10-18 17:27:16

在 IE7/8、Firefox、Chrome 中测试。

现场演示#2

CSS :

#container{width:80%; padding:12px; margin:0 auto}
#top{position:relative;overflow:auto}
#top img{float:left; background:red; width:100px; height:180px}
#header{position:absolute; bottom:0; right:0}
#content{height:200px}

JS/jQuery:

$('#header').css('margin-left', $('#top img').width() + 10);

(您可能需要更改 parseInt($('#top img').css(' 的 + 10右边距'), 10))

HTML:

<div id="container">
    <div id="top">
        <img src="" />
        <div id="header">Some text here that should wrap to fit on row. Some text here that should wrap to fit on row. Some text here that should wrap to fit on row. Some text here that should wrap to fit on row. </div>
    </div>
    <div id="content">dfgdfg</div>
</div>

Tested in IE7/8, Firefox, Chrome.

Live Demo #2

CSS:

#container{width:80%; padding:12px; margin:0 auto}
#top{position:relative;overflow:auto}
#top img{float:left; background:red; width:100px; height:180px}
#header{position:absolute; bottom:0; right:0}
#content{height:200px}

JS/jQuery:

$('#header').css('margin-left', $('#top img').width() + 10);

(you might want to change the + 10 for parseInt($('#top img').css('margin-right'), 10))

HTML:

<div id="container">
    <div id="top">
        <img src="" />
        <div id="header">Some text here that should wrap to fit on row. Some text here that should wrap to fit on row. Some text here that should wrap to fit on row. Some text here that should wrap to fit on row. </div>
    </div>
    <div id="content">dfgdfg</div>
</div>
残疾 2024-10-18 17:27:16

我将标题图像和标题 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文