CSS 이미지 크기 비율에 맞게 줄이기

Jmnote bot (토론 | 기여)님의 2021년 4월 13일 (화) 23:30 판 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
How to resize image proportionally with CSS
CSS 이미지 크기 비율에 맞게 줄이기
CSS 종횡비 유지한 채로 이미지 크기 조정

1 방법 2: 너비·높이 중 둘 다 지정 #1

  • 너비·높이 둘 다 제한하면서 비율은 유지하고 싶은 경우
  • 너비·높이는 auto로 두고 max-width, max-height를 주어 크기 제한
<style>
.max-small {
    width: auto; height: auto;
    max-width: 100px;
    max-height: 100px;
}
</style>
 
<img class='max-small' src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>

2 방법 3: 너비·높이 중 둘 다 지정 #2

  • 배경이미지로 주고 background-size: contain;를 적용하는 방법
<style>
.bg-small {
    width: 100px; height: 100px;
    background: url('https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png') no-repeat;
    background-size: contain;
}
</style>

<div class='bg-small'></div>

3 같이 보기

4 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}