๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

CSS ์†์„ฑ 12. background

โ—๏ธ ํ•ด๋‹น ๊ธ€์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜์—์„œ HTML & CSS, SASS(SCSS) Part์˜ ๋ฐ•์˜์›… ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜์ž๋ฃŒ๋ฅผ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

background#

์š”์†Œ์˜ ๋ฐฐ๊ฒฝ์„ ์„ค์ • - ๋‹จ์ถ• ์†์„ฑ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
background-color๋ฐฐ๊ฒฝ ์ƒ‰์ƒtransparent
background-imageํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€none
background-repeat๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ๋ฐ˜๋ณตrepeat
background-position๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ์œ„์น˜0 0
background-attachment๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ์Šคํฌ๋กค ์—ฌ๋ถ€(ํŠน์„ฑ)scroll

์‚ฌ์šฉ๋ฒ•#

background: ์ƒ‰์ƒ ์ด๋ฏธ์ง€๊ฒฝ๋กœ ๋ฐ˜๋ณต ์œ„์น˜ ์Šคํฌ๋กคํŠน์„ฑ;
.box1 {background: red url("../img/image.jpg") no-repeat left top scroll;/* ์ƒ‰์ƒ ์ด๋ฏธ์ง€๊ฒฝ๋กœ ๋ฐ˜๋ณต ์œ„์น˜ ์Šคํฌ๋กคํŠน์„ฑ */}.box2 {background: url("../img/image.jpg") no-repeat right 100px;/* ์ด๋ฏธ์ง€๊ฒฝ๋กœ ๋ฐ˜๋ณต ์œ„์น˜ */}.box3 {background: red;/* ์ƒ‰์ƒ */}

background-color#

์š”์†Œ์˜ ๋ฐฐ๊ฒฝ ์ƒ‰์ƒ์„ ์ง€์ • - ๊ฐœ๋ณ„ ์†์„ฑ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
์ƒ‰์ƒ์š”์†Œ์˜ ๋ฐฐ๊ฒฝ ์ƒ‰์ƒ
transparentํˆฌ๋ช…transparent

โ˜๏ธ ๊ธฐ๋ณธ๊ฐ’์ด transparent ์ด๊ธฐ ๋•Œ๋ฌธ์— ์š”์†Œ์— ์‚ฌ์ด์ฆˆ๋งŒ ์„ค์ •ํ•  ๊ฒฝ์šฐ ๋ณด์ด์ง€ ์•Š์•˜๋‹ค.


์˜ˆ์ œ#

<div></div>
div {width: 200px;height: 100px;background-color: tomato;}

background-image#

์š”์†Œ์˜ ๋ฐฐ๊ฒฝ์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์ด๋ฏธ์ง€๋ฅผ ์‚ฝ์ž… - ๊ฐœ๋ณ„ ์š”์†Œ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
none์ด๋ฏธ์ง€ ์—†์Œnone
url("๊ฒฝ๋กœ")์ด๋ฏธ์ง€ ๊ฒฝ๋กœ(URL)

์‚ฌ์šฉ๋ฒ•#

background-image: url("๊ฒฝ๋กœ");
.box {background-image: url("../img/image.jpg");width: 120px;height: 80px;}.box1 {/* ๊ฐœ๋ณ„ ์†์„ฑ */background-image: url("../img/image1.jpg"),url("../img/image2.jpg"),url("../img/image3.jpg");}.box2 {/* ๋‹จ์ถ• ์†์„ฑ */background: url("../img/image1.jpg") no-repeat,url("../img/image2.jpg") no-repeat 100px 50px,url("../img/image3.jpg") repeat-x;}

