给定以下HTML:
< div id = "容器" > <!——这里的其他元素——> < div id =“版权”> 版权所有Foo网页设计 < / div > < / div >
我想把#copyright贴在#container的底部。我能在不使用绝对定位的情况下实现这一点吗?
给定以下HTML:
< div id = "容器" > <!——这里的其他元素——> < div id =“版权”> 版权所有Foo网页设计 < / div > < / div >
我想把#copyright贴在#container的底部。我能在不使用绝对定位的情况下实现这一点吗?
当前回答
你可以通过为顶部的内容分配可用空间来使用网格:
#{容器 显示:网格; Grid-template-rows: 1fr auto; 高度:10眼动;/*或100%或任何*/ } < div id = "容器" > 这是随机内容。 < div id =“版权”> 版权所有Foo网页设计 < / div > < / div >
其他回答
针对此特定场景的解决方案:
将inner放在父元素的底部。父节点的高度由它的“最高”兄弟节点的高度设置
设置:
我有一行与多个<div class="容器"> 这些<div class="container">在另一个<div class="supercontainer">中彼此相邻 每个<div class="container">都有3个内部div相互重叠:<div class="title">, <div class="content">, <div class="footer">
期望的结果:
所有<div class="container">的高度相同。高度没有在px中定义,它将是其中“最高”的高度。 <div class="title">应该在<div class="container">的顶部 <div class="content">应该放在<div class="title">下面 <div class="footer">应该放在<div class="container">的底部,而不与前面的内容重叠
这是当前状态:https://codepen.io/xavier-atero/pen/ExvWQww
.supercontainer { border: solid 1px black; display: flex; } .container, .other-container { position: relative; border: solid 1px red; width: 200px; margin: 10px; } .title { margin: 10px; border: solid 1px blue; } .content { margin: 10px; border: solid 1px cyan; } .footer { margin: 10px; background: lime; } <body> <div class="supercontainer"> <div class="container"> <div class="title"> This part represents the title and it is placed on top </div> <div class="content"> This one represents the body and it is placed below the title </div> <div class="footer"> And this one is the footer. It should always be at the bottom of the container </div> </div> <div class="other-container"> <div class="title"> This part represents the title and it is placed on top. </div> <div class="content"> This one represents the body and it is placed below the title. This one is longer than the first one to stretch the parent div. Since it is longer, the footers of the two containers are not alinged. </div> <div class="footer"> And this one is the footer. It should always be at the bottom of the container </div> </div> </div> </body>
__________解决方案与FLEXBOX __________
这是结果:https://codepen.io/xavier-atero/pen/MWvpBMz
.supercontainer { border: solid 1px black; display: flex; } .container, .other-container { position: relative; border: solid 1px red; width: 200px; margin: 10px; display: flex; flex-direction: column; } .title { margin: 10px; border: solid 1px blue; } .content { margin: 10px; border: solid 1px cyan; } .footer { margin: 10px; background: lime; margin-top: auto; border: solid 1px fuchsia; } <body> <div class="supercontainer"> <div class="container"> <div class="title"> This part represents the title and it is placed on top </div> <div class="content"> This one represents the body and it is placed below the title </div> <div class="footer"> And this one is the footer. It should always be at the bottom of the container </div> </div> <div class="other-container"> <div class="title"> This part represents the title and it is placed on top. </div> <div class="content"> This one represents the body and it is placed below the title. This one is longer than the first one to stretch the parent div. Since it is longer, the footers of the two containers are not alinged. </div> <div class="footer"> And this one is the footer. It should always be at the bottom of the container </div> </div> </div> </body>
__________带有TABLE-ROW __________的解决方案
这是结果:https://codepen.io/xavier-atero/pen/rNzyKJm
.supercontainer { border: solid 1px black; display: flex; } .container, .other-container { position: relative; border: solid 1px red; width: 200px; margin: 10px; border-collapse:collapse; display : table; } .title { margin: 10px; border: solid 1px blue; } .content { margin: 10px; border: solid 1px cyan; } .footer { margin: 10px; background: lime; border: solid 1px fuchsia; display: table-row; vertical-align: bottom; height: 1px; } <body> <div class="supercontainer"> <div class="container"> <div class="title"> This part represents the title and it is placed on top </div> <div class="content"> This one represents the body and it is placed below the title </div> <div class="footer"> And this one is the footer. It should always be at the bottom of the container </div> </div> <div class="other-container"> <div class="title"> This part represents the title and it is placed on top. </div> <div class="content"> This one represents the body and it is placed below the title. This one is longer than the first one to stretch the parent div. Since it is longer, the footers of the two containers are not alinged. </div> <div class="footer"> And this one is the footer. It should always be at the bottom of the container </div> </div> </div> </body>
CSS中没有所谓的float:bottom。最好的方法是在这种情况下使用定位:
position:absolute;
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 >
作为旁注,您可能希望添加供应商前缀以获得额外支持。
正因为这一点根本没有被提及,在你这种情况下通常有效的方法是:
将copyright-div放在container-div之后
您只需要以与其他容器类似的方式格式化copyright-div(相同的整体宽度、居中等),就可以了。
CSS:
#container, #copyright {
width: 1000px;
margin:0 auto;
}
HTML:
<div id="container">
<!-- Other elements here -->
</div>
<div id="copyright">
Copyright Foo web designs
</div>
唯一不理想的情况是当container-div声明高度为:100%时,用户需要向下滚动以查看版权。但即使如此,你仍然可以修改(例如margin-top:-20px -当你的版权元素的高度是20px)。
没有绝对定位 没有表格布局 没有疯狂的css,其他浏览器看起来都不一样(至少IE是这样) 简单明了的格式
旁白:我知道OP要求的解决方案是“……粘在“容器”div的底部…”,而不是下面的东西,但拜托,人们在这里寻找好的解决方案,这就是一个!
代码依赖链接在这里。
html, body { width: 100%; height: 100%; } .overlay { min-height: 100%; position: relative; } .container { width: 900px; position: relative; padding-bottom: 50px; } .height { width: 900px; height: 50px; } .footer { width: 900px; height: 50px; position: absolute; bottom: 0; } <div class="overlay"> <div class="container"> <div class="height"> content </div> </div> <div class="footer"> footer </div> </div>