2011-03-17 35 views
13

我有一個div居中對齊,其保證金設置爲0 auto。現在我想從中間縮進它的左邊距50個像素。但是,每當我嘗試這樣做時,它都會將div與其容器的左側對齊,並失去中心對齊。我認爲這是因爲它覆蓋了保證金屬性的左側字段。有誰知道如何做到這一點?爲了澄清,我想從容器的中心縮進50個額外的像素。如何縮進DIV?

+0

顯示一些代碼請 – sandeep 2011-03-17 06:14:02

+0

我認爲你需要用另一個容器包裝所有內容。 – JohnP 2011-03-17 06:17:39

回答

24

將它包裹在另一個div中。使外層div margin: 0 auto;和內DIV margin-left: 50px

+1

爲了幫助響應式網頁設計,我使用百分比而不是像素。 margin-left:5% – mmv1219 2015-06-19 17:09:48

+1

如果div沒有指定寬度,即使margin設置爲px,它也會響應 – 2016-01-15 17:36:09

0

包裹你div的另一個div。然後居中對齊新的div,然後空白左邊或填充留下您的原始div。

3

嘗試

padding-left: 50px; 

,如果這不足以保持填充和廣告裏更大的彼此股利。

5
<html> 
<head> 
    <title>Test</title> 
    <style> 
     #outer { 
      margin: 0 auto; /*center*/ 
      width:200px; 
      background-color:red; /*just to display the example*/ 
     } 

     #inner { 
      /*move the whole container 50px to the left side*/ 
      margin-left:-50px; 
      margin-right:50px; 
      /*or move the whole container 50px to the right side*/ 
      /* 
      margin-left:50px; 
      margin-right:-50px; 
      */ 
     } 
    </style> 
</head> 
<body> 
<div id="outer"> 
    <div id="inner"> 
     This is the result! 
    </div> 
</div> 
</body> 
</html>