[TIL] Image sprite

Sohee Yeoยท2022๋…„ 4์›” 19์ผ
1

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
20/23
post-thumbnail

2022.04.19 Today I Learned๐Ÿ“š

๐Ÿ“ธImage Sprite

์ด๋ฏธ์ง€ ์Šคํ”„๋ผ์ดํŠธ๋ž€, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋กœ ํ•ฉ์ณ์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. HTML์— img ํƒœ๊ทธ๊ฐ€ ๋งŽ์•„์ง€๋ฉด ์„œ๋ฒ„ ์š”์ฒญ์ด ๋งŽ์•„์ ธ ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์†๋„๊ฐ€ ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฏธ์ง€ ์Šคํ”„๋ผ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋งŒ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๋”ฉ ์†๋„๊ฐ€ ํ›จ์”ฌ ๋นจ๋ผ์ง„๋‹ค.
background-image๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„์ด์ฝ˜ ๋“ฑ ์ค‘์š”ํ•œ ๋‚ด์šฉ์ด ์—†๋Š” ์ด๋ฏธ์ง€๋“ค์„ ์‚ฝ์ž…ํ•  ๋•Œ image sprite ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.

โ†’ Naver์˜ image sprite โ†’ Daum์˜ image sprite. ํ…์ŠคํŠธ๋ฅผ image sprite๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

Image sprite ๋ฐฉ๋ฒ•

image sprite generator์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํฌํ† ์ƒต, ํ”ผ๊ทธ๋งˆ, ์Šค์ผ€์น˜ ๋“ฑ์„ ์‚ฌ์šฉํ•ด ์ด๋ฏธ์ง€๋ฅผ ํŽธ์ง‘ํ•œ๋‹ค.

image sprite generator(์˜จ๋ผ์ธ ์—๋””ํ„ฐ)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ sprite๋ฅผ ํ•ด๋ณด์ž.
https://www.toptal.com/developers/css/sprite-generator/

1.ํŽธ์ง‘ํ•  ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

choose files๋ฅผ ํด๋ฆญํ•˜์—ฌ ํŽธ์ง‘ํ•  ์ด๋ฏธ์ง€๋“ค์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. ์™ผ์ชฝ ์ƒ๋‹จ Padding between elements๋กœ padding ๊ฐ’์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ , ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์˜ Align elements๋กœ ์ด๋ฏธ์ง€๋“ค์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

2.ํŽธ์ง‘ํ•œ ์ด๋ฏธ์ง€ ์ €์žฅํ•˜๊ธฐ

์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋งŒ ํ•˜๋ฉด ๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€๋“ค์ด ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋กœ ํŽธ์ง‘๋œ๋‹ค. ์™ผ์ชฝ์˜ ์ขŒํ‘œ๋“ค์€ ์ด๋ฏธ์ง€ ํ•˜๋‚˜ํ•˜๋‚˜์˜ ์œ„์น˜ ์ขŒํ‘œ๊ฐ’์ด๊ณ , ์˜ค๋ฅธ์ชฝ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•˜์—ฌ sprite ๊ธฐ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

3. background-image ํƒœ๊ทธ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์ฃผ๊ธฐ

IR ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ image sprite ๊ธฐ๋ฒ•์„ ์ ์šฉํ•ด๋ณด์ž.

<section>
        <ul>
            <li class="aribnb"><a href="#">AirBnB</a></li>
            <li class="chrome"><a href="#">Chrome</a></li>
            <li class="github"><a href="#">gitHub</a></li>
            <li class="discord"><a href="#">Discord</a></li>
            <li class="figma"><a href="#">Figma</a></li>
        </ul>
    </section>
ul {
     list-style: none;
     position: relative;
}

li {
    margin: 10px 0;
}
a {
   display: block;
   overflow: hidden;
   font-size: 1px;
   line-height: 0;
   text-indent: -9999px;
}
        
.aribnb {
         background: url("sprites.png") no-repeat;
         background-position: -10px -10px;
         width: 28px;
         height: 31px;
}

