如何将内部div与外部div底部对齐?

5

我需要将内部div与外部div的底部对齐。

我的代码如下:

<div class="myOuterDiv">
    <div class="div1 floatLeft"> Variable content here </div>
    <div class="div2 floatRight"> <img class="myButton" src="" /> </div>
</div>

'div1'的内容可能会变化,从而导致外部div的高度发生变化。 我该如何确保我的按钮(div2)始终保持在外部div的底部?

我更喜欢使用CSS,但如果不行,我可以使用jQuery。

更新

我选择用jQuery的方式来解决这个问题,但我遇到了一些问题。现在问题已经解决,你可以在这里找到我的可行解决方案:如何在跨浏览器使用jQuery计算元素的高度?


所以你有左列和右列,右列的按钮需要在底部,不管左侧列的长度,对吗? - ryanulit
是的,没错。我可能甚至不需要在按钮周围加上DIV(?)。 - Steven
4个回答

12

试试这个:

.myOuterDiv { position: relative; }
.div2 { position: absolute; bottom: 0 }

将某个元素设置为绝对定位会将其从文档流中移除;你可以通过向 .myOuterDiv 添加与 .div2 高度相等的底部填充来解决这个问题。


如果outerdiv是静态的,那么这个方法可能有效。但是在页面上有多个高度不同的outer divs。在我提问之前,我尝试过类似的方法,但它并没有起作用。 - Steven
我接受这个解决方案,因为它是最常用的一个。 - Steven

1

Mike的解决方案在所有浏览器中都表现良好。这是我使用他的想法编写的代码,以达到你想要的效果:

<html>
<head runat="server">
    <title></title>
    <style type="text/css">
        #wrapper
        {           
            overflow: auto;
            position: relative;
        }        
        #left
        {
            float: left;
            width: 300px;
            margin-right: 10px;
        }
        #right
        {
            width: 200px;
            float: left;
        }
        #bottom
        {
            position: absolute;
            bottom: 0;
        }
    </style>
</head>
<body>
    <div id="wrapper">
        <div id="left">
            Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
        </div>
        <div id="right">
            <div id="bottom"><input type="button" value="Testing"  /></div>
        </div>
    </div>
</body>
</html>

0

你的评论意味着你只会在 div 中放置图片,没有其他内容。如果是这样的话,你可以将其设置为底部对齐的背景图片:

<style>
.myOuterDiv {
    background: transparent url("myButton.png") no-repeat right bottom;
}
</style>

<div class="myOuterDiv">
    <div class="div1 floatLeft"> Variable content here </div>
</div>

如果你想要的话,可以使用 left bottom 或者 center bottom 代替 right bottom。你可能也不需要内部的 div


0

我想提供帮助,但我现在正在使用手机。不过这只是一个使用jQuery的提示。

获取两个div的高度值。将它们的高度值相减。 现在,使用结果的值来控制右侧div的margin-top,以将其向下推。

如果我回到我的桌面或笔记本电脑上,我会再次查看此内容。


不知道为什么你被踩了,但这是我目前正在测试的方式,通过创建自己的jQuery插件。还不太完美,但已经接近了。 - Steven
已经过去了十几年了。你有没有机会登陆你的台式机或笔记本电脑? :) - WinEunuuchs2Unix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接