๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์›น(IT) ์ •๋ณด/css

css3 background-size

by ๋‹ˆ๋ชจ๋„ค 2017. 6. 25.
๋ฐ˜์‘ํ˜•

๊ธฐ์กด css์—์„œ ์ถ”๊ฐ€๋œ css3์ถ”๊ฐ€ ์†์„ฑ์ธ background-size์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค.


๊ธฐ์กด์— ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์˜€์„๋•Œ ์ •๋ ฌ ๋ฐ ์œ„์น˜๋งŒ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ


background-size ์ด ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋”์šฑ ํŽธ๋ฆฌํ•œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.


์šฐ์„  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.




auto 

๊ธฐ๋ณธ๊ฐ’. ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„์™€ ๋†’์ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

lenth

๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ฐ’์€ ๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ๊ฐ’์€ ๋†’์ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์ฃผ์–ด์ง€๋ฉด ๋‘ ๋ฒˆ์งธ ๊ฐ’์€ "auto"๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

percentage

๋ถ€๋ชจ ์š”์†Œ์˜ ๋ฐฑ๋ถ„์œจ๋กœ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ฐ’์€ ๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ๊ฐ’์€ ๋†’์ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์ฃผ์–ด์ง€๋ฉด ๋‘ ๋ฒˆ์งธ ๊ฐ’์€ "auto"๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

cover

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

contain

์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„์™€ ๋†’์ด๊ฐ€ ๋‚ด์šฉ ์˜์—ญ ์•ˆ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ์ตœ๋Œ€๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

initial

์ด ์†์„ฑ์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค




-auto-


div {

    background-image:url('sample.gif');

    background-repeat:no-repeat;

    background-size:auto;

}







-lenth-


div {

    background-image:url('sample.gif');

    background-repeat:no-repeat;

    background-size:100px 100px;

}







-percentage-


div {

    background-image:url('sample.gif');

    background-repeat:no-repeat;

    background-size:50%;

}







-cover-


div {

    background-image:url('sample.gif');

    background-repeat:no-repeat;

    background-size:cover;

}








-contain-


div {

    background-image:url('sample.gif');

    background-repeat:no-repeat;

    background-size:contain;

}








-initial-


div {

    background-image:url('sample.gif');

    background-repeat:no-repeat;

    background-size:initial;

}





์œ„์™€๊ฐ™์€ ์†์„ฑ๋“ค์˜ ์ถ”๊ฐ€๋กœ ๋”์šฑ๋” ํŽธ๋ฆฌํ•˜๊ณ  ์ž์œ ๋กœ์šด ์ฝ”๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ์š”. 

์‹ค์งˆ์ ์œผ๋กœ lenth์™€ cover ์†์„ฑ๋“ฑ์ด ์ €๋Š” ๋งŽ์ด ์“ฐ์ด๋Š” ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค.


๋‹ค๋ฅธ ๋ถ„๋“ค๋„ ํ…Œ์ŠคํŠธ ๋งŽ์ด ํ•ด๋ณด์‹œ๊ณ  ์‹ค์ œ ์ฝ”๋”ฉ์—๋„ ๋งŽ์ด ํ™œ์šฉํ•ด๋ณด์„ธ์š”!

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€