0

使用 left: 明确定位我的图像正确放置它,但是当我删除 left: 并用 margin-left: auto 和 margin-right: auto 替换它时,它被定位在左侧。我做错了什么?

<body>
    <div id = "main_header" href="http://xxx" >
        <img id = "logo" src="logo.png"/>
        <h1 id = "main_title">Title</h1>
    </div>


    body {
        position:relative;
        background-image: url("large_background.png");
        font-family: Helvetica; 
        margin: 0;              /* Amount of negative space around the outside of the body */
        padding: 0;             /* Amount of negative space around the inside of the body */
    }

    #main_header {
        position: relative;
    }


    #logo {
        position:absolute;
        top: 5px;
        left: 140px;
        height: 50px;
        width: 50px;
    }
4

1 回答 1

0

您的图像将 css 设置为绝对位置。使用此属性,您的“logo”将相对于 div“main_header”定位,因为它设置为 position:relative。这就是边距不起作用的原因。使用 position:absolute 只有 "left"、"top" 和其他的会起作用。

要使用 Margin,您应该删除 position 属性。

于 2012-09-03T00:51:35.690 回答