๐Ÿšซ ์•ˆํ‹ฐ ํŒจํ„ด์œผ๋กœ์„œ์˜ CSS background-image ์†์„ฑ

Chris Yangยท2019๋…„ 8์›” 28์ผ
35
post-thumbnail

๋“ค์–ด๊ฐ€๋ฉฐ

์›นํŽ˜์ด์ง€์— ์ด๋ฏธ์ง€๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ <img> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, CSS background-image ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ €๋Š” ์–ด๋–ค ์ƒํ™ฉ์— ์–ด๋–ค ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

img tag vs css background-image ๋“ฑ์˜ ํ‚ค์›Œ๋“œ๋กœ ๊ฒ€์ƒ‰ํ•˜๋‹ค ์ž์„ธํžˆ ์„ค๋ช…๋œ ๊ธ€์„ ์ฝ์—ˆ๊ณ , ๋” ๋งŽ์€ ๋ถ„๋“ค์ด ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๋ฒˆ์—ญํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” nystudio107์˜ ๋ธ”๋กœ๊ทธ ๊ธ€ "The CSS background-image property as an anti-pattern"์„ ์›์ž‘์ž์˜ ํ—ˆ๋ฝ์„ ๋ฐ›์•„ ๋ฒˆ์—ญํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

์˜์—ญ, ์˜ค์—ญ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ๋ฐฑ์€ ํ™˜์˜์ž…๋‹ˆ๋‹ค. ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.


์•ˆํ‹ฐ ํŒจํ„ด์œผ๋กœ์„œ์˜ CSS background-image ์†์„ฑ

CSS background-image ์†์„ฑ์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋†€๋ผ์šด ์ผ๋“ค์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—๋Š” ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

image.png

๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด CSS background-image ์†์„ฑ์œผ๋กœ ๋‹ค์–‘ํ•œ ์ผ๋“ค์„ ํ•ด์™”์Šต๋‹ˆ๋‹ค. ์ด ์†์„ฑ์€ ์˜ค๋ž˜ ์‚ฌ์šฉ๋˜์–ด ์™”์ง€๋งŒ, ์ด์ œ๋Š” ์ž‘๋ณ„ ์ธ์‚ฌ๋ฅผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ง„์งœ ๋ฌธ์ œ๋Š” CSS background-image ์†์„ฑ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ, ๋ฉ”์ธ ์ด๋ฏธ์ง€๋‚˜ UI ์ด๋ฏธ์ง€์™€ ๊ฐ™์€, ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•ด์„œ๋Š” ์•ˆ๋˜๋Š” ๊ณณ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ถ€์ ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ background-image๋Š” ์•ˆํ‹ฐ ํŒจํ„ด์ž…๋‹ˆ๋‹ค. background-image์˜ ์ข‹์€ ์‚ฌ์šฉ ์‚ฌ๋ก€๋„ ์žˆ์„๊นŒ์š”? ๋‹น์—ฐํžˆ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, CSS background-image ์†์„ฑ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ์ค‘์š”ํ•œ ๊ฒƒ์€, ์˜ค๋Š˜๋‚  ์ตœ์‹  ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ด๋ฏธ์ง€๋ฅผ ์œ„ํ•œ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ˜ธ๋ž‘์ด ๋‹ด๋ฐฐํ”ผ๋˜ ์‹œ์ ˆ์˜ ์‹œ๋Œ€์ง€๋‚œ ์ด์•ผ๊ธฐ๋Š” ํ•˜์ง€๋ง™์‹œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ด์ œ CSS background-image ์†์„ฑ์˜ ๋‹จ์ ๊ณผ, ์ด ์†์„ฑ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐ ํ•ด๋ด…์‹œ๋‹ค.

image.png

CSS background-image ์†์„ฑ์ด ๋‚˜์œ ์ด์œ ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. SEO์— ์ข‹์ง€ ์•Š๋‹ค

