编辑:我没有意识到你的网站,直到你的编辑。您应该在.date-contact
风格中使用position: absolute
。所以,我推荐的代码在这里不适用。但是你可以从我希望的解释中受益。
首先,您不能使用margin: 0 auto
和position: absolute
。在独立的css文件中使用类,而不是使用内联样式,总是可以帮助您清楚地看到代码。有了这种分离的担忧,你也会在代码中应用DRY原理。
我整理了您的代码以提供您想要的效果。请看,如果你有问题,请开火。将尽我所能提供帮助。
HTML
<div class="outer-div">
<div class="date-contact">proba</div>
</div>
CSS
.date-contact {
width:300px;
height:150px;
background:blue;
margin: 0 auto;
}
.outer-div {
text-align:center;
margin:100px auto;
padding:0 10%;
}
新的答案EDITTED问题
其他的答案说,你应该绝对定位你的蓝格。我说如果你这样做你永远不会让它显示在中心。这样做的简单方法是将你的蓝色div放在绝对定位的另一个div中。你的蓝色div将显示在中心,就像你想要的margin: 0 auto;
另外,我把你的蓝色div放在div#huge_it_google_map1
里面,因为我相信它是它的所在。
HTML
<div class="yourMapDiv">
<div class="outer-div" id="huge_it_google_map1">
<div class="date-contact">proba</div>
<!-- Your other divs and map contents inside the div#huge_it_google_map1 -->
</div>
</div>
CSS
.yourMapDiv {
position: relative;
background-color: yellow;
padding: 10px;
}
.outer-div {
position: absolute;
top:0;
left:0;
width:100%;
text-align:center;
background-color:red; /* Remove this attribute to see your map div (yellow) */
}
.date-contact {
background-color:blue;
width:300px;
/*height:150px;*/
margin: 0 auto;
/* or "margin: 50px auto 0" if you like to give a little margin-top for 50px */
}}
为了您的方便,这是工作fiddle。 我希望你能达到你想要的。
需要设置左边和顶部财产.date接触CSS类 –