2011-03-17 99 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>