如何缩进DIV?

时间:2011-03-17 06:11:35

标签: css html indentation

我的div中心对齐,其边距设置为0 auto。现在我想将其左边距从中间缩进50像素。但是,每当我尝试这样做时,它会将div与其容器的左侧对齐并失去中心对齐。我认为这是因为它会覆盖margin属性的左侧字段。有谁知道如何做到这一点?为了澄清,我想从容器中心缩进50个额外的像素。

4 个答案:

答案 0 :(得分:26)

将它包装在另一个div中。制作外部div margin: 0 auto;和内部div margin-left: 50px

答案 1 :(得分:6)

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

答案 2 :(得分:3)

padding-left: 50px;

如果这还不够,请保留填充并在较大的内部添加另一个div。

答案 3 :(得分:0)

围绕你的div包裹另一个div。然后居中对齐新div,然后左边距或填充左边原始div。