2015-05-04 73 views
0

我需要在另一個容器div的內部放置另一個長方形div的矩形div。最裏面的div內部的圖像必須相對於最外面的div水平和垂直居中。我怎麼做?在另一個div的中心放置一個div

<div class="col-sm-4 col-sm-offset-1" style="margin-right:10px;"> 
    <div class="panel panel-default" style="min-height:320px;"> 
    <div class="panel-body" style="padding:0;">    
     <img src=""> 
    </div> 
    <div> 
</div> 
+0

您可以添加更多的div作爲需要的,但有一些沿着這條線? http://jsbin.com/qocude/2/edit?html,css,output – Waymond

+0

我應該提到最外面的div是兩個現有的列之一,所以我不能混淆它的顯示屬性,因爲它會干擾與另一列。 col-sm-4 col-sm-offset-1是Bootstrap框架的一部分,所以我堅持讓它保持原樣。 – Dave

回答

0

#div1{ 
 
    border:1px solid; 
 
    width:200px; 
 
    height:200px; 
 
} 
 

 
#div2{ 
 
    border:1px solid; 
 
    width:100px; 
 
    height:100px; 
 
    margin:25%; 
 
} 
 

 
#div3{ 
 
    border:1px solid; 
 
    width:50px; 
 
    height:50px; 
 
    margin:25%; 
 
}
<div id="div1"> 
 
    <div id="div2"> 
 
     <div id="div3"></div> 
 

 
    </div> 
 
</div>

確保所有的div都寬度和高度設置,然後使用:

margin: 25%; 

在內部的div

+0

這隻會設置水平居中。我需要水平和垂直居中的圖像。 – Dave

+0

嗨,山姆。如果你和CSS一起查看[code](http://jsbin.com/gesohavure/1/edit?html,css,output),可能會更容易些?請注意,IMG元素實際上是由javascript動態生成的,因此只能指定最小尺寸。 – Dave

+0

當前[code](http://jsbin.com/rodukejoro/1/edit?html,css,output) – Dave