CSS background-image ์†์„ฑ์— ์žˆ๋Š” ์ด๋ฏธ์ง€๋Š” Google ๋ด‡์ด ํฌ๋กค๋งํ•˜๊ฑฐ๋‚˜ ์ƒ‰์ธํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ณ„๊ฑฐ ์•„๋‹ˆ๋ผ๊ตฌ์š”? ๊ธ€์Ž„์š”, Moz.com์˜ ๊ธ€Google ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰์—์„œ ์ˆœ์œ„๋ฅผ ๋งค๊ธฐ๋Š” ๋ฐฉ๋ฒ•์—์„œ ์ธ์šฉํ•œ ์ด ๊ธ€์„ ๋ด…์‹œ๋‹ค.

โ€ฆ Google ๋ชจ๋“  ๊ฒ€์ƒ‰์˜ 3๋ถ„์˜ 1์€ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰์ด๋ฉฐ, ๊ฒ€์ƒ‰๊ฒฐ๊ณผ์˜ 12.5%๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹คโ€ฆ

Google ๊ฒ€์ƒ‰์˜ 33%๋Š” ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๊ฐ€ ํŽ˜์ด์ง€์˜ ์ฃผ์ œ๋‚˜ ์‚ฌ์—…๊ณผ ๊ด€๋ จ์ด ์žˆ๋‹ค๋ฉด, ๋‹น์‹ ์€ ๋ถ„๋ช… ๊ฒ€์ƒ‰์— ์ƒ‰์ธ๋˜๊ธธ ์›ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

background-image๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์ด๋ฏธ์ง€์˜ alt=""์œผ๋กœ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์„ค๋ช…๊ณผ ์ปจํ…์ŠคํŠธ๋ฅผ Google์— ์ œ๊ณตํ•  ์ˆ˜ ์กฐ์ฐจ ์—†์Šต๋‹ˆ๋‹ค.

2. ์ ‘๊ทผ์„ฑ์— ์ข‹์ง€ ์•Š๋‹ค

CSS background-image ์†์„ฑ์€ ์ ‘๊ทผ์„ฑ์— ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Screen readers๋Š” background-image๋ฅผ ์™„์ „ํžˆ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.

Accessibility Conscerns๋ฅผ ๋ด…์‹œ๋‹ค:

๋ธŒ๋ผ์šฐ์ €๋Š” ๋ณด์กฐ ๊ธฐ์ˆ (assistive tech)์„ ์œ„ํ•ด ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ํŠน๋ณ„ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์Šคํฌ๋ฆฐ ๋ฆฌ๋”์— ์ค‘์š”ํ•œ๋ฐ, ์Šคํฌ๋ฆฐ ๋ฆฌ๋”๋Š” ๊ทธ ์ •๋ณด๋ฅผ ์•Œ๋ฆฌ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž์—๊ฒŒ ์•„๋ฌด๊ฒƒ๋„ ์ „๋‹ฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์Šคํฌ๋ฆฐ ๋ฆฌ๋”๊ฐ€ ์ด๋ฏธ์ง€๋ฅผ ์™„์ „ํžˆ ๋ฌด์‹œํ•˜์ง€ ์•Š์•˜๋”๋ผ๋„, ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‚˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” alt="" ํ…์ŠคํŠธ์กฐ์ฐจ ์—†์Šต๋‹ˆ๋‹ค.

Pro Tip: ์‹ค์ œ๋กœ ์Šคํฌ๋ฆฐ ๋ฆฌ๋”๊ฐ€ ์ด๋ฏธ์ง€๋ฅผ ๋ฌด์‹œํ•˜๋„๋ก ํ•˜๋ ค๋ฉด(๋””์ž์ธ์  ์š”์†Œ์ผ ๋•Œ), ๋นˆ alt ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. ์˜ˆ: alt="" (role="presentation" ์†์„ฑ์€ ARIA์˜ ์ฒซ ๋ฒˆ์งธ ๊ทœ์น™์„ ์œ„๋ฐ˜ํ•˜๋ฏ€๋กœ, ์ตœ์„ ์ด ์•„๋‹™๋‹ˆ๋‹ค.)

