2014-04-25 4 views
2

앵커 요소에 이미지가 있습니다.div의 이미지가있는 가운데 앵커

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

결과 :

enter image description here

답변

4

래퍼에 position:relative를 추가 한 다음에 이미지의 CSS를 변경 :

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

jsFiddle example

+0

왜 마진은 자동 여백이 아닌지 0 자동 –

+0

왜 '0 자동'은 가로로만 놓고 OP는 세로 및 가로 모두를 요구하기 때문입니까? 자동은 동시에 4 개 모두를 처리합니다. – j08691

관련 문제