Udacity - Responsive Images
image๋ web์์ ์ฝ 62% ์ฐจ์งํ๋ค. ์ด๋ฏธ์ง์ ์ฌ์ด์ฆ๋ ๋งค์ฐ ๋ค์ํ๋ค. ๊ธฐ๊ณ๊ฐ ๋ค์ํ๋งํผ ์ด๋ฏธ์ง ํฌ๊ธฐ๋ ๊ฑฐ๊ฐ์ ๋ง์ถฐ์ ธ์ผํ๋ค.
"images consume more than 60% of the bytes that cross the web." This isn't strictly true: images on average consume more than 60% of the bytes to open a web page, but most of the bytes that "cross the web" are for video.
Units, Formats, Environments
Total bits = pixels X bits per pixel less pixels * better compression = less bytes
์ด๋ป๊ฒ ํ์ผํฌ๊ธฐ๊ฐ ๋์ด๋ ๋ ํ๋ฆฌํฐ๋ฅผ ์ ์งํ ๊น?
ํ๋ฉด์ด ์ค์ด๋ค๋ ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ฅผwidth: 100%
๋ก ํด์ค์ผ๋ก์จ ๊ฐ๋ ค์ง๋๊ฒ์ ํด๊ฒฐํ ์ ์๋ค. ํ์ง๋ง ํ๋ฉดํฌ๊ธฐ๋ฅผ ํค์ฐ๋ฉด ์ด๋ฏธ์ง์ ํ๋ฆฌํฐ๊ฐ ๊นจ์ง๋ค.
max-width: 100%
๋ฅผ ํ๋ฉด natural width๋งํผ ์ปค์ง๊ณ ๊ทธ์ด์์ ์์ปค์ง๋ค.
calc()
CSS function can be used anywhere a , , , , , or is required.width: calc(100% - 80px); # 100% ๋๋น์์ 80%๋งํผ ๋บ ๋๋น
์ฐ์ฐ์ ๋ฐ๋์ ์ฌ์น์ฐ์ฐ +,-๊ธฐํธ ์์ชฝ์๋ ๊ณต๋ฐฑ์ ์ฝ์ ํด์ผํ๋ค.
Landscape and Portrait
: ํ๋ฉด์ ๊ฐ๋ก๋ก ํ์๋๋ ๊ด์ฐฎ์๋ฐ ์ธ๋ก๋ก ํ์๋ ์ด๋ฏธ์ง๊ฐ ๊ฐ๋ ค์ง๋ ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
Did you notice how setting both the height and the width to 100vmax or 100vmin changes the image's aspect ratio? It'll compress your images to squares, so be careful if you want to maintain a different aspect ratio! vmin
๊ณผ vmax
๋ฅผ ์ด์ฉํด์ ํ๋ฉดํฌ๊ธฐ์ ๋ง๊ฒ ์ด๋ฏธ์ง๊ฐ ๋ฐ๋๋๋ก ํ ์ ์๋ค.
Raster and Vertor
Raster image(png,jpeg)๋ ์ผ๋ฐ์ ์ธ ์นด๋ฉ๋ผ, ์ค์บ๋๋ก ์ฐ์ ์ด๋ฏธ์ง์ด๋ฉฐ, Vector ์ด๋ฏธ์ง(svg)๋ ๋ก๊ณ ์๊ฐ์ด ์ผ๋ฌ์คํธ๋ ์ดํธ๋ก ๋ง๋ ์ด๋ฏธ์ง์ด๋ค.
๋๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ๋น๊ตํด๋ดค์๋ png๋ ํ๋ฉด์ ํฌ๊ธฐ๋ฅผ ํค์ฐ๋ฉด ์ด๋ฏธ์ง๊ฐ ์ฝ๊ฐ ๊นจ์ง๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.svg๋ ์ด๋ ์ฌ์ด์ฆ์์๋ perfectํ๋ค. jpeg๋ณด๋ค svg๊ฐ ์ฌ์ด์ฆ๋ ๋ ์์ ๊ฒ์ ํ์ธํ ์ ์๋ค. png๋ ์ต๋ํ ํผํ๊ณ , svg๋ฅผ ์ต๋ํ ์ฌ์ฉํด๋ผ.
Grunt & ImageMagick & ImageOptim
์ด๋ฏธ์งํฌ๊ธฐ์ ํ์ ์ ๋ค์ํํด์ฃผ๋ฉฐ, ์ด๋ฏธ์ง๋ก๋ฉ์ ๋น ๋ฅด๊ฒ ํด์ค๋ค. http://gruntjs.com/ https://imageoptim.com/mac http://www.imagemagick.org/script/index.php
Grunt๊ฐ ์์ ์ข์! grunt๋ก pagespeed๋ ํ์ธํด๋ณผ ์ ์๋ค.
Html figure tag
:figure์์๋ ์ผ๋ฌ์คํธ, ๋ค์ด์ด๊ทธ๋จ, ์ฌ์ง, ์ฝ๋๋ฑ์ ์ฃผ์์ ๋ค๋ ์ฉ๋๋ก ์ฌ์ฉ๋ฉ๋๋ค.
์ด ์์๊ฐ ์ ๊ฑฐ ๋๋๋ผ๋ ๋ฌธ์์ ์ฃผ๋ ํ๋ฆ์๋ ํฐ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.
figure์์๋ figcaption ์์๋ฅผ ํฌํจํ ์ ์์ผ๋ฉฐ * figure์์์์์ ๋งจ์์ด๋ ๋งจ ๋ง์ง๋ง์๋ง ์ฌ ์ ์์ต๋๋ค. ๋จ, figcaption์์๊ฐ ํ์๋ ์๋๋๋ค.
Images with Markup
Text
ํ ์คํธ๋ฅผ ์ฌ์ง์์ ์จ์ jpeg๋ก ์ ์ฅํ๋ค๋ฉด, ํ๋ฉด์ ํฌ๊ธฐ๊ฐ ๋ฐ๋๋ ๊ธ์๊ฐ ๊นจ์ง๊ฒ๋๋ค. ํ์ง๋ง ํ ์คํธ๋ฅผ ์ด๋ฏธ์ง์ ๋ณ๋๋ก ์ฐ๊ฒ๋๋ค๋ฉด ๊นจ์ง์ง๋ ์๊ณ , ์คํฌ๋กค์ํด์ ๊ธ์ด ์ฌ ์๋ ์๋ค. ํ์ผ ํฌ๊ธฐ๋ํ ์ค์ด๋ ๋ค.(CSS๋ฅผ ํตํด์!) ํ์ง๋ง CSS ํจ๊ณผ๋ load time์ ์ ๊ฒฝ์จ์ผํ๋ค. ๋ชจ๋ฐ์ผ์์ ๋ก๋ฉ์ ํ ๋ ์ค๋๊ฑธ๋ฆด ์ ์๊ธฐ ๋๋ฌธ์.
https://www.smashingmagazine.com/2013/04/build-fast-loading-mobile-website/
CSS Background images
ํ๋ฉดํฌ๊ธฐ์ ๋ฐ๋ผ์ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ฐ๋๋ก ํ ์๋ ์๋ค.
background-size: cover;
background-size: contain;
cover๋ก ์ง์ ํ๋ฉด ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ๊ฐ๋ก, ์ธ๋ก ๊ธธ์ด ๋ชจ๋ ์๋ฆฌ๋จผํธ๋ณด๋ค ํฌ๋ค๋ผ๋ ์กฐ๊ฑดํ์ ๊ฐ๋ฅํํ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์๊ฒ ์กฐ์ ํฉ๋๋ค. ๊ฐ๋ก/์ธ๋ก ๋น์จ์ ์ ์ง๋ฉ๋๋ค. ๋ฐ๋ผ์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ ์๋ฆฌ๋จผํธ์ ํฌ๊ธฐ๋ณด๋ค ํญ์ ํฌ๊ฑฐ๋ ๊ฐ์ต๋๋ค.
contain์ผ๋ก ์ง์ ํ๋ฉด ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ๊ฐ๋ก, ์ธ๋ก ๊ธธ์ด ๋ชจ๋ ์๋ฆฌ๋จผํธ ์์ ๋ค์ด์จ๋ค๋ผ๋ ์กฐ๊ฑดํ์ ๊ฐ๋ฅํํ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ํฌ๊ฒ ์กฐ์ ํฉ๋๋ค. ๊ฐ๋ก/์ธ๋ก ๋น์จ์ ์ ์ง๋ฉ๋๋ค. ๋ฐ๋ผ์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ ์๋ฆฌ๋จผํธ์ ํฌ๊ธฐ๋ณด๋ค ํญ์ ์๊ฑฐ๋ ๊ฐ์ต๋๋ค.
Inline vs External images
: ์ค์ ๋ก ํด๋ณด๊ณ ๋ ๋น ๋ฅธ๊ฒ์ ์ ํํด์ผํ๋ค.
Full Responsiveness
Srcset & Sizes
<img src="small.jpg"
srcset="large.jpg 1024w, medium.jpg 640w, small.jpg 320w"
sizes="(min-width: 36em) 33.3vw, 100vw"
alt="A rad wolf">
<img>
ํ๊ทธ์ ์๋ก srcset๊ณผ sizes ์์ฑ์ด ๋ง๋ถ์๋ค. ์ฌ๊ธฐ ์์ ์ srcset ์์ฑ์์๋ ๊ฐ๊ฐ ๊ฐ๋กํญ 1024px, 640px, 320px์ธ 3๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ผํ๋ก ๋ถ๋ฆฌ๋ ๋ฌธ์์ด ํ์์ผ๋ก ์ ์๋ค. ์ด๋ด ๊ฒฝ์ฐ ๋ธ๋ผ์ฐ์ (srcset์ ์ง์ํ๋ ๋ธ๋ผ์ฐ์ )๋ ์ด ๊ฐ์ ์ฝ์ด ํ์ฌ์ ํ๋ฉด ์ํ์ ๋ง๋ ์ ์ ํ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ค๊ฒ ๋๋ค. sizes ์์ฑ๋ ์ถ๊ฐํ ์ ์๋๋ฐ, ์ด ์์ฑ์ ๋ฏธ๋์ด์ฟผ๋ฆฌ์ ์ค๋จ์ (breakpoint) ๋ณ๋ก ํ์ํ ์ด๋ฏธ์ง์ ์ ๋ณด๋ฅผ ์ถ๊ฐ๋ก ์ ๊ณตํจ์ผ๋ก์จ ์ญ์ ๋ธ๋ผ์ฐ์ ๋ก ํ์ฌ๊ธ ํ์ฌ์ ์ํ์ ๊ฐ์ฅ ์ ๋ง๋ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ค๋๋ฐ ๋์์ ์ฃผ๊ฒ ๋๋ค. ์ ์์ ์์๋ ๋ฏธ๋์ด์ฟผ๋ฆฌ์ ์ค๋จ์ ์ผ๋ก ๊ฐ๋กํญ์ด 36em ์ด์์ธ ๊ฒฝ์ฐ์๋ 33.3vw (โviewport widthโ๋ฅผ ์๋ฏธ)์ ์ด๋ฏธ์ง๊ฐ ํ์ํจ์ ๋ธ๋ผ์ฐ์ ์ ์๋ฆฌ๊ณ ์์ผ๋ฉฐ ๋ค์ 100vw๋ ๋ํดํธ๊ฐ์ด๋ค. ๋ฌผ๋ก srcset๊ณผ sizes๋ฅผ ์ง์ํ์ง ์๋ ๋ธ๋ผ์ฐ์ ์์๋ ๊ธฐ์กด์ src ์์ฑ์ด ํด๋ฐฑ(fallback)์ผ๋ก ์ฌ์ฉ๋๋ค.
srcset๊ณผ sizes ์์ฑ์ ์ฌ์ฉํ๋ฉด ์์์ ์ ๊ธฐํ ๋ค ๊ฐ์ง ๋ฌธ์ ๋ค ์ค ์ ์ด๋ ์ฒ์ ๋ ๊ฐ์ง ๋ฌธ์ ๋ ์์ฝ๊ฒ ํด๊ฒฐ๋๋ค. ๋ธ๋ผ์ฐ์ ๊ฐ ํ๋ฉด ํฌ๊ธฐ์ ๋ง์ถฐ ์ ์ ํ ์ฉ๋์ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ค๊ณ ๋ํ ๊ณ ๋ฐ์ง๋ ๋์คํ๋ ์ด ํ๋ฉด์ ๋ํ ๋์๋ ๋ธ๋ผ์ฐ์ ๊ฐ ํ๋จํ์ฌ ๋์ํ๊ธฐ ๋๋ฌธ์ด๋ค.
srcset syntax
one using x
to differentiate between device pixel ratios (DPR), and the other using w
to describe the image's width.
Reacting to Device Pixel Ration
<img src="image_2x.jpg" srcset="image_2x.jpg 2x, image_1x.jpg 1x" alt="a cool image">
1x represents
1x
displays and2x
represents displays with twice the pixel density, like Apple's Retina displayReacting to Image Width
<img src="image_200.jpg" srcset="image_200.jpg 200w, image_100.jpg 100w" alt="a cool image">
widthDescriptor is measured in pixels and must be an integer followed by a
w
.
sizes Syntax
sizes attribute to the image with a media query and a vw value.
<img src="images/great_pic_800.jpg" sizes="(max-width: 400px) 100vw, (min-width: 401px) 50vw" srcset="images/great_pic_400.jpg 400w, images/great_pic_800.jpg 800w" alt="great picture">
sizes consists of comma separated mediaQuery width pairs. sizes tells the browser early in the load process that the image will be displayed at some width when the mediaQuery is hit.
In fact, if sizes is missing, the browser defaults sizes to 100vw, meaning that it expects the image will display at the full viewport width.
Picture
srcset/sizes ์์ฑ๋ง์ผ๋ก ํด๊ฒฐํ ์ ์๋ ๋ฌธ์ ๋ค, ์์ปจ๋ ์ํธ ๋๋ ์
์ฒ๋ฆฌ๋ ์ฌ๋ฌ ์ด๋ฏธ์ง ํฌ๋งท ์ง์ ๋ฑ์ <picture>
์๋ฆฌ๋จผํธ๋ก ํด๊ฒฐํ ์ ์๋ค.
<picture>
<source media="(min-width: 40em)"
srcset="big.jpg 1x, big-hd.jpg 2x">
<source srcset="small.jpg 1x, small-hd.jpg 2x">
<img src="fallback.jpg" alt="">
</picture>
์๋ฆฌ๋จผํธ๋ ํ์ ์์๋ก <source>
์๋ฆฌ๋จผํธ๋ฅผ ๋์ด ๊ฐ๊ฐ์ ์ด๋ฏธ์ง ์์ค๋ฅผ ์ฒ๋ฆฌํ๋ค. ์ ์์ ์์๋ ๋ฏธ๋์ด์ฟผ๋ฆฌ๋ก min-width๊ฐ์ด 40em ์ด์์ธ ๊ฒฝ์ฐ๋ big.jpg ํ์ผ์, ๊ทธ ์ดํ์ธ ๊ฒฝ์ฐ๋ small.jpg ํ์ผ์ ๊ฐ๊ฐ ๋ก๋ํ๊ฒ๋ ์ค์ ๋์ด ์๋ค. ์ด ๋ ๊ฐ๊ฐ์ <source>
์๋ฆฌ๋จผํธ ๋ด์๋ srcset ์์ฑ์ ์ถ๊ฐํ์ฌ ๋ฐ์ง๋(์ฌ๊ธฐ์๋ 1x์ 2x๋ฅผ ์ฌ์ฉํ๋๋ฐ, 2x๋ 196 DPI ์ด์์ ์ผ์ปซ๋๋ค)์ ๋ฐ๋ผ ๊ฐ๊ธฐ ๋ค๋ฅธ ์ด๋ฏธ์ง ํ์ผ์ ๋ก๋ํ๊ฒ๋ ์ค์ ํ๋๋ฐ, ์ด ๋ถ๋ถ์ ์์์ <img>
ํ๊ทธ์ ๋ถ์ srcset ์์ฑ๊ณผ ๋์ผํ๋ค.
<picture>
<source media="(min-width: 800px)"
sizes="80vw"
srcset="lighthouse-landscape-640.jpg 640w,
lighthouse-landscape-1280.jpg 1280w,
lighthouse-landscape-2560.jpg 2560w">
<img src="lighthouse-160.jpg" alt="lighthouse"
sizes="80vw"
srcset="lighthouse-160.jpg 160w,
lighthouse-320.jpg 320w,
lighthouse-640.jpg 640w,
lighthouse-1280.jpg 1280w">
</picture>
์ด <picture>
์๋ฆฌ๋จผํธ๋ฅผ ์ด์ฉํ๋ฉด ์์ <img>
ํ๊ทธ์ ๋ถ์ฌ ์ฌ์ฉํ๋ srcset/sizes ๋ฐฉ์๋ณด๋ค ์กฐ๊ธ ๋ ๋ค์ํ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํด ์ง๋ค. ์๋ฅผ ๋ค์ด, ์ํธ๋๋ ์
(art direction) ์ ๋ค์๊ณผ ๊ฐ์ด ์ฒ๋ฆฌํ ์ ์๋ค. ์ฌ๊ธฐ์๋ ๋ฏธ๋์ด์ฟผ๋ฆฌ๋ก width๊ฐ์ด 800px ์ด์์ธ ๊ฒฝ์ฐ(lighthouse-landscape)์ ๊ทธ ์ดํ์ธ ๊ฒฝ์ฐ(lighthouse) ๊ฐ๊ฐ ๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๋ฉฐ ๊ทธ ๊ฒฐ๊ณผ ์ข์ ํญ์ ํ๋ฉด์์๋ ๋์ ํญ๊ณผ๋ ๋ค๋ฅธ ๋ชจ์์ ์ด๋ฏธ์ง๊ฐ ๋ณด์ฌ์ง์ ์ ์ ์๋ค.
<picture>
<source type="image/webp" srcset="images/butterfly.webp">
<img src="images/butterfly.jpg" alt="a butterfly">
</picture>
<picture>
์๋ฆฌ๋จผํธ๋ ๋ค์ํ ์ด๋ฏธ์ง ํ์์ ์ฒ๋ฆฌํ๋ ๋ฐ๋ ์ฌ์ฉ๋ ์ ์๋ค. ์๋ ์์ ๋ฅผ ๋ณด๋ฉด webp๋ฅผ ์ง์ํ๋ ๋ธ๋ผ์ฐ์ ์ธ ๊ฒฝ์ฐ jpg ํ์ผ์ด ์๋ webp ํ์ผ์ ํ์ํ๋๋ก ํ๊ณ ์๋ค.
[์ฐธ์กฐํ์ด์ง] http://www.usefulparadigm.com/2014/11/03/processing-images-on-responsive-web/
Last updated
Was this helpful?