โ˜๏ธ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์‚ฝ์ž… ์‹œ, ์š”์†Œ์˜ ํฌ๊ธฐ๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๊ฐ€ ๋ณด์ผ ์ˆ˜ ์žˆ๋‹ค.โœŒ๏ธ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์‚ฝ์ž…ํ•  ๊ฒฝ์šฐ , ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. ๋จผ์ € ์ž‘์„ฑ๋œ ์ด๋ฏธ์ง€๊ฐ€ ๋” ์œ„์— ์Œ“์ธ๋‹ค. ์ด๋Ÿฐ โ€™๋‹ค์ค‘ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€โ€™๋Š” IE8 ์ดํ•˜ ๋ฒ„์ „์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.


background-repeat#

๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ๋ฐ˜๋ณต์„ ์„ค์ • - ๊ฐœ๋ณ„ ์š”์†Œ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
repeat๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์ง, ์ˆ˜ํ‰์œผ๋กœ ๋ฐ˜๋ณตrepeat
repeat-x๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜ํ‰์œผ๋กœ ๋ฐ˜๋ณต
repeat-y๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์ง์œผ๋กœ ๋ฐ˜๋ณต
no-repeat๋ฐ˜๋ณต ์—†์Œ

์˜ˆ์ œ#

<div></div>
.box {background-image:url("https://heropy.blog/css/images/logo.png");background-size: 100px;background-repeat: no-repeat;width: 550px;height: 300px;border: 2px dashed lightgray;}

background-position#

๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ์œ„์น˜๋ฅผ ์„ค์ • - ๊ฐœ๋ณ„ ์š”์†Œ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
%์™ผ์ชฝ ์ƒ๋‹จ ๋ชจ์„œ๋ฆฌ๋Š” 0% 0% , ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ ๋ชจ์„œ๋ฆฌ๋Š” 100% 100%0% 0%
๋ฐฉํ–ฅ๋ฐฉํ–ฅ์„ ์ž…๋ ฅํ•˜์—ฌ ์œ„์น˜ ์„ค์ • top , bottom , left , right , center
๋‹จ์œ„px , em , cm ๋“ฑ ๋‹จ์œ„๋กœ ์ง€์ •

์‚ฌ์šฉ๋ฒ•#

๊ฐ’์ด ๋ฐฉํ–ฅ์ผ ๊ฒฝ์šฐ

background-position: ๋ฐฉํ–ฅ1 ๋ฐฉํ–ฅ2;

๊ฐ€์šด๋ฐ์— ์œ„์น˜์‹œํ‚ฌ ๋•Œ๋Š” center center; ๋กœ ํ•ด๋„ ๋˜์ง€๋งŒ center ๋ฅผ ํ•œ๋ฒˆ๋งŒ ์ž…๋ ฅํ•ด๋„๊ฐ€๋Šฅํ•˜๋‹ค.

๊ฐ’์ด ๋‹จ์œ„( % , px ๋“ฑ)์ผ ๊ฒฝ์šฐ

background-position: X์ถ• Y์ถ•;

โ˜๏ธ ๊ฐ’์ด ๋ฐฉํ–ฅ์ผ ๊ฒฝ์šฐ์—๋Š” ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”๋„ ์ƒ๊ด€์ด ์—†์ง€๋งŒ ๊ฐ’์ด ๋‹จ์œ„์ผ ๊ฒฝ์šฐ์—๋Š” ์ˆœ์„œ๊ฐ€ X์ถ•, Y์ถ•์œผ๋กœ ์ •ํ•ด์ ธ ์žˆ๋‹ค.

๋‹จ์œ„์™€ ๋ฐฉํ–ฅ์„ ํ˜ผํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

background-position: X์ถ•(left, right, center) Y์ถ•(top, bottom, center);

โ˜๏ธ ๋‹จ์œ„์™€ ๋ฐฉํ–ฅ์„ ํ˜ผํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ left , right ์™€ ๊ฐ™์€ X์ถ• ๋ฐฉํ–ฅ์„ ์ฒซ ๋ฒˆ์งธ๋กœ, top , bottom ๊ฐ™์€ Y์ถ• ๋ฐฉํ–ฅ์„ ๋‘ ๋ฒˆ์งธ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. center ๋Š” X์ถ•๊ณผ Y์ถ•์œ„์น˜์— ๋”ฐ๋ผ ๋™์ž‘ํ•œ๋‹ค.