3. ์„ฑ๋Šฅ์— ์ข‹์ง€ ์•Š๋‹ค

์–ด๋–ป๊ฒŒ background-image ์†์„ฑ์ด ์„ฑ๋Šฅ์— ๋‚˜์œ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ผ๋ฐ˜์ ์œผ๋กœ ๋””๋ฐ”์ด์Šค ํ™”๋ฉด ๋„ˆ๋น„๋‚˜ ํ•ด์ƒ๋„์— ๊ด€๊ณ„์—†์ด background-image ์†์„ฑ์— ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋งŒ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ด์ƒ์ ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ๋ฐ˜์‘ํ˜• ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋””๋ฐ”์ด์Šค์˜ ํ™”๋ฉด ๋„ˆ๋น„๋‚˜ ํ•ด์ƒ๋„์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์ธ ์‚ฌ์ดํŠธ์—์„œ ์ด๋ฏธ์ง€๋Š” ์ „์†ก๋˜๋Š” ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ์ฒญํฌ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ์ด๋ฏธ์ง€๋“ค์„ ์ตœ์ ํ™”ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

@media ์ฟผ๋ฆฌ์™€ CSS background-image๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ด๋ฏธ์ง€๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์•ผํ•˜๋Š” ๊ฒฝ์šฐ ๊ต‰์žฅํžˆ ๋ฒˆ๊ฑฐ๋กœ์›Œ์ง‘๋‹ˆ๋‹ค.

์ด๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กœ์šด ์ด์œ ๋Š” ์ด๋ฏธ์ง€๊ฐ€ CSS์— ํฌํ•จ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ CSS๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ œ์–ดํ•˜์ง€ ์•Š๋Š” ๋นŒ๋“œ ํ”„๋กœ์„ธ์Šค์— ์˜ํ•ด ๋นŒ๋“œ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ด๊ฒƒ์ด ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์€ ๋ณดํ†ต ํฌ๊ธฐํ•˜๊ณ  ๋ชจ๋“  ๋””๋ฐ”์ด์Šค ํ™”๋ฉด ํฌ๊ธฐ์™€ ํ•ด์ƒ๋„์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ๋„์™€์ฃผ๋Š” bgset๊ณผ ๊ฐ™์€ JavaScript ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์ง€๋งŒ, ์™œ ๋‹ค๋ฅธ ๋‹จ์ ์ด ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด JavaScript๋ฅผ ๋” ์ถ”๊ฐ€ํ•ด์•ผํ•˜๋‚˜์š”?

๊ฒŒ๋‹ค๊ฐ€ webp์™€ ๊ฐ™์€ next-generation ํฌ๋งท์˜ ํ”„๋กœ๊ทธ๋ ˆ์‹œ๋ธŒ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•ด๋„, background-image๋กœ ์ธํ•ด ๋” ๋ฒˆ๊ฑฐ๋กœ์›Œ์ง‘๋‹ˆ๋‹ค.

๋ธŒ๋ผ์šฐ์ €๊ฐ€ CSS๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ํŒŒ์‹ฑํ•˜๊ธฐ ์ „๊นŒ์ง€ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค

๋˜ ๋‹ค๋ฅธ ์„ฑ๋Šฅ ๋‹จ์ ์€, ์ด๋ฏธ์ง€๊ฐ€ background-image๋กœ CSS์— ํฌํ•จ๋œ ๊ฒฝ์šฐ, ์ด๋ฏธ์ง€๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์ „์— ๋ธŒ๋ผ์šฐ์ €๊ฐ€ CSS๋ฅผ ๋จผ์ € ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ํŒŒ์‹ฑํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ฆ‰, ๋ฉ”์ธ hero ์ด๋ฏธ์ง€๊ฐ€ ๋‹ค์šด๋กœ๋“œ ๋Œ€๊ธฐ์—ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋‚ฎ์•„์ง€๊ณ , ๋กœ๋“œ๊ฐ€ ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  HTML ์š”์†Œ๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, CSS๊ฐ€ ๋‹ค์šด๋กœ๋“œ๋˜๊ธฐ ์ „์— ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํ›จ์”ฌ ๋” ๋น ๋ฅด๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. CMS์™€ CDN์— ์ข‹์ง€ ์•Š๋‹ค.

