多divs居中
问题描述:
我有2个divs里面有3个div,所有2个围绕一个大div。我尝试和集中它,它不工作。这里是我的代码:多divs居中
<div stye="margin-left:auto; margin-right:auto; width: 1210px;">
<div id="left" style=" float:left; width: 606px; height: 506px;">
<div style="top:0px; float:left; width:300px; border: 2px solid #FF0; height:502px; text-align:center; font-family: Myriad Pro, Arial, sans-serif; color:#FFF;"><h1>A picture of a cool guy wearing our shirt will go in here.</h1></div>
<div style="top:0px; float:left; width:300px; border: 2px solid #FF0; border-left:0px; height:200px; text-align:center; font-family: Myriad Pro, Arial, sans-serif; color:#FFF;"><h1>A product image will go in here.</h1></div>
<div style="top:0px; float:left; width:300px; border: 2px solid #FF0;border-top:0px; border-left:0px; height:300px; text-align:center; font-family: Myriad Pro, Arial, sans-serif; color:#FFF;"><h1>A tight product image will go in here.</h1></div>
</div>
<div id="left" style="float:left; width: 604px; height: 506px;">
<div style="top:0px; float:right; width:300px; border: 2px solid #FF0; border-left:0px; height:502px; text-align:center; font-family: Myriad Pro, Arial, sans-serif; color:#FFF;"><h1>A picture of a cool guy wearing our shirt will go in here.</h1></div>
<div style="top:0px; float:left; width:300px; border: 2px solid #FF0; border-left:0px; height:200px; text-align:center; font-family: Myriad Pro, Arial, sans-serif; color:#FFF;"><h1>A product image will go in here.</h1></div>
<div style="top:0px; float:left; width:300px; border: 2px solid #FF0;border-top:0px; border-left:0px; height:300px; text-align:center; font-family: Myriad Pro, Arial, sans-serif; color:#FFF;"><h1>A tight product image will go in here.</h1></div>
</div>
</div>
答
margin:0 auto;宽度:1210px;
你也有一个错字“麦粒肿” :)
答
您应该使用CSS类更有效率。你也有两个div id="left"
。元素ID是独特的,所以你应该使用类。
<style type="text/css">
.myclass {
top:0px; float:left; width:300px; border: 2px solid #FF0; height:502px; text-align:center; font-family: Myriad Pro, Arial, sans-serif; color:#FFF;
}
</style>
<!-- this div gets all style from .myclass definition -->
<div class="myclass">content</div>
而且居中的div,新增的风格,你是对的margin: auto;
+0
+1推荐课程。内联CSS副本是丑陋的,非常糟糕的做法! – Bojangles 2012-04-07 17:56:20
AW胡说。愚蠢的错别字! – user1319385 2012-04-07 17:31:14
:)你也应该接受@William Van Rensselaer的建议和定义类。内联CSS不是一个健康的方法。 – Radu 2012-04-07 17:33:00