CSS โญ โข ํ์ ์์ฑ - (2) ํ๋ฉด ๋ฐฐ์น
โญ์์น ์์ฑ์ผ๋ก HTML ์์ ๋ฐฐ์น
์์น ์์ฑ์ ์์๋ฅผ ์๋์ ์ผ๋ก ๋๋ ์ ๋์ ์ผ๋ก ๋ฐฐ์นํ ์ ์์ต๋๋ค.
postion ์์ฑ
๐ position์ ์์์ ์์น๋ฅผ ์ง์ ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์์ฑ
๊ฐ ์์ฑ์ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ง์ ๋ ๊ฑฐ๋ฆฌ๋งํผ ์ด๋์ํต๋๋ค.
position ์์ฑ๊ณผ ํจ๊ป ์ฌ์ฉ๋๋ ์์ฑ์ผ๋ก z-index๊ฐ ์์ต๋๋ค. z-index๋ ์์์ ์์ ์์๋ฅผ ์ง์ ํ๋๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ๊ฐ์ด ๋์ ์๋ก ์์ ์์ด๊ฒ ๋ฉ๋๋ค.
position ์์ฑ์ ๋ ์ด์์์ ๊ตฌ์ฑํ๋ ๋ฐ ๋งค์ฐ ์ค์ํ ์ญํ ์ ํฉ๋๋ค.
ํนํ absolute์ fixed ๊ฐ์ ์์๋ฅผ ์์ ๋กญ๊ฒ ๋ฐฐ์นํ ์ ์๋๋ก ๋์์ค๋๋ค. ๊ทธ๋ฌ๋ ์ด๋ฌํ ์์ฑ์ ์ฌ์ฉํ ๋์๋ ๋ค๋ฅธ ์์๋ค๊ณผ ์ถฉ๋์ด๋ ๊ฒน์นจ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์กฐ์ฌํด์ผ ํฉ๋๋ค.
static
๐ positoin ์์ฑ๊ฐ์ static์ผ๋ก ์ง์ ํ๋ฉด ์๋ฌด๋ฐ ๋ณํ ์์
relative
๐ ์์๋ฅผ ์ขํ ์์ฑ์ ๋ฐ๋ผ ์ฌ๋ฐฐ์นํ ์ ์์ต๋๋ค.
์๋ ์์น ๊ธฐ์ค์ผ๋ก ์๋์ ์ผ๋ก ๋ฐฐ์น ๋จ.
๐ ๋ถ๋ชจ ์์์ ์์น๋ฅผ ๋ฃ๋๋ค๋ฉด relative
๋ฅผ..!
absolute
๐ ์์๋ฅผ ๋ถ๋ชจ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋์ ์ธ ์์น์ ๋ฐฐ์นํ๋ ๋ฐ ์ฌ์ฉ
์์น์์ ๋ถ๋ชจ ์์ ๊ธฐ์ค, ์ฆ ๋ถ๋ชจ์ position ์์ฑ์ด ๋ฐ๋์ relative, absolute, fixed ์ค ํ๋์ฌ์ผ ํฉ๋๋ค.
์์ ์์๊ฐ ์์ผ๋ฉด html ๊ธฐ์ค์ผ๋ก ์์์ ๊ธฐ์ค ์์น ๊ฒฐ
์๋ ์์น
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>์์น ์์ฑ์ผ๋ก html ์์ ๋ฐฐ์นํ๊ธฐ</title>
<style>
body {
margin: 0;
}
.box {
width: 100px;
height: 100px;
}
.red-box {
background-color: red;
}
.green-box {
background-color: green;
}
</style>
</head>
<body>
<div class="box red-box"></div>
<div class="box green-box"></div>
</body>
</html>
relative ์ ์ฉ
absolute ์ ์ฉ
fixed
๐์์๋ฅผ ๋ทฐํฌํธ ๊ธฐ์ค์ผ๋ก ๊ณ ์ ๋ ์์น์ ๋ฐฐ์นํ๋ ๋ฐ ์ฌ์ฉ
์คํฌ๋กค์ด ๋์ด๋ ํ๋ฉด์์ ์ฌ๋ผ์ง์ง ์๊ณ ํญ์ ๊ณ ์ ๋ ์์น์ ์์ต๋๋ค.
absolute์ ๋๊ฐ์ด ์๋ํ๋ ๊ฒ ์ฒ๋ผ ๋ณด์ด์ง๋ง, ์คํฌ๋กค ํ์ ๋ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
๐ ์๋จ ๊ณ ์ ๋ฉ๋ด๋ฐ ๊ฐ์ ....
sticky
๐์คํฌ๋กค์ด ๋ฐ์ํ ๋ ์ง์ ํ ์์น์ ๊ณ ์ ๋๋ ์์๋ฅผ ๋ง๋๋ ๋ฐ ์ฌ์ฉ
๐ fixed์ ๋ฌ๋ฆฌ ์คํฌ๋กค์ด ๋ฐ์ํ์ง ์์ผ๋ฉด ์ผ๋ฐ์ ์ธ ๋ฌธ์ ํ๋ฆ์ ๋ฐ๋ผ ๋ฐฐ์น๋จ.
๐ ์คํฌ๋กค์ด ๋ฐ์ํ๋ฉด ์ง์ ํ ์์น๊น์ง๋ง ์์ง์ด๊ณ ๊ทธ ๋ค๋ก๋ ๊ณ ์ ๋จ.
๐ ์ฌ์ด๋๋ฐ ๋๋...?
z-index
๐ position ์์ฑ์ผ๋ก ๋ฐฐ์นํ ์์์ ์์ด๋ ์์น(๋๋ฎ์ด)๋ฅผ ๊ฒฐ์ (z์ถ)
๐์ฐ์ ์์ ๋ณ๊ฒฝ, ๊ฐ์ด ์ปค์ง ์๋ก ์ฐ์ ์์๊ฐ ์ปค์ง. ์์น ๋ค์ง๊ธฐ(?)
๋จ, ๋๋ฑ ์กฐ๊ฑด์ผ ๋์๋ง ์ ์ฉ๋จ. (static์ด๋ฉด ์ ์ฉ ์๋จ)
๐ธ ์ฐ์ต๋ฌธ์
์๋์ ํ๋ฉด๊ณผ ๊ฐ์ด ๋ฐฐ์น ํด ๋ณด์ธ์.
- ์ ์ฒด ๋ฐ์ค ๊ฐ๋ก ํฌ๊ธฐ 300px (๋ฐฐ๊ฒฝ์์ royalblue)
- ๋ด๋ถ ๋ฐ์ค ํฌ๊ธฐ 100px์ ์ ์ฌ๊ฐํ (๋ฐฐ๊ฒฝ ์์ orange)
- ์ ๋๊ป 4px
๋ด๊ฐ ์์ฑํ ์ฝ๋
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
margin: 0;
}
.background {
width: 300px;
height: 200px;
background-color: royalblue;
}
.box {
width: 100px;
height: 100px;
background-color: orange;
border: 4px dashed red;
box-sizing: border-box;
}
.box1 {
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
.box2 {
position: absolute;
left: 50px;
top: 50px;
z-index: 2;
}
.box3 {
position: relative;
bottom: 100px;
z-index: 3;
}
</style>
</head>
<body>
<div class="background"></div>
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
</body>
</html>
๊ฐ์ฌ๋ ์ฝ๋
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.container {
width: 300px;
background-color: royalblue;
position: relative;
}
.container .item {
width: 100px;
height: 100px;
border: 4px dashed red;
background-color: orange;
}
.container .item:nth-child(2) {
position: absolute;
left: 50px;
top: 50px;
}
.container .item:nth-child(3) {
z-index: 1;
position: relative;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
</body>
</html>
๋ด ์ฝ๋์ ๋ค๋ฅธ ์
- ํ๋์ ๋ฐ์ค๋ฅผ ๋ถ๋ชจ ์์๋ก ๋ฃ๊ณ ์ค๋ ์ง ๋ฐ์ค 3๊ฐ๋ฅผ ์์์ผ๋ก ๋์ด ๋ถ๋ชจ ๋ฐ์ค ๋์ด๊ฐ ์์ ๋ฐ์ค ํฌ๊ธฐ์ ๋ง์ถฐ ๋์ด๋๋๋ก ํ๋ค.
- ์์๋ค์ ์ ํ์๋ก ์ง์ ํ์ฌ ์์น ์์ฑ ์ ์ฉ
- ์์์ด ์ฌ๋ฌ๊ฐ ์ผ ๋ ๊ตณ์ด ํด๋์ค ์ฌ๋ฌ๊ฐ ๋ง๋ค์ง ๋ง๊ณ
nth-child(n)
์์ฑ ์ด์ฉํ ๊ฒ
float
๐ ๋์ ์์๋ฅผ ๊ณต์ค์ ๋์ ๋ค์์ ์ค๋ ์์๋ฅผ ์ฃผ๋ณ์ ์์ฐ์ค๋ฝ๊ฒ ๋ฐฐ์น
๐ margin์ ์์ฃผ๋ฉด ์์ ๋ฑ ๋ถ์ด๋ฒ๋ฆผ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
img {
float: left;
margin-right: 14px;
}
</style>
</head>
<body>
<img src="./images/coffee.jpg" alt="์ปคํผ">
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit.
Corrupti ut dolor ratione blanditiis iste, harum fugit, soluta eum
obcaecati, expedita perspiciatis quam culpa? Doloribus repellendus
labore sed inventore iusto eveniet. Lorem, ipsum dolor sit amet
consectetur adipisicing elit. Maiores at natus aliquid numquam vitae nisi,
voluptates laudantium saepe error sapiente nobis adipisci et accusamus
molestiae commodi consequuntur earum, ut dicta? Lorem ipsum dolor sit amet consectetur,
adipisicing elit. Id molestiae perspiciatis non temporibus voluptates alias
ducimus libero esse recusandae placeat, natus quo mollitia sint enim?
Ex numquam non officia ipsam?</p>
</body>
</html>
๋ธ๋ก ์์๋ฅผ ์ธ๋ผ์ธ ์์์ฒ๋ผ ๋ฐฐ์น ํ๊ธฐ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div {
color: white;
}
.red-box {
background-color: red;
float: left;
width: 100px;
}
.blue-box {
background-color: blue;
width: 100px;
float: left;
}
.green-box {
background-color: green;
width: 100px;
float: left;
}
</style>
</head>
<body>
<div class="red-box">red-box</div>
<div class="blue-box">blue-box</div>
<div class="green-box">green-box</div>
</body>
</html>
- ๊ณ์ ๋ฐฐ์นํ๋ ค๋ฉด float:left ํด์ผํจ..
- ํ์ง๋ง
float
๋ณด๋จflex-box
๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด ๋ ์ง๊ด์ ์ด๊ณ ์ข์.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div {
color: white;
}
.red-box {
background-color: red;
float: left;
width: 100px;
}
.blue-box {
background-color: blue;
width: 100px;
float: left;
}
.green-box {
background-color: green;
width: 100px;
float: left;
}
p {
color: black;
clear: left;
}
</style>
</head>
<body>
<div class="red-box">red-box</div>
<div class="blue-box">blue-box</div>
<div class="green-box">green-box</div>
<div>
<p>์ฌ๊ธฐ์ ๋ถํฐ๋ ๋ณธ๋ฌธ ์
๋๋ค.</p>
</div>
</body>
</html>
clear
์ ์ฐ๋ฉดp
๋ ๋ฐ์ค ์์ ๋ถ์!- ์๋๋ก ๋ด๋ฆฌ๋ ค๋ฉด
clear:left;
์ฐ๋ฉด ๋๋ค.