CSS background-image ์†์„ฑ์€ Content Management Systems๊ณผ Content Distribution Networks (CDNs)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ์—๋„ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

<div>์— background-image๋กœ ์„ค์ •๋œ ์ด๋ฏธ์ง€๋กœ ๊ตฌํ˜„๋œ ์›น์‚ฌ์ดํŠธ์— ๋ฉ”์ธ hero ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค.

์ „์„ธ๊ณ„ ์‚ฌ๋žŒ๋“ค์ด ์ด ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ €๋Š” CDN์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ๋ฆฌ์†Œ์Šค๋“ค๋ฅผ ์ตœ์ ์œผ๋กœ ์ œ๊ณตํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

CSS์— ์ด๋ฏธ์ง€ URL์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด, ์ €๋Š” ํ”„๋กœ์ ํŠธ ์ „์ฒด์˜ ์ด๋ฏธ์ง€๋“ค์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋ชจ๋“  ์ด๋ฏธ์ง€๋ฅผ local URL์—์„œ CDN URL๋กœ ๊ต์ฒดํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

CSS ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ๋ฆฌ ๋ชจ๋“  ์ด๋ฏธ์ง€์˜ URL์„ ์ค€๋น„ํ•œ ๊ฒฝ์šฐ์—” ๊ดœ์ฐฎ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์€ ์ด๋ฏธ์ง€๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด CSS ์—์…‹ ๋ฒˆ๋“ค์„ ๋งŒ๋“ค๊ณ  ๋ฐฐํฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋‹ˆ๋ฉด ์ด๋ฅผ ์œ„ํ•ด ๋™์ ์œผ๋กœ inline CSS๋ฅผ ์ƒ์„ฑํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ... ๊ทธ๋ ‡๊ฒŒ๊นŒ์ง€ ํ•˜์ง„ ๋ง™์‹œ๋‹ค.

CMS๊ฐ€ URL prefix๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฒŒํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์‰ฌ์šธ ์ˆ˜ ์žˆ์ง€๋งŒ, CSS background-image๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ธ๋ผ์ธ ์Šคํƒ€์ผ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํŠธ๋ฆญ์ด ํ•„์š”ํ•ด์„œ ๋” ๊ท€์ฐฎ์•„ ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

CSS์— ์ด๋ฏธ์ง€ URL์„ ๋„ฃ๋Š” ๊ฒƒ์€ ํ•ญ์ƒ "ํ•˜๋“œ ์ฝ”๋”ฉ"ํ•˜๋Š” ๋Š๋‚Œ์ด ๋“ญ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์–ด๋–จ ๋•Œ ์ข‹์€๊ฑด๊ฐ€์š”?

๊ทธ๋Ÿฌ๋ฉด, ์ง€๊ธˆ๊นŒ์ง€ ๋‹จ์ ์— ๋Œ€ํ•ด์„œ๋งŒ ์ด์•ผ๊ธฐํ–ˆ์ง€๋งŒ, CSS background-image ์†์„ฑ์ด ์‹ค์ œ๋กœ ์ข‹์€ ์ƒํ™ฉ์€ ์–ธ์ œ์ผ๊นŒ์š”?

image.png

์ „ํ˜€ ๋ถˆํ•„์š”ํ•ด ๋ณด์ผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์‹ค์ œ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ˆœ์ˆ˜ ์žฅ์‹ ์ด๋ฏธ์ง€ ๋ฐฐ๊ฒฝ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐโ€ฆ background-image๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”!

