"CSS 이미지 크기 비율에 맞게 줄이기"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-<source +<syntaxhighlight , -</source> +</syntaxhighlight>))
 
(사용자 10명의 중간 판 27개는 보이지 않습니다)
1번째 줄: 1번째 줄:
;How to resize image proportionally with CSS
;How to resize image proportionally with CSS
;CSS 이미지 크기 비율에 맞게 줄이기
;CSS 이미지 크기 비율에 맞게 줄이기
;CSS 종횡비 유지한 채로 이미지 크기 조정


==방법 1: 너비·높이 중 하나만 지정==
==방법 1: 너비·높이 중 하나만 지정==
*너비(width) 또는 높이(height) 둘 중 하나만 지정하면 나머지 하나는 비율에 맞게 줄어듬
*너비(width) 또는 높이(height) 둘 중 하나만 지정하면 나머지 하나는 비율에 맞게 줄어든다.
:( 만약 다른 CSS에 영향을 받는다면 나머지에 auto 명시 )
:( 만약 다른 CSS에 영향을 받는다면 나머지에 auto 명시 )
<source lang='css'>
<syntaxhighlight lang='html5' run outheight='200'>
.my-image {
    width: 150px;
    height: auto;
}
</source>
 
;예시
<source lang='html5'>
<style>
<style>
.small1 { width: 100px; }
.small1 { width: 100px; }
19번째 줄: 12번째 줄:
</style>
</style>


<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>
<img class='small1' src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>
<img class='small1' src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>
<img class='small2' src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>
<img class='small2' src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>
</source>
</syntaxhighlight>
<jsfiddle height='420'>e3g4n6vh</jsfiddle>


==방법 2: 너비·높이 중 둘 다 지정 #1==
==방법 2: 너비·높이 중 둘 다 지정 #1==
*너비·높이 둘 다 제한하면서 비율은 유지하고 싶은 경우
*너비·높이 둘 다 제한하면서 비율은 유지하고 싶은 경우
*너비·높이는 auto로 두고 max-width, max-height를 주어 크기 제한
*너비·높이는 auto로 두고 max-width, max-height를 주어 크기 제한
<source lang='html5'>
<syntaxhighlight lang='html5' run outheight='150'>
<style>
<style>
.max-small {
.max-small {
38번째 줄: 29번째 줄:
   
   
<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'>
<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'>
</source>
</syntaxhighlight>
<jsfiddle height='150'>ujk2dzzx</jsfiddle>


==방법 3: 너비·높이 중 둘 다 지정 #2==
==방법 3: 너비·높이 중 둘 다 지정 #2==
*배경이미지로 주고 <code>background-size: contain;</code>를 적용하는 방법
*배경이미지로 주고 <code>background-size: contain;</code>를 적용하는 방법
<source lang='html5'>
<syntaxhighlight lang='html5' run outheight='150'>
<style>
<style>
.bg-small {
.bg-small {
53번째 줄: 43번째 줄:


<div class='bg-small'></div>
<div class='bg-small'></div>
</source>
</syntaxhighlight>
<jsfiddle height='150'>xcLshxau</jsfiddle>


==같이 보기==
==같이 보기==
63번째 줄: 52번째 줄:
*[[CSS max-height 속성]]
*[[CSS max-height 속성]]
*[[CSS background-size 속성]]
*[[CSS background-size 속성]]
*[[CSS 이미지 비율유지, 최대크기로 crop]]


==참고 자료==
==참고==
*http://stackoverflow.com/questions/787839/resize-image-proportionally-with-css
*http://stackoverflow.com/questions/787839/resize-image-proportionally-with-css


[[분류: CSS]]
[[분류: CSS]]

2021년 4월 16일 (금) 12:30 기준 최신판

How to resize image proportionally with CSS
CSS 이미지 크기 비율에 맞게 줄이기
CSS 종횡비 유지한 채로 이미지 크기 조정

1 방법 1: 너비·높이 중 하나만 지정[ | ]

  • 너비(width) 또는 높이(height) 둘 중 하나만 지정하면 나머지 하나는 비율에 맞게 줄어든다.
( 만약 다른 CSS에 영향을 받는다면 나머지에 auto 명시 )
<style>
.small1 { width: 100px; }
.small2 { height: 100px; }
</style>

<img class='small1' src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>
<img class='small2' src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Albert_Einstein_%28Nobel%29.png/260px-Albert_Einstein_%28Nobel%29.png'>

2 방법 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'>

3 방법 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>

4 같이 보기[ | ]

5 참고[ | ]

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