multi divs centered



我有 2 个div,里面有 3 个div,所有 2 个都包裹在一个大div 上。 我尝试将其居中,但它不起作用。 这是我的代码:

<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>

边距: 0 自动; 宽度: 1210px;

你还有一个错别字"麦粒肿":)

你应该使用 CSS 类来提高效率。您还有两个带有id="left"的div。元素 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;

最新更新