์‹ค์ œ๋กœ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•  ๊ฒฝ์šฐ background-image๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค

๊ทธ๋ ‡๊ฒŒํ•œ๋‹ค๋ฉด, image-set()์„ background-image์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์„ฑ๋Šฅ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์†์„ฑ์ด ๋ณธ์งˆ์ ์œผ๋กœ ๋‚˜์˜๋‹ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ด ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ตœ์„ ์˜ ์„ ํƒ์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ๋งํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

๋Œ€์‹  <PICTURE>๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”!

์ €๋Š” ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด background-image๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” CSS background-size: cover; ์†์„ฑ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ๋งค์šฐ ์‰ฌ์šด ๋ฐฉ์‹์œผ๋กœ "๋ฎ์„(cover)" ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Š” object-fit ์†์„ฑ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

object-fit ์†์„ฑ๊ณผ ํ•จ๊ป˜ semantic HTML ์š”์†Œ <picture>๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, background-size: cover;์™€ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒƒ๋“ค ๋˜ํ•œ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • SEO ์นœํ™”์  ์ด๋ฏธ์ง€
  • ์ ‘๊ทผ์„ฑ ์นœํ™”์  ์ด๋ฏธ์ง€ (alt="" ์†์„ฑ ์‚ฌ์šฉ)
  • CMS-generated ์ด๋ฏธ์ง€, CDN๊ณผ ํ•จ๊ป˜ ์ž˜ ๋™์ž‘ํ•จ
  • ์ด๋ฏธ์ง€ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ srcset๊ณผ ํ•จ๊ป˜ ์ž˜ ๋™์ž‘ํ•จ
  • .webp ๊ฐ™์€ next-generation ์ด๋ฏธ์ง€ ํฌ๋งท์„ ์œ„ํ•ด <source>๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

์ด๋Š” ์‹ค์ œ ๊ตฌํ˜„ํ•˜๊ธฐ๋Š” ์–ด๋ ต์ง€์•Š์€๋ฐ ๋น„ํ•ด ์ด์ ์€ ์ •๋ง ๋งŽ์Šต๋‹ˆ๋‹ค! object-fit ์†์„ฑ์€ ์ด๋ฏธ์ง€๋‚˜ ๋น„๋””์˜ค๊ฐ€ ๋ถ€๋ชจ ์ปจํ…Œ์ด๋„ˆ์— ์–ด๋–ป๊ฒŒ ๋งž์•„์•ผํ•˜๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

<div
  style="background-image: url('/some/man-with-a-dog.jpg');
            background-size: cover;"
></div>
<!-- The old way -->

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

<picture>
  <img
    src="/some/man-with-a-dog.jpg"
    alt="Man with a dog"
    style="object-fit: cover;"
  />
</picture>
<!-- The new way -->

๋ถ„๋ช… ๋‘ ์ผ€์ด์Šค ๋ชจ๋‘, inline ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ background-image ์†์„ฑ์„ ๋ฒ„๋ฆฌ๊ณ  <picture>์™€ <img> ๊ฐ™์€ ์˜๋ฏธ์  HTML ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ ‘๊ทผ ๋ฐฉ์‹์ด ์–ผ๋งˆ๋‚˜ ์œ ์—ฐํ•œ์ง€ ์•Œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค.

