简体   繁体   English

无法弄清楚如何使用边距将图像居中:自动

[英]Can't figure out how to center image with margin: auto

I've checked multiple threads and have tried multiple options. 我检查了多个线程,并尝试了多个选项。 I've tried setting display to block, setting specific width for both image and container. 我尝试将显示设置为块,为图像和容器设置特定的宽度。 Any other condition that I might be missing out on? 还有其他条件我可能会错过吗?

HTML: HTML:

<footer>
    <div id="footercontent">
        <div id="logobox">
            <img src="images/logo.png" />   <--- THIS IS THE IMAGE IN QUESTION
        </div>
        <div id="social">

        </div>
    </div>
</footer>

CSS: CSS:

footer {
    width: 100%;
    height: 200px;
    background-color: white;
    position: relative;
    margin-top: 70px;
}
#footercontent {
    width: 70%;
    height: 100%;
    background-color: black;
    margin: auto;
}
#logobox {
    width: 30%;
    height: 100%;
    margin-right: 0;
    float: left;
}
img {
    height: 70%;
    position: absolute;
    margin: auto;
    display: block;
}
#social {
    width: 70%;
    height: 100%;
    background-color: white;
    float: left;
}

Remove position: absolute and apply margin: 0 auto to img . 移除position: absolute并将margin: 0 auto应用于img When position: absolute is applied on some element, it is taken out from the normal flow of DOM position: absolute应用于某些元素时,它从DOM的正常流程中取出

img {
    height: 70%;
    margin: 0 auto;
    display: block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM