编辑:在这里,使用你的 jsfiddle:http: //jsfiddle.net/kfbjd/7/
正如其他人评论的那样,固定或绝对位置会将其从页面流中删除,因此您将无法让其他 div 将自己定位在它周围。如果你做了相对位置,然后使用 css 'display: block' 样式,它可能会让你更接近你正在寻找的东西。
http://www.w3schools.com/cssref/pr_class_display.asp
更新以包含来自 JSFiddle 的解决方案的全文,包括水平居中,以供后代使用:
<style>
#main{
background-color:#000;
width:900px;
margin-top:149px;
position:relative;
margin-left: auto;
margin-right: auto;
opacity:0.9;
z-index:2;
box-shadow: 0px 0px 20px #000;
-moz-box-shadow: 0px 0px 20px #000;
-webkit-box-shadow: 0px 0px 20px #000;
}
.maintext{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
font-size:14px;
color:#6b6b6b;
padding:25px;
text-align:left;
-webkit-touch-callout: text;
-webkit-user-select: text;
-khtml-user-select: text;
-moz-user-select: text;
-ms-user-select: text;
user-select: text;
}
#about{
background-color:#000;
width:900px;
/*height:450px; */
margin-top:20px;
margin-left: auto;
margin-right: auto;
position:relative;
opacity:0.9;
z-index:2;
box-shadow: 0px 0px 20px #000;
-moz-box-shadow: 0px 0px 20px #000;
-webkit-box-shadow: 0px 0px 20px #000;
}
</style>
<div id="main">
<div class="maintext">Welcome to my personal portfolio. Here you can find projects I have worked on, and hire me for freelance work. If you like my work and want to hire me for projects please check out my software skills <a href="about.html">here</a> and <a href="contact.html">contact</a> me to discuss about projects.Welcome to my personal portfolio.
<div style="text-align:center;"><br/><img src="images/green.png" border="0" style="vertical-align:middle; margin-bottom:3px;"/>Available for freelance work</div></div>
</div>
<div id="about">
<div class="maintext">ABOUT TEXT</div>