๊ทธ ๋‹ค์Œ srcset์„ ํ†ตํ•ด ๋ฐ˜์‘ํ˜• ์‚ฌ์ด์ฆˆ ์ด๋ฏธ์ง€๋“ค์„ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ <source>๋ฅผ ํ†ตํ•ด ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” .webp์™€ ๊ฐ™์€ next-generation ์ด๋ฏธ์ง€ ํฌ๋งท์„ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด Chrome์ด๋‚˜ ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €์˜ native image lazy load ๊ธฐ๋Šฅ๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์žฅ์  ๋˜ํ•œ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ชจ๋“  ์ž‘์—…์„ ํ•˜๋Š” ์ตœ์ข… ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<picture>
  <source
    srcset="
      /some/_1170x658_crop_center-center/man-with-a-dog.webp 1170w,
      /some/_970x545_crop_center-center/man-with-a-dog.webp   970w,
      /some/_750x562_crop_center-center/man-with-a-dog.webp   750w,
      /some/_320x240_crop_center-center/man-with-a-dog.webp   320w
    "
    sizes="100vw"
    type="image/webp"
  />
  <source
    srcset="
      /some/_1170x658_crop_center-center/man-with-a-dog.jpg 1170w,
      /some/_970x545_crop_center-center/man-with-a-dog.jpg   970w,
      /some/_750x562_crop_center-center/man-with-a-dog.jpg   750w,
      /some/_320x240_crop_center-center/man-with-a-dog.jpg   320w
    "
    sizes="100vw"
  />
  <img
    src="/some/man-with-a-dog-placeholder.jpg"
    alt="Man with a dog"
    style="object-fit: cover;"
    loading="lazy"
  />
</picture>
<!-- The final way -->

์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ lazy loading (์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ๋™์ž‘)
  • ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ webp ํฌ๋งท (์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฉด skip)
  • ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ ํ™”๋œ ๋ฐ˜์‘ํ˜• ์ด๋ฏธ์ง€์˜ srcset.

SEO ์นœํ™”์ , ์ ์ง„์  ํ–ฅ์ƒ, ์ ‘๊ทผ์„ฑ, ์„ฑ๋Šฅ์ด ๋ชจ๋‘ ํ•˜๋‚˜์˜ ์ž‘์€ ์ฝ”๋“œ ๋ฒˆ๋“ค๋กœ ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค.

image.png

Boom. ๐Ÿ’ฅ

๋˜ํ•œ, ๋ชจ๋“  ์ด๋ฏธ์ง€๋Š” ImageOptimize์™€ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ํ†ตํ•ด CMS์—์„œ ํฌ๊ธฐ๊ฐ€ ์ž๋™์œผ๋กœ ์กฐ์ •๋˜์–ด CDN์ด๋‚˜ Amazon S3์„ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, object-fit์œผ๋กœ ๋…์ฐฝ์ ์ธ ์ด๋ฏธ์ง€ ํฌ๋กญํ•‘์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

OUT WITH THE OLD, IN WITH THE NEW

<picture>์™€ srcset์˜ ๋ธŒ๋ผ์šฐ์ € ์ง€์›์€ ๊ต‰์žฅํžˆ ์ข‹์Šต๋‹ˆ๋‹ค. (ํ•„์š”ํ•˜๋‹ค๋ฉด polyfill ์‚ฌ์šฉ), object-fit์— ๋Œ€ํ•œ ๋ธŒ๋ผ์šฐ์ € ์ง€์› ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. (์—ญ์‹œ ํ•„์š”ํ•˜๋‹ค๋ฉด polyfill ์‚ฌ์šฉ). polyfill.io๋Š” ๋˜ ๋‹ค๋ฅธ ์ข‹์€ ํด๋ฆฌํ•„ ์‚ฌ์šฉ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

์‹ค์ œ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์›ํ•˜๋Š” ๋“œ๋ฌธ ๊ฒฝ์šฐ๋Š” ์ œ์™ธํ•˜๊ณ , ์ด์ œ background-image๋ฅผ ๋ฒ„๋ฆด ๋•Œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

image.png

๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•ด ๋ชจ๋ฒ”์‚ฌ๋ก€๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ํ•„์š”ํ•  ๋•Œ ์ ์ง„์  ํ–ฅ์ƒ๊ณผ ํด๋ฆฌํ•„์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ๋ฐฉ๋ฌธ์ž์˜ ๋น„์œจ์ด ์•„์ฃผ ์ ์€ ์˜ˆ์ „ ๋ธŒ๋ผ์šฐ์ €์— ๋Œ€ํ•ด ๋„ˆ๋ฌด ๋™์š”ํ•˜์ง€ ๋งˆ์„ธ์š”.

