็ก์
background ์์ฑ ๋ณธ๋ฌธ
๐ขโ โbackground
์์์ ๋ฐฐ๊ฒฝ ์์, ์ด๋ฏธ์ง, ์์น, ๋ฐ๋ณต ๋ฐฉ์ ๋ฑ์ ์ค์ ํ๋ ์์ฑ.
1๏ธโฃ ๊ฐ๋ณ ๋ฐฐ๊ฒฝ ์์ฑ
๋ฐฐ๊ฒฝ์ ์ธ๋ฐํ๊ฒ ์ ์ดํ๊ธฐ ์ํ ์ฃผ์ ์์ฑ.
๐น background-color
๋ฐฐ๊ฒฝ ์์ ์ง์ .
.box {
background-color: #3498db;
}
- ์์ ์ด๋ฆ
- Hex Code (16์ง์ ์ฝ๋)
- RGB / RGBA
- HSL / HSLA
๐น background-image
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ๊ฒฝ๋ก ์ง์ .
.box {
background-image: url('image.jpg');
}
๐น background-repeat
์ด๋ฏธ์ง ๋ฐ๋ณต ์ฌ๋ถ.
.box {
background-repeat: no-repeat;
}
- repeat: ๋ฐ๋ณต. (๊ธฐ๋ณธ๊ฐ)
- no-repeat: ๋ฐ๋ณต ์์.
- repeat-x: ๊ฐ๋ก ๋ฐฉํฅ๋ง ๋ฐ๋ณต.
- repeat-y: ์ธ๋ก ๋ฐฉํฅ๋ง ๋ฐ๋ณต.
๐น background-position
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์์น.
.box {
background-position: center;
}
- left
- right
- top
- bottom
- center
- ๋๋ px, %
๐น background-size
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ํฌ๊ธฐ.
.box {
background-size: cover;
}
- cover: ๋น์จ ์ ์งํ๋ฉฐ ๊ฝ ์ฑ์. (์ด๋ฏธ์ง ์ผ๋ถ๊ฐ ์๋ฆด ์ ์๋ค)
- contain: ๋น์จ ์ ์งํ๋ฉฐ ์ด๋ฏธ์ง ์ ์ฒด ํ์. (์ฌ๋ฐฑ์ด ์๊ธธ ์ ์๋ค)
๐น background-attachment
์คํฌ๋กค ์ ๋ฐฐ๊ฒฝ ๊ณ ์ ์ฌ๋ถ.
.box {
background-attachment: fixed;
}
- scroll: ์์์ ํจ๊ป ์คํฌ๋กค. (๊ธฐ๋ณธ๊ฐ)
- fixed: ๋ทฐํฌํธ(ํ๋ฉด)์ ๊ณ ์ .
- local: ์์ ๋ด๋ถ์ ์คํฌ๋กค์ ๋ฐ๋ผ ์์ง์.
2๏ธโฃโ ๋ฐฐ๊ฒฝ ํด๋ฆฝ๊ณผ ์์ (Clip & Origin)
๋ฐฐ๊ฒฝ์ด ์ด๋๊น์ง ๊ทธ๋ ค์ง์ง(Clip)์, ์ด๋์๋ถํฐ ์์ํ ์ง(Origin) ๊ธฐ์ค ์์ญ์ ๊ฒฐ์ ํ๋ ์์ฑ.
๐น background-clip
๋ฐฐ๊ฒฝ์ด ๊ทธ๋ ค์ง๋ ๋ฒ์๋ฅผ ์ค์ .
.box. {
background-clip: padding-box;
}
- border-box: ํ ๋๋ฆฌ๊น์ง ๋ฐฐ๊ฒฝ ์ฑ์์ง. (๊ธฐ๋ณธ๊ฐ)
- padding-box: ํจ๋ฉ ์์ญ๊น์ง๋ง ๋ฐฐ๊ฒฝ ์ฑ์์ง.
- content-box: ์ฝํ ์ธ ์์ญ๊น์ง๋ง ๋ฐฐ๊ฒฝ ์ฑ์์ง.
๐น background-origin
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ์์ ๊ธฐ์ค์ ์ ์ค์ .
.box. {
background-origin: border-box;
}
- border-box: ํ ๋๋ฆฌ ๊ธฐ์ค์ผ๋ก ์ด๋ฏธ์ง ์์.
- padding-box: ํจ๋ฉ ์์ญ ๊ธฐ์ค์ผ๋ก ์ด๋ฏธ์ง ์์. (๊ธฐ๋ณธ๊ฐ)
- content-box: ์ฝํ ์ธ ์์ญ ๊ธฐ์ค์ผ๋ก ์ด๋ฏธ์ง ์์.
3๏ธโฃโ ๋ค์ค ๋ฐฐ๊ฒฝ (Multiple Backgrounds)
ํ๋์ ์์์ ์ฌ๋ฌ ๊ฐ์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ๊ฒน์ณ์ ์ฌ์ฉํ ์ ์๋ ์์ฑ.
.multi-bg {
background-image: url('overlay.png'), url('base-photo.jpg');
background-position: center top, left center, right bottom;
background-repeat: no-repeat, repeat-x, repeat-y;
background-size: contain, cover, auto;
}
- ๋จผ์ ์ด ์ด๋ฏธ์ง๊ฐ ๋งจ ์(์ฌ์ฉ์์๊ฒ ๊ฐ๊น์ด ์ชฝ)์ ํ์๋จ
๐จ ์ถ์ฝํ ์์ฑ (Shorthand)
๋ฐฐ๊ฒฝ ์์ฑ์ ํ ์ค๋ก ์ ์ํ๋ ์์ฑ.
.shorthand {
background: <color> <image> <repeat> <attachment> <position> / <size>;
}
- ๋ฌธ๋ฒ: background: color url('img.jpg') repeat attachment position / size;
- ์ฃผ์: <size> ๋ <position> ๋ค์ / ๋ก ๊ตฌ๋ถํด์ผ ํ๋ค.
- ํ์ ์๋ ์์ฑ์ ์๋ต ๊ฐ๋ฅ (url('img.jpg'), no-repeat)
๐โ POINT ๐โ
โ๏ธ background-size: cover; ๋ ๋ฐ์ํ ์น ๋์์ธ์์ ์ด๋ฏธ์ง๋ฅผ ์ฐ๊ทธ๋ฌ๋จ๋ฆฌ์ง ์๊ณ ์์ญ์ ์ฑ์ธ ๋ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๋ ์์ฑ์ด๋ค. โ๏ธ
โ๏ธ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์์ ๊ธ์๊ฐ ์ ๋ณด์ด๊ฒ ํ๋ ค๋ฉด rgba()๋ฅผ ์ด์ฉํ ๋ฐํฌ๋ช ํ ์์์ด๋ ๊ทธ๋ผ๋ฐ์ด์ ์ค๋ฒ๋ ์ด๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ฉด ์ข๋ค. โ๏ธ
โ๏ธ ์ด๋ฏธ์ง๊ฐ ๋ก๋๋์ง ์์ ์ํฉ์ ๋๋นํด ํญ์ ์ ์ฌํ background-color ๋ฅผ ํจ๊ป ์ง์ ํ๋ค. โ๏ธ
๐ ๋ฐฐ๊ฒฝ ์ฃผ์ ์์ฑ ์์ฝํ๐
| ์์ฑ | ์ค๋ช | ์ฃผ์ ๊ฐ ์์ |
| background-color | ๋ฐฐ๊ฒฝ์ | red, #ffffff, rgba(0,0,0,0.5) |
| background-image | ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง | url('image.jpg'), linear-gradient(...) |
| background-size | ์ด๋ฏธ์ง ํฌ๊ธฐ | cover, contain, 100% 50% |
| background-position | ์ด๋ฏธ์ง ์์น | center, bottom right, 50% 50% |
| background-clip | ๋ฐฐ๊ฒฝ ์ ์ฉ ๋ฒ์ | border-box, padding-box, content-box |
โ๏ธ ์ค์ ์์ ์ฝ๋: ํ ์คํฌ๋ฆฐ ๋ฐฐ๊ฒฝ ๋ง๋ค๊ธฐ โ๏ธ
<style>
.main-banner {
width: 100%;
height: 100vh; /* ํ๋ฉด ๋์ด ์ ์ฒด */
/* ๋ฐฐ๊ฒฝ ์ค์ */
background-color: #333;
background-image: url('nature.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed;
/* ํ
์คํธ ์ ๋ ฌ (Flexbox ํ์ฉ) */
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 3rem;
}
</style>
<div class="main-banner">
Welcome to My World
</div>
๐ CSS Backgrounds training ๐
https://www.w3schools.com/css/css_background.asp
W3Schools.com
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
'<style>' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| RWD (Responsive Web Design) ๋ฐ์ํ ์น ๋์์ธ (0) | 2026.03.23 |
|---|---|
| gradients ํจ์ (0) | 2026.03.20 |
| CSS์ Position ์์ฑ (0) | 2026.03.19 |
| float ์์ฑ (0) | 2026.03.19 |
| display ์์ฑ (0) | 2026.03.19 |