【问题标题】:Can't figure out how to center image with margin: auto无法弄清楚如何用边距居中图像:自动
【发布时间】:2016-09-29 00:26:09
【问题描述】:

我检查了多个线程并尝试了多个选项。我尝试将显示设置为阻止,为图像和容器设置特定的宽度。我可能会错过任何其他条件吗?

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:

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

【问题讨论】:

    标签: html css margin centering


    【解决方案1】:

    删除position: absolute 并将margin: 0 auto 应用于img。当position: absolute被应用到某个元素上时,会从DOM的正常流程中取出来

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

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 2014-12-18
      • 2011-07-19
      • 1970-01-01
      • 2014-04-25
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多