์˜ˆ์ œ#

<div class="box"></div>
.box {width: 550px;height: 300px;border: 2px dashed lightgray;background-image:url("https://heropy.blog/css/images/logo.png");background-size: 100px;background-repeat: no-repeat;background-position: center;}

background-attachment#

์š”์†Œ๊ฐ€ ์Šคํฌ๋กค๋  ๋•Œ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ์Šคํฌ๋กค ์—ฌ๋ถ€(ํŠน์„ฑ) ์„ค์ • - ๊ฐœ๋ณ„ ์†์„ฑ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
scroll๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๊ฐ€ ์š”์†Œ๋ฅผ ๋”ฐ๋ผ์„œ ๊ฐ™์ด ์Šคํฌ๋กค ๋จscroll
fixed๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๊ฐ€ ๋ทฐํฌํŠธ(viewport)์— ๊ณ ์ •๋˜์–ด, ์š”์†Œ์™€ ๊ฐ™์ด ์Šคํฌ๋กค๋˜์ง€ ์•Š์Œ
local์š”์†Œ ๋‚ด ์Šคํฌ๋กค ์‹œ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๊ฐ€ ๊ฐ™์ด ์Šคํฌ๋กค ๋จ

์˜ˆ์ œ#

  • fixed
<section class="section1"></section><section class="section2"></section><section class="section3"></section><section class="section4"></section><section class="section5"></section>
section {height: 300px;border: 2px dashed lightgray;}.section2 {background-image: url("https://www.istarbucks.co.kr/common/img/main/fav_prod_bg_new.jpg");background-size: auto 100%;background-position: right bottom;background-attachment: fixed;}.section3 {background-image:url("https://www.istarbucks.co.kr/common/img/main/reserve_bg.jpg");background-size: auto 100%;background-position: right bottom;background-attachment: fixed;}
  • local
<div class="container"><div class="for-scroll"></div></div>
.container {width: 400px;height: 300px;border: 4px solid red;margin: 50px;overflow: auto;background-image: url("https://heropy.blog/css/images/logo.png");background-size: 50%;background-attachment: local;}.for-scroll {height: 2000px;}

์‚ฌ์šฉ ๋นˆ๋„ ๋‚ฎ์Œ.


background-size#

๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ • - ๊ฐœ๋ณ„ ์†์„ฑ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
auto๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๊ฐ€ ์›๋ž˜์˜ ํฌ๊ธฐ๋กœ ํ‘œ์‹œ๋จauto
๋‹จ์œ„- px , em , % ๋“ฑ ๋‹จ์œ„๋กœ ์ง€์ •- width height ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๊ฐ€๋Šฅ(E.g. 120px 370px )- width ๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋น„์œจ์— ๋งž๊ฒŒ ์ง€์ •๋จ(E.g. 120px )
cover- ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ ๋น„์œจ์„ ์œ ์ง€ํ•˜๋ฉฐ, ์š”์†Œ์˜ ๋” ๋„“์€ ๋„ˆ๋น„์— ๋งž์ถฐ์ง- ์ด๋ฏธ์ง€๊ฐ€ ์ž˜๋ฆด ์ˆ˜ ์žˆ์Œ
contain- ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ ๋น„์œจ์„ ์œ ์ง€ํ•˜๋ฉฐ, ์š”์†Œ์˜ ๋” ์งง์€ ๋„ˆ๋น„์— ๋งž์ถฐ์ง- ์ด๋ฏธ์ง€๊ฐ€ ์ž˜๋ฆฌ์ง€ ์•Š์Œ

์˜ˆ์ œ#

<div class="box"></div>
.box {width: 400px;height: 300px;border: 2px solid red;margin: 50px;background-image:url("https://heropy.blog/css/images/logo.png");/* 500x500 */background-repeat: no-repeat;background-size: contain;}

Reference#

ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong