【问题标题】:Center anchor with image in divdiv中带有图像的中心锚点
【发布时间】:2014-06-10 13:32:27
【问题描述】:

我有一个位于锚元素中的图像。

我希望锚点和图像在父 div 中垂直和水平居中。

Here 是一个例子:

<div class="wrapper">
    <a href="#" class="anchor">
        <img src="http://bit.ly/1mFH8AW"></img>
    </a>
</div>
.wrapper {
    background: black;
    width: 500px;
    height: 500px;
}
.anchor {
}
.anchor > img {
    height: 100px;
    width: 100px;
}

结果:

【问题讨论】:

    标签: image css alignment anchor


    【解决方案1】:

    position:relative 添加到您的包装器中,然后将图像的 CSS 更改为:

    .anchor > img {
        height: 100px;
        width: 100px;
        position:absolute;
        margin:auto;
        top:0;right:0;bottom:0;left:0;
    }
    

    jsFiddle example

    【讨论】:

    • 为什么margin auto而不是margin 0 auto
    • 因为0 auto 只会将其水平居中,而 OP 要求垂直和水平。 Auto 同时处理所有四个。
    猜你喜欢
    • 2010-09-15
    • 2013-11-29
    • 2016-02-03
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    相关资源
    最近更新 更多