尝试使用自动边距 - 左/右:自动放置左侧图像

时间:2012-09-03 00:40:39

标签: css

使用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;
    }

1 个答案:

答案 0 :(得分:0)

您的图片将css设置为绝对位置。 使用此属性,您的“徽标”将相对于div“main_header”定位,因为它设置为position:relative。 这就是为什么保证金不起作用的原因。位置:绝对只有“左”,“顶”,其他可以工作。

要使用保证金,您应该删除位置属性。

相关问题