Center anchor with image in div

不打扰是莪最后的温柔 提交于 2019-12-04 14:19:07

问题


I have an image that's in an anchor element.

I want the anchor an the image to be centered both vertically and horizontally in the parent div.

Here's an example:

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

Result:


回答1:


Add position:relative to your wrapper and then change the image's CSS to:

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

jsFiddle example



来源:https://stackoverflow.com/questions/23282890/center-anchor-with-image-in-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!