Html 将一个 div 定位在另一个 div 的底部附近
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/857916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Positioning a div near bottom side of another div
提问by Roman
I have outer div and inner div. I need to place inner div at the bottom of the outer one.
我有外部 div 和内部 div。我需要将内部 div 放在外部 div 的底部。
Outer div is elastic (width: 70% for example). I also need to center inner block.
外部 div 是弹性的(例如宽度:70%)。我还需要将内部块居中。
Simple model of described make-up is shown on the picture below:
描述的化妆的简单模型如下图所示:
采纳答案by RichieHindle
Tested and working on Firefox 3, Chrome 1, and IE 6, 7 and 8:
在 Firefox 3、Chrome 1 和 IE 6、7 和 8 上测试和运行:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><body>
<div style='background-color: yellow; width: 70%;
height: 100px; position: relative;'>
Outer
<div style='background-color: green;
position: absolute; left: 0; width: 100%; bottom: 0;'>
<div style='background-color: magenta; width: 100px;
height: 30px; margin: 0 auto; text-align: center'>
Inner
</div>
</div>
</div>
</body>
</html>
Live version here: http://jsfiddle.net/RichieHindle/CresX/
回答by Magnar
You need a wrapping div for the bottom one, in order to center it.
您需要一个包装底部的 div,以便将其居中。
<style>
/* making it look like your nice illustration */
#outer { width: 300px; height: 200px; background: #f2f2cc; border: 1px solid #c0c0c0; }
#inner { width: 50px; height: 40px; background: #ff0080; border: 1px solid #800000; }
/* positioning the boxes correctly */
#outer { position: relative; }
#wrapper { position: absolute; bottom: 3px; width: 100%; }
#inner { margin: 0 auto; }
</style>
<div id="outer">
<div id="wrapper"><div id="inner"></div></div>
</div>
回答by Manoj Kumar
CSS3 Flexbox
allows the bottom positioning very easily. Check the Flexbox support table
CSS3Flexbox
允许非常容易的底部定位。检查Flexbox 支持表
HTML
HTML
<div class="outer">
<div class="inner">
</div>
</div>
CSS
CSS
.outer {
display: flex;
justify-content: center; /* Center content inside */
}
.inner {
align-self: flex-end; /* At the bottom of the parent */
}
Output:
输出:
.outer {
background: #F2F2CD;
display: flex;
width: 70%;
height: 200px;
border: 2px solid #C2C2C3;
justify-content: center;
}
.inner {
background: #FF0081;
width: 75px;
height: 50px;
border: 2px solid #810000;
align-self: flex-end;
}
<div class="outer">
<div class="inner">
</div>
</div>
回答by Dmitri Farkov
Works well on all browsers including ie6.
适用于所有浏览器,包括 ie6。
<style>
#outer{
width: 70%;
background-color: #F2F2CC;
border: 1px solid #C0C0C0;
height: 500px;
position: relative;
text-align: center;
}
#inner{
background-color: #FF0080;
border: 1px solid black;
width: 30px;
height: 20px;
/* Position at the bottom */
position: relative;
top: 95%;
/* Center */
margin: 0 auto;
text-align: left;
}
</style>
<div id="outer">
<div id="inner">
</div>
</div>