.chrome {
         background: url("sprites.png") no-repeat;
         background-position: -58px -10px;
         width: 28px;
         height: 29px;
}

.github {
         background: url("sprites.png") no-repeat;
         background-position: -106px -10px;
         width: 28px;
         height: 28px;
}

.discord {
          background: url("sprites.png") no-repeat;
          background-position: -10px -61px;
          width: 28px;
          height: 22px;
}

.figma {
        background: url("sprites.png") no-repeat;
        background-position: -10px -103px;
        width: 19px;
        height: 28px;
}

๐Ÿ“ŒIR ๊ธฐ๋ฒ•์ด๋ž€, ํ…์ŠคํŠธ๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜์ง„ ์•Š์ง€๋งŒ ์Šคํฌ๋ฆฐ๋ฆฌ๋”๋‚˜ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์œ„ํ•ด ์ด๋ฏธ์ง€์˜ ๋Œ€์ฒดํ…์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•˜๋Š” CSS ๊ธฐ๋ฒ•์ด๋‹ค.

a ํƒœ๊ทธ์˜ ํ…์ŠคํŠธ๋“ค์„ text-indent: -9999px๋กœ ์„ค์ •ํ•ด ํ™”๋ฉด ๋ฐ–์œผ๋กœ ๋นผ๋‚ด์ฃผ์–ด ๋ณด์ด์ง€ ์•Š๋„๋ก ์„ค์ •ํ•˜์˜€๋‹ค.

background ์†์„ฑ์œผ๋กœ sprite ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ , image sprite generator ์— ๋ช…์‹œ๋œ ์ขŒํ‘œ๊ฐ’๋“ค์„ background-position ๊ฐ’์œผ๋กœ ์ž…๋ ฅํ•œ๋‹ค. ๊ฐ ์ด๋ฏธ์ง€๋“ค์˜ ์›๋ณธ width, height๊ฐ’์„ ์ž…๋ ฅํ•œ๋‹ค.

4. ์™„์„ฑ

IR ๊ธฐ๋ฒ•์œผ๋กœ a ํƒœ๊ทธ๋“ค์˜ ํ…์ŠคํŠธ๊ฐ€ ๋ณด์ด์ง€ ์•Š๊ณ , image sprite ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋กœ ๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

profile
์‹คํŒจ์— ๋ฌด๋”˜ ์‚ฌ๋žŒ. ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๋ฅผ ๊ฟˆ๊ฟ‰๋‹ˆ๋‹ค

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

comment-user-thumbnail
2022๋…„ 4์›” 26์ผ

์†Œํฌ๋‹˜!!!!!!๐ŸŽถ๐ŸŽถ
์ด๋ฏธ์ง€ ์Šคํ”„๋ผ์ดํŠธ ์ •๋ฆฌ๋ฅผ ์ด๋ ‡๊ฒŒ ์ž˜ํ•ด๋‘์‹œ๋‹ค๋‹ˆ!! ์–ผ๋ฅธ ์„ธ์ƒ ์‚ฌ๋žŒ๋“คํ•œํ…Œ ์•Œ๋ ค์•ผ ๋ฉ๋‹ˆ๋‹ค..
์ €๋งŒ ๋ณด๊ธฐ ์•„๊นŒ์›Œ์š”!!๐Ÿ˜Š๐Ÿ˜Š

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ
comment-user-thumbnail
2022๋…„ 4์›” 27์ผ

์˜ค ์ด๋ฏธ์ง€์Šคํ”„๋ผ์ดํŠธ ์ง„์งœ ์ •๋ฆฌ ์ž˜ํ•˜์…จ๋„ค์š”!! ์š”์ฆ˜ ๋ฐฐ์šฐ๋Š”๊ฒŒ ๋งŽ์•„์„œ ๊ณ„์† ๊นŒ๋จน๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๋ณด๋ฉด์„œ ๋˜ ๋ณต์Šตํ•ฉ๋‹ˆ๋‹ค ใ… ใ…  ์ต์˜ค!!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