์ฝ˜ํ…์ธ  ์ด๋ฏธ์ง€๋ฅผ ์žฅ์‹(decoration)์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. ์˜๋ฏธ์žˆ๋Š” HTML ์ปจํ…์ธ  ์ด๋ฏธ์ง€ ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

์ €๋Š” ๋‹น์‹ ์ด SEO์— ๋” ์นœํ™”์ ์ด๊ณ , ๋” ์ ‘๊ทผํ•˜๊ธฐ ์‰ฝ๊ณ , ๋” ์„ฑ๋Šฅ์ด ์ข‹์€ ์›น ์‚ฌ์ดํŠธ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ฒŒ๋  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์˜๋ฏธ์žˆ๋Š” HTML๋กœ ์ž‘์„ฑํ•˜๊ธฐ ๋”์šฑ ๋งŒ์กฑ์Šค๋Ÿฝ๊ฒŒ ๋Š๋ผ๊ฒŒ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

CSS background-image ์†์„ฑ์€ ๋ฉ”์ธ hero ์ด๋ฏธ์ง€๋‚˜ UI ์š”์†Œ์— ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด์ œ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์œผ๋‹ˆ, ์ด๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

์ด๋ฏธ์ง€ ์ตœ์ ํ™”์— ๊ด€์‹ฌ์ด ๋งŽ๋‹ค๋ฉด, images.guide๋ฅผ ํ™•์ธํ•ด๋ณด์„ธ์š”.

Enjoy your object-fit!

๋” ์ฝ๊ธฐ

8๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2019๋…„ 8์›” 28์ผ

๋•๋ถ„์— ์ข‹์€ ์ •๋ณด๋ฅผ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿค—

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2019๋…„ 8์›” 30์ผ

์ข‹์€ ์ •๋ณด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~!

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2019๋…„ 9์›” 6์ผ

๋ฒˆ์—ญ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2019๋…„ 9์›” 9์ผ

picture ๋ผ๋Š” ํƒœ๊ทธ๋„ ์žˆ๊ตฐ์š”. ์š”์ฆ˜์—” ๋งˆํฌ์—… ๋ถ€๋ถ„์„ ์ด์ œ๋Š” ๋ณด์ง€ ์•Š์•˜๋Š”๋ฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ œ ์ƒ๊ฐ์—” img์— ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ์‹œ

์™€ ํƒœ๊ทธ ์กฐํ•ฉ์ด ๋” ๋‚ซ๋‹ค๊ณ  ๋ด…๋‹ˆ๋‹ค. picture ํƒœ๊ทธ๋Š” ๋ฐ˜์‘ํ˜• ์›น์ด๋‚˜ ๊ธฐ์ˆ ์ ์ธ ๋ถ€๋ถ„์— ์ ํ•ฉํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์ŠคํŽ™๋ณด๋‹ˆ video, audio์™€ ๋น„์Šทํ•œ ๋ฉด๋“ค์ด ์žˆ๊ณ ์š”. ๊ทธ๋ฆฌ๊ณ , background-image ์†์„ฑ์€ ์–ธ๊ธ‰ํ•˜์‹ ๋Œ€๋กœ SEO์™€ ๊ฑฐ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์‚ฌ์šฉ์ด๋ฏธ์ง€๊ฐ€ ํ•ด๋‹น html ๋ฌธ์„œ์—์„œ ํ•„์š”์š”์†Œ๋ฉด imgํƒœ๊ทธ๋กœ, ๊ทธ๋ƒฅ ๊พธ๋ฏธ๋Š” ์š”์†Œ๋ฉด background-image๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ๋งž์Šต๋‹ˆ๋‹ค.

1๊ฐœ์˜ ๋‹ต๊ธ€