给定以下HTML:
< div id = "容器" > <!——这里的其他元素——> < div id =“版权”> 版权所有Foo网页设计 < / div > < / div >
我想把#copyright贴在#container的底部。我能在不使用绝对定位的情况下实现这一点吗?
给定以下HTML:
< div id = "容器" > <!——这里的其他元素——> < div id =“版权”> 版权所有Foo网页设计 < / div > < / div >
我想把#copyright贴在#container的底部。我能在不使用绝对定位的情况下实现这一点吗?
当前回答
也许这对某些人有帮助:你可以总是把一个div放在另一个div的外面,然后用负边距把它往上推:
<div id="container" style="background-color: #ccc; padding-bottom: 30px;">
Hello!
</div>
<div id="copyright" style="margin-top: -20px;">
Copyright Foo web designs
</div>
其他回答
显示:w3schools.com
有位置的元素:绝对的;定位相对于 最近的定位祖先(而不是相对于 视口,像固定)。
所以你需要将父元素定位为相对或绝对元素,等等,并将所需元素定位为绝对元素,然后将bottom设置为0。
灵活的方法!
在受支持的浏览器中,您可以使用以下方法:
例子
.parent {
display: flex;
flex-direction: column;
}
.child {
margin-top: auto;
}
.parent { 身高:100 px; 边框:5px实体#000; 显示:flex; flex-direction:列; } .child { 高度:40像素; 宽度:100%; 背景:# f00; margin-top:汽车; } < div class = "父" > <div class="child">对齐到底部</div> < / div >
上面的解决方案可能更灵活,但是,这里有一个替代方案:
例子
.parent {
display: flex;
}
.child {
align-self: flex-end;
}
.parent { 身高:100 px; 边框:5px实体#000; 显示:flex; } .child { 高度:40像素; 宽度:100%; 背景:# f00; align-self: flex-end; } < div class = "父" > <div class="child">对齐到底部</div> < / div >
作为旁注,您可能希望添加供应商前缀以获得额外支持。
试试这个;
<div id="container">
<div style="height: 100%; border:1px solid #ff0000;">
<!-- Other elements here -->
</div>
</div>
<div id="copyright" style="position:relative;border:1px solid #00ff00;top:-25px">
Copyright Foo web designs
</div>
如果您使用内联块元素的文本对齐特性知道#容器的高度,您确实可以在不使用position:absolute的情况下将方框对齐到底部。
在这里你可以看到它的行动。
这是代码:
#container {
/* So the #container most have a fixed height */
height: 300px;
line-height: 300px;
background:Red;
}
#container > * {
/* Restore Line height to Normal */
line-height: 1.2em;
}
#copyright {
display:inline-block;
vertical-align:bottom;
width:100%; /* Let it be a block */
background:green;
}
对于容器中只有一个子div的容器,可以使用表格单元格和垂直对齐方法,这种方法可以可靠地将单个div定位在父div的底部。
注意,使用table-footer-group作为前面提到的其他答案将破坏父表的高度计算。
#{容器 显示:表; 宽度:100%; 身高:200 px; } #{项 显示:表格单元; vertical-align:底部; } < div id = "容器" > <div id="item">单个底部项目</div> < / div >