如何缩进DIV?

Joh*_*ith 15 html css indentation

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

mpe*_*pen 27

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

  • 为了帮助响应式网页设计,我使用百分比而不是像素。左边距:5% (2认同)

Dan*_*tik 7

<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>
Run Code Online (Sandbox Code Playgroud)