Background
1. ๊ฐ์
Background ๊ด๋ จ ํ๋กํผํฐ๋ ํด๋น ์์์ ๋ฐฐ๊ฒฝ์ผ๋ก ์ด๋ฏธ์ง ๋๋ ์์์ ์ ์ํ๋ค. ์น ๋ฌธ์์ ์ ์ฒด ๋ฐฐ๊ฒฝ๋ฟ๋ง ์๋๋ผ ํ ์คํธ, ๋ชฉ๋ก ๋ฑ ํน์ ํ ์์์๋ ๋ฐฐ๊ฒฝ์ ์ง์ ํ ์ ์๋ค.
2. background-image ํ๋กํผํฐ
์์์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์ง์ ํ๋ค.
๊ธฐ๋ณธํ background-color: url("์ด๋ฏธ์ง ํ์ผ ๊ฒฝ๋ก")
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div {
height: 100vh;
background-image: url("https://raw.githubusercontent.com/iteach12/html_css_js_basic/1c84313da723024e72882618d203e5f9acab045b/images/logo.png");
}
</style>
</head>
<body>
<div>Background-image</div>
</body>
</html>

3. background-repeat ํ๋กํผํฐ
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ๋ฐ๋ณต์ ์ง์ ํ๋ค. ์์ง, ์ํ ๋๋ ์์ง๊ณผ ์ํ ๋ชจ๋์ ๋ฐ๋ณต์ ์ง์ ํ ์ ์๋ค.
repeat
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ๊ฐ๋ ์ฐฐ ๋๊น์ง ๊ฐ๋ก์ ์ธ๋ก๋ก ๋ฐ๋ณตํ๋ค. ๊ธฐ๋ณธ๊ฐ
repeat-x
๋ธ๋ผ์ฐ์ ํ๋ฉด ๋๋น์ ๊ฐ๋ ์ฐฐ ๋๊น์ง ๊ฐ๋ก๋ก ๋ฐ๋ณตํ๋ค.
repeat-y
๋ธ๋ผ์ฐ์ ํ๋ฉด ๋์ด์ ๊ฐ๋ ์ฐฐ ๋๊น์ง ์ธ๋ก๋ก ๋ฐ๋ณตํ๋ค.
no-repeat
ํ ๋ฒ๋ง ํ์ํ๊ณ ๋ฐ๋ณตํ์ง ์๋๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div {
height: 100vh;
background-image: url("http://poiemaweb.com/img/bg/dot.png");
/* ๊ธฐ๋ณธ๊ฐ */
background-repeat: repeat;
}
</style>
</head>
<body>
<div>Background-image</div>
</body>
</html>

๋ฐ๋ณต ์ถ๋ ฅ์ ๋ฉ์ถ๊ณ ์ถ์ ๊ฒฝ์ฐ, background-repeat
ํ๋กํผํฐ๊ฐ์ no-repeat๋ฅผ ์ค์ ํ๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div {
height: 100vh;
background-image: url("http://poiemaweb.com/img/bg/dot.png");
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div>Background-image</div>
</body>
</html>

background-image
์ ๋ณต์๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ค์ ํ ๊ฒฝ์ฐ, ๋จผ์ ์ค์ ๋ ์ด๋ฏธ์ง๊ฐ ์ ๋ฉด์ ์ถ๋ ฅ๋๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
div {
height: 100vh;
background-image: url("http://poiemaweb.com/img/bg/dot.png"),
url("http://poiemaweb.com/img/bg/paper.gif");
background-repeat: no-repeat, repeat;
}
</style>
</head>
<body>
<div>Background-image</div>
</body>
</html>

4. background-size ํ๋กํผํฐ
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ์ฌ์ด์ฆ๋ฅผ ์ง์ ํ๋ค. ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ๊ณ ์ ๋น์จ์ ์ ์งํ๊ธฐ ๋๋ฌธ์ ์ค์ ์ ๋ฐ๋ผ ์ด๋ฏธ์ง์ ์ผ๋ถ๊ฐ ๋ณด์ด์ง ์์ ์ ์๋ค. background-size
ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ ์ ์๋ค. ํนํ ํ๋ฉด์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ ์ฑ์์ผ ํ ๊ฒฝ์ฐ์ ์ ์ฉํ๋ค.
์๋์ ํ๋ background-size ํ๋กํผํฐ๊ฐ์ด๋ค.
auto
์๋ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ํฌ๊ธฐ๋งํผ ํ์ํ๋ค. ๊ธฐ๋ณธ๊ฐ
contain
์์ ์์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๊ฐ ๋ค ๋ค์ด์ค๋๋ก ์ด๋ฏธ์ง๋ฅผ ํ๋ ์ถ์ํ๋ค.
cover
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ก ์์๋ฅผ ๋ชจ๋ ๋ฎ๋๋ก ์ด๋ฏธ์ง๋ฅผ ํ๋ ์ถ์ํ๋ค.
<ํฌ๊ธฐ>
์ด๋ฏธ์ง์ ๋๋น์ ๋์ด๋ฅผ ์ง์ ํ๋ค. ๊ฐ์ด ํ๋๋ง ์ฃผ์ด์ง ๊ฒฝ์ฐ ๋๋น๊ฐ์ผ๋ก ์ธ์ํ๋ฉฐ, ์ด๋ฏธ์ง์ ๋๋น์ ๋๋น๊ฐ์ ๋ง์ถ ๋์ด๊ฐ๋ ์๋ ๊ณ์ฐ๋๋ค.
<๋ฐฑ๋ถ์จ>
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๊ฐ ๋ค์ด๊ฐ ์์์ ํฌ๊ธฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฐ์ ๋ฐฑ๋ถ์จ๋ก ์ง์ ํ๊ณ ๊ทธ ํฌ๊ธฐ์ ๋ง๋๋ก ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ํ๋ ์ถ์ํ๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.bg {
display: inline-block;
width: 200px;
height: 200px;
background-image: url("https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80");
background-repeat: no-repeat;
}
.bg1 {
background-size: auto;
}
.bg2 {
background-size: contain;
}
.bg3 {
background-size: cover;
}
.bg4 {
/* ๋์ด๋ ์๋ ๊ณ์ฐ๋๋ค. */
background-size: 100px;
}
.bg5 {
background-size: 80% 80%;
}
</style>
</head>
<body>
<div class="bg bg1">background-size: auto</div>
<div class="bg bg2">background-size: contain</div>
<div class="bg bg3">background-size: cover</div>
<div class="bg bg4">background-size: ํฌ๊ธฐ</div>
<div class="bg bg5">background-size: ๋ฐฑ๋ถ์จ</div>
</body>
</html>

5. background-attachment ํ๋กํผํฐ
์ผ๋ฐ์ ์ผ๋ก ํ๋ฉด์ ์คํฌ๋กคํ๋ฉด ๋ฌธ์ ์ ์ฒด๊ฐ ์์ง์ด๋ฏ๋ก ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ ํจ๊ป ์คํฌ๋กค๋๋ค. ํ๋ฉด์ด ์คํฌ๋กค๋๋๋ผ๋ ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง๋ ์คํฌ๋กค๋์ง ์๊ณ ๊ณ ์ ๋์ด ์๊ฒ ํ๋ ค๋ฉด background-attachment
ํ๋กํผํฐ์ fixed ํค์๋๋ฅผ ์ง์ ํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
*,
*:after,
*:before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.bg-wrap {
min-height: 600px;
height: 100%;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
overflow: auto;
}
.parallax {
background-image: url("http://poiemaweb.com/img/bg/stock-photo-125979219.jpg");
background-attachment: fixed;
}
.normal {
background-image: url("http://poiemaweb.com/img/bg/stock-photo-155153867.jpg");
background-attachment: fixed;
}
p {
padding: 30px;
width: 600px;
height: 320px;
margin: 320px auto;
background-color: white;
text-align: center;
display: flex;
align-items: center;
}
</style>
</head>
<body>
<div class="bg-wrap parallax">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia harum
consequatur reprehenderit id nulla tenetur fugit aperiam natus
asperiores quos facilis veniam perferendis omnis doloremque ipsum,
delectus laudantium, commodi accusamus?
</p>
</div>
<div class="bg-wrap normal">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia harum
consequatur reprehenderit id nulla tenetur fugit aperiam natus
asperiores quos facilis veniam perferendis omnis doloremque ipsum,
delectus laudantium, commodi accusamus?
</p>
</div>
</body>
</html>

์์ ์๋์ ๋ฐฐ๊ฒฝํ๋ฉด์ ๋ชจ๋ ๊ณ ์ ์์ผฐ๊ธฐ ๋๋ฌธ์ ์คํฌ๋กค์ ํด๋ ์์ง์ด์ง ์๋๋ค.
์ง๊ธ๊น์ง ํ ๋ฒ๋ ์ฌ์ฉํ์ง ์์๋ ํ๋กํผํฐ์ด๋ค. ํ์ง๋ง ์ง์ ์ ์ฉํ๊ณ ๋ณด๋ ์์ ๋ฉ์ ธ..
6. background-position ํ๋กํผํฐ
์ผ๋ฐ์ ์ผ๋ก background-image
๋ ์ข์๋จ๋ถํฐ ์ด๋ฏธ์ง๋ฅผ ์ถ๋ ฅํ๋ค. background-position
์์ฑ์ ์ด์ฉํ๋ฉด ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง์ ์ํ ์์น ๋๋ ์์ง ์์น์ ๊ฐ์ ์ง์ ํ ์ ์๋ค.
๊ธฐ๋ณธ๊ฐ background-position: 0% 0%;
๊ธฐ๋ณธํ background-position: <์ํ ์์น> <์์ง ์์น>;
ํ๋กํผํฐ๊ฐ์ ํ๋๋ง ์ง์ ํ๋ค๋ฉด ์น ๋ธ๋ผ์ฐ์ ์์๋ ์ง์ ํ ๊ฐ์ ์ํ ์์น๊ฐ์ผ๋ก ๊ฐ์ฃผํ๊ณ , ์์ง ์์นซ๊ฐ์ 50%๋ center๋ก ๊ฐ์ฃผํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: inline-block;
width: 400px;
height: 400px;
background-color: orange;
background-image: url("https://images.unsplash.com/photo-1657664043009-c4975cb4eed3?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1074&q=80");
background-size: 150px 150px;
background-repeat: no-repeat;
margin-top: 10px;
}
.ex1 {
background-position: top;
}
.ex2 {
background-position: bottom;
}
.ex3 {
background-position: center;
}
.ex4 {
background-position: left;
}
.ex5 {
background-position: right;
}
.ex6 {
background-position: 25% 75%;
}
.ex7 {
background-position: 10px 20px;
}
</style>
</head>
<body>
<div class="ex1">top</div>
<div class="ex2">bottom</div>
<div class="ex3">center</div>
<div class="ex4">left</div>
<div class="ex5">right</div>
<div class="ex6">25% 75%</div>
<div class="ex7">10px 20px</div>
</body>
</html>

7. background-origin ํ๋กํผํฐ
background-origin
ํ๋กํผํฐ๋ฅผ ์ง์ ํ์ฌ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ํจ๋ฉ๊น์ง ํ์ํ๊ฑฐ๋ ํ
๋๋ฆฌ๊น์ง ํฌํจํด์ ํ์ํ ์ ์๋ค.
์๋์ ํ๋ background-origin
ํ๋กํผํฐ๊ฐ์ด๋ค.
content-box
๋ฐ์ค ๋ชจ๋ธ์์ ๋ด์ฉ ๋ถ๋ถ์๋ง ์ด๋ฏธ์ง๋ฅผ ํ์ํ๋ค. ๊ธฐ๋ณธ๊ฐ
padding-box
๋ฐ์ค ๋ชจ๋ธ์์ ํจ๋ฉ๊น์ง ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ํ์ํ๋ค.
border-box
๋ฐ์ค ๋ชจ๋ธ์์ ํ ๋๋ฆฌ๊น์ง ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ํ์ํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: inline-block;
width: 400px;
height: 400px;
background-color: orange;
background-image: url("https://images.unsplash.com/photo-1657664043009-c4975cb4eed3?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1074&q=80");
background-size: 150px 150px;
background-repeat: no-repeat;
margin-top: 10px;
border: 50px solid rgba(10, 10, 10, 0.3);
padding: 20px;
background-position: top right;
}
.ex1 {
/* ๊ธฐ๋ณธ๊ฐ */
background-origin: content-box;
}
.ex2 {
background-origin: padding-box;
}
.ex3 {
background-origin: border-box;
}
</style>
</head>
<body>
<div class="ex1">content-box</div>
<div class="ex2">padding-box</div>
<div class="ex3">border-box</div>
</body>
</html>

8. background-color ํ๋กํผํฐ
๋ฐฐ๊ฒฝ์์ ์ง์ ํ๋ ค๋ฉด ๋ฐฐ๊ฒฝ์ ๋ฃ๊ณ ์ถ์ ์์์ ์คํ์ผ ๊ท์น์ ๋ง๋ค ๋ background-color
ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ค. background-color
๋ 16์ง์๋ rgb๊ฐ ๋๋ ์์ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ์ง์ ํ๋ค. ๋ํ transparentํค์๋๋ฅผ ์ง์ ํ ์ ์๋ค.
div {
background-color: #000000;
background-color: rgb(255, 255, 255);
background-color: white;
}
9. background-clip ํ๋กํผํฐ
๋ฐฐ๊ฒฝ์ ๋ฃ๊ณ ์ถ์ ์์๋ง๋ค ์์ฑ์ ์ ๋ ฅํ๋ฉด ๋์ง๋ง ๋ฐ์ค ๋ชจ๋ธ ๊ด์ ์์ ๋ฐฐ๊ฒฝ์ ๋ฒ์๋ฅผ ์กฐ์ ํ ์๋ ์๋ค. ํ ๋๋ฆฌ๊น์ง ์ ์ฉํ ์ง, ํจ๋ฉ ๋ฒ์๊น์ง ์ ์ฉํ ์ง, ์๋๋ฉด ๋ด์ฉ ๋ถ๋ถ์๋ง ์ ์ฉํ ์ง ์ ํํ ์ ์๋ค.
border-box
๋ฐ์ค ๋ชจ๋ธ์ ๊ฐ์ฅ ์ธ๊ณฝ์ธ ํ ๋๋ฆฌ๊น์ง ์ ์ฉํ๋ค. ๊ธฐ๋ณธ๊ฐ
padding-box
๋ฐ์ค ๋ชจ๋ธ์์ ํ ๋๋ฆฌ๋ฅผ ๋บ ํจ๋ฉ ๋ฒ์๊น์ง๋ง ์ ์ฉํ๋ค.
content-box
๋ฐ์ค ๋ชจ๋ธ์์ ๋ด์ฉ(์ฝํ ์ธ ) ๋ถ๋ถ์๋ง ์ ์ฉํ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
div {
display: inline-block;
width: 400px;
height: 400px;
background-color: orange;
background-size: 150px 150px;
background-repeat: no-repeat;
margin-top: 10px;
border: 50px solid rgba(10, 10, 10, 0.3);
padding: 20px;
background-position: top right;
}
.ex1 {
background-clip: content-box;
}
.ex2 {
background-clip: padding-box;
}
.ex3 {
/* ๊ธฐ๋ณธ๊ฐ */
background-clip: border-box;
}
</style>
</head>
<body>
<div class="ex1">content-box</div>
<div class="ex2">padding-box</div>
<div class="ex3">border-box</div>
</body>
</html>

10. background Shorthand
background-color, background-image, background-repeat, background-position๋ฅผ ํ๋ฒ์ ์ ์ํ๊ธฐ ์ํ Shorthand Syntax์ด๋ค.
๊ธฐ๋ณธํ background: color || image || repeat || attachment || position
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 400px;
height: 400px;
background: orchid
url("https://images.unsplash.com/photo-1446776811953-b23d57bd21aa?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1172&q=80")
no-repeat top left;
background-size: 200px 200px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

11. Conclusion
background
๊ด๋ จ ํ๋กํผํฐ๋ ์์ฃผ ์ฌ์ฉํ๋ ๊ฒ ์ค ํ๋์ด๋ค. ๊ทธ๋์ ๋ง์ด ์ฌ์ฉํด์๋ค. ์ฒ์ ํด๋น ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ ๋์๋backgrounbd-position
,background-size
๋ฅผ ํตํด ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ค์ ์ ๋ง๊ฒ ๊ตฌ์ฑํ๋ ๊ฒ ๊น์ง๋ง ํด๋ ๊ต์ฅํ ๊ธฐ๋ถ์ด ์ข์๊ณ ์ดํ์๋ ๋ค๋ฅธ ํ๋กํผํฐ์ ๋ํด ์ ์์๋ณด์ง ์๊ณ ์ง๋ด์๋ค. ์ด๋ฒ์ ๊ณต๋ถํ๋ฉด์ ํ ๋ฒ๋ ์ฌ์ฉํด๋ณด์ง ์์๋background-attachment
ํ๋กํผํฐ๋ฅผ ์ฒ์ ์ฌ์ฉํด๋ดค๋๋ฐ ์ ๋ง ์ ์ธ๊ณ์๋ค. ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง๊ฐ ์คํฌ๋กค๋์ง ์๊ณ ๊ทธ ์๋ฆฌ ๊ทธ๋๋ก ์๋ค๋ ๊ฒ์ด ๊ธ๋ก๋ ์ดํด๊ฐ ์ ๋์ง ์์๋๋ฐ ์ง์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ๊ฒฐ๊ณผ๋ฌผ์ ๋ณด๋ ๋๋ฌด ๋ฉ์ก๋ค. ๊ทธ๋์ ํด๋น ํ๋กํผํฐ๋ฅผ ๋ฐฐ์ฐ๊ณ ์ ๊น ๋ฉ์ถ ํ ๊ณ์ํด์ ์ฌ๋ฌ๊ฐ์ง ์๋๋ฅผ ํ๋ค. ์ด ํ๋กํผํฐ๋ฅผ ์์ผ๋ก ์ ํ์ฉํ์ฌ ๋ ๋ฉ์ง ์น ํ์ด์ฆ๋ฅผ ๋ง๋ค์ด์ผ ๊ฒ ๋ค๐
์ฐธ๊ณ
poiemaweb 2-6 ๋ฐฑ๊ทธ๋ผ์ด๋ ๋์ - HTML + CSS + ์๋ฐ์คํฌ๋ฆฝํธ ์น ํ์ค์ ์ ์
๐ 2022-07-19
Last updated