์ด๋ฆ์์์ฒ๋ผ, position์ด๋ HTML ํ๊ทธ์ ์์น๋ฅผ ์ง์ ํด์ฃผ๋ ์์ฑ์ ๋๋ค. position ์์ฑ์ ์ด์ฉํด ์ฐ๋ฆฌ๋ ํ์ด์ง์ ๋ ์ด์์์ ๊ฒฐ์ ํ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ํ๊ทธ๋ค์ ๋ฐ๋ก position ์์ฑ๊ฐ์ ์ฃผ์ง ์์๋ค๋ฉด static ๊ฐ์ ๊ฐ์ง๋๋ค. ์ฆ html์ ์ด ํ๊ทธ ์์ผ๋ก ์ ์์ ์ธ ํ๋ฆ(normal flow)์ ๋ฐ๋ผ ์์น๊ฐ ์ง์ ๋๊ฒ ๋ฉ๋๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>static</title>
<style>
.box1{
position:static;
background-color: green;
color:white;
width: 100px;
height: 100px;
}
.box2{
position:static;
background-color: red;
color:white;
width: 100px;
height: 100px;
}
.box3{
position:static;
background-color: blue;
color:white;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
</body>
</html>
์ด๋ ๊ฒ static์ ์ฐจ๋ก๋๋ก ์๋ฆฌ๋จผํธ๋ฅผ ๋์ต๋๋ค!
๋จ์ด ์์ฒด์ ๋ป์ฒ๋ผ '์๋์ '์ธ ์์ฑ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๊ทธ๋ผ ๋์ฒด '๋ฌด์'์ ์๋์ ์ธ์ง ๊ถ๊ธํดํ์ค ํ
๋ฐ์. ๋ฐ๋ก static, ์ฆ ์๋ ์์ ์ด ์์ด์ผ ํ๋ ์์น์ ์๋์ ์
๋๋ค. relative๋ ์์ ์ด ์๋ ์๋ ์๋ฆฌ๋ฅผ ๊ธฐ์ตํ๋ ์น๊ตฌ์
๋๋ค. ๊ทธ๋์ position: relative;
๋ผ๋ ๊ฐ์ ์ฃผ๊ณ left : 50px;
์ด๋ผ๊ณ ์ถ๊ฐ์ ์ผ๋ก ์ ์ด ์ฃผ๋ฉด, ๋ณธ์ธ์ static ์๋ฆฌ์์ ์ผ์ชฝ์ผ๋ก 50px๋งํผ ๋จ์ด์ง ์๋ฆฌ์ ์์นํ๊ฒ ๋ฉ๋๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>relative</title>
<style>
.box1{
position: static;
background-color: green;
color: white;
width: 100px;
height: 100px;
}
.box2{
position:relative;
left: 40px;
background-color: red;
color:white;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="box1">box1</div>
<div class="box2">box2</div>
</body>
</html>
relative ์์ฑ์ ์ฃผ์ด box2๊ฐ ์๋ ์์ด์ผ ํ ์๋ฆฌ์์ ์ผ์ชฝ์ผ๋ก 40px ๋จ์ด๋จ๋ฆฐ ๋ชจ์ต์ ๋๋ค.
absolute์ ํน์ง์ ๊ตณ์ด ํ ๋จ์ด๋ก ์ค๋ช
ํ์๋ฉด 'my way'๋ผ๊ณ ํ ์ ์์ต๋๋ค. position: absolute;
๋ผ๊ณ ํ ๋ค top : 20px; right: 30px;
์ด๋ผ๊ณ ์ถ๊ฐ์ ๊ฐ์ ์ฃผ๋ฉด, ์ค๋ฅธ์ชฝ ์๋จ์ ๋๋จ์ด์ง ๋ฐ์ค๊ฐ ํ๋ ๋์ฌ์๋ ๊ฒ์ ๋ณด์ค ์ ์์ต๋๋ค.
๊ทธ๋ฌ๋, ์ด absolute๋ ๋์น๋ฅผ ๋ณด๋ ๋ ์์ด ์๋๋ฐ, ๋ฐ๋ก ๋ถ๋ชจ ์์์ ๋๋ค. absolute๋ static์ ์ ์ธํ position ์์ฑ๊ฐ์ ๊ฐ์ง ๊ฐ์ฅ ๊ฐ๊น์ด ๋ถ๋ชจ์ ๋ฐ์ค ๋ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์์นํ๊ฒ ๋ฉ๋๋ค. ๋ง์น relative๊ฐ static ์์ฑ๊ฐ์ด์์ ๋์ ์๋ฆฌ๋ฅผ ์ ๋ ํ๊ณ ์๋ ๊ฒ์ฒ๋ผ์.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>relative-absolute</title>
<style>
.box1{
position:relative;
top:40px;
background-color: green;
color:white;
width: 100px;
height: 100px;
}
.box2{
position:absolute;
top: 40px;
background-color: red;
color:white;
width: 100px;
height: 100px;
}
.box3{
position: absolute;
top: 30px;
left: 30px;
background-color: blue;
color:white;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="box3">box3</div>
<div class="box1">box1
<div class="box2">
box2
</div>
</div>
</body>
</html>
์๋จ์ ์ฝ๋๋ฅผ ์นํ์ด์ง์ ๊ตฌํํ ์ฌ์ง์
๋๋ค. ์ด์ฒ๋ผ relative ์์ฑ์ ๊ตฌ์ ๋ฐ์ง ์๋ box3์ left์ top์ ์ด๋ ์ ๋ px์ ๋์ด ๋จ์ดํธ๋ ธ์ ๋ ์๋จ ์ ์ผ ์ผ์ชฝ์ ๊ธฐ์ค์ ์ผ๋ก ์์ง์๋ค๋ฉด, relative ์์ฑ์ธ box1 ๋ด์ ๊ตฌ์๋ฐ๋ box2๋ top: 40px;
๊ฐ์ ์ฃผ์์ ๋, box1์ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ง์์์ ํ์ธํ์ค ์ ์์ต๋๋ค. ์ฆ box2์ 3์์ ํ์ธํ ์ ์๋ฏ ๋๊ฐ์ด position: absolute;
์ ์์ฑ์ ๊ฐ์ง๊ณ ์์ด๋, ์์ ์๋ฆฌ๋จผํธ๊ฐ position ์์ฑ๊ฐ์ ๊ฐ์ก๋์ง ์๋์ง์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ์์น ๊ฒฐ๊ณผ๊ฐ ๋ํ๋๋ค๋ ๊ฒ์
๋๋ค.
one:nth-child(2)
์ position: absolute;
๋ฅผ relative๋ก ๋ฐ๊ฟ๋ณด์ธ์.position: relative;
๋ฅผ ์ฃผ์์ฒ๋ฆฌ ํด๋ณด์ธ์.<!DOCTYPE html>
<html lang="ko">
<head>
<style>
.container {
position: relative;
width: 500px;
height: 500px;
margin-left: 100px;
border: 5px solid black;
}
.one {
width: 100px;
height: 100px;
border: 1px solid black;
color: white;
}
.one:nth-child(1) {
position: static;
background-color: red;
}
.one:nth-child(2) {
position: absolute;
top: 10px;
left: 10px;
background-color: green;
}
.one:nth-child(3) {
position: static;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="one">box1</div>
<div class="one">box2</div>
<div class="one">box3</div>
</div>
</body>
</html>
์ด๋ ๋ฏ ์ฌ๋ฌผ์ด ๋์ด๋ ๊ณต๊ฐ์ 2์ฐจ์์ด ์๋ 3์ฐจ์์ด๊ธฐ ๋๋ฌธ์
์คํฌ๋กค์ ์ฌ๋ฆฌ๊ฑฐ๋ ๋ด๋ฆด ๋, ํน์ ๋ฐ์ค๊ฐ ๊ณ ์ ๋์ด ์์ง์ด์ง ์์์ผ๋ฉด ํ๋ค๋ฉด, ์ด fixed ์์ฑ์ ์ด์ฉํ์๋ฉด ๋ฉ๋๋ค! position:fixed
๋ฅผ ๊ธฐ์
ํ๋ฉด, absolute
๊ฐ position
์์ฑ๊ฐ์ ๊ฐ์ง ๊ฐ์ฅ ๊ฐ๊น์ด ๋ถ๋ชจ๋ฅผ ๊ธฐ์ค์ผ๋ก ์์น๋ฅผ ์ ํ๋ ๊ฒ ์ฒ๋ผ, fixed๋ ํ์ฌ ์ฌ์ฉ์๊ฐ ๋ณด๊ณ ์๋ ๋ธ๋ผ์ฐ์ ํ๋ฉด(๋ทฐํฌํธ)๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ง์น ํ๋ฉด์ ๋ถ์ ๊ฒ์ฒ๋ผ ๊ทธ ์๋ฆฌ์ ๊ณ์ํด์ ์์นํ ๊ฒ์
๋๋ค. ์์ฆ ๋ง์ ์ฌ์ดํธ๋ค์ด ํ์ด์ง๊ฐ ์คํฌ๋กค ๋์ด๋ ์ค์ํ ์ ๋ณด(๋ค๋น๊ฒ์ด์
๊ฐ์)๋ฅผ ํ๋ฉด์ ๊ณ์ ๋
ธ์ถ ์์ผ์ฃผ๊ธฐ ์ํด ๋ง์ด ์ฌ์ฉํ๋ ์์ฑ์
๋๋ค.
์ ๊ฒ์ ๊ฒฐ๊ณผ ํ๋ฉด์์ ์คํฌ๋กค์ ๋ด๋ ค๋ ์๋จ์ ๊ฒ์ ์ฐฝ์ ์ฌ๋ผ์ง์ง ์๊ณ ๋ทฐํฌํธ ์๋จ์ ๊ณ ์ ๋์ด ์๋ ๋ชจ์ต์ ํ์ธ ํ ์ ์์ต๋๋ค. ํฌ๋กฌ ๊ฐ๋ฐ์ ๋๊ตฌ์์ header
ํด๋์ค์ ์ ์ฉ๋ position:fixed
์์ฑ์ ์ง์ ํ์ธํด ๋ณด์ธ์.
<!DOCTYPE html>
<html lang="ko">
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
}
.nav {
font-family: 'Pacifico', cursive;
position: fixed;
padding: 10px;
background-color: brown;
color: white;
font-size: 28px;
width: 100%;
height: 45px;
}
.container {
margin-top: 0;
padding-top: 120px;
margin-left: 20px;
}
.dark-mode {
font-family: 'Pacifico', cursive;
font-size: 23px;
position: fixed;
width: 200px;
height: 40px;
background-color: brown;
border-radius: 7px;
color: white;
bottom: 50px;
right: 50px;
text-align:center
}
</style>
</head>
<body>
<div class="nav">hello</div>
<div class="container">
(lorem+br)*100
</div>
<div class="dark-mode">dark</div>
</body>
</html>
sticky(๋๋ํ, ๋์ ๋์ ํ) ์์ฑ๊ฐ์ด ์ ์ฉ๋ ์์๋ ์กฐ์์ ์คํฌ๋กค์ด ์๋ค๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ๋ถ๋ชจ ์์์ ์ปจํ ์ธ ์์ญ์ ๋ฌ๋ผ๋ถ์ต๋๋ค.
์๋ ์์ ์์ ์คํฌ๋กค์ ๋ด๋ ค๋ณด์ธ์.
(body์์ฑ์๋ ์คํฌ๋กค ๊ธฐ๋ฅ์ด ์๋ํ์ง ์์ต๋๋ค)
<!DOCTYPE html>
<html lang="ko">
<head>
<style>
section {
height: 1000px;
border: 3px solid black;
}
h2 {
position: -webkit-sticky;
position: sticky;
top: 0px;
background: greenyellow;
margin: 0;
}
</style>
</head>
<body>
<h1>sticky test</h1>
(section>h2{์ค๋์ ๋ฉ๋ด$}+ul>(li>lorem)*3)*3
</body>
</html>
position - CSS: Cascading Style Sheets | MDN
position์ ํตํด ์์์ ์์น๋ฅผ ๋ณ๊ฒฝํ๋ค๋ณด๋ฉด ์์์ ์์๊ฐ ๊ฒน์ณ๋ณด์ด๋ ์ผ์ด ๋ฐ์ํฉ๋๋ค. ์ด๋ ์ด๋ค ์์๊ฐ ๋ ์๋ก ๋ํ๋๊ฒ ํ ์ง ๊ฒฐ์ ํ ๋ ์ฌ์ฉํ๋๊ฒ ๋ฐ๋ก z-index ์์ฑ์ ๋๋ค.
์ค์ง static์ ์ ์ธํ position ์์ฑ๊ฐ์ด ์ ์ฉ๋ ์์์ Z์ถ ์์๋ฅผ ๊ฒฐ์ ํ ์ ์์ผ๋ฉฐ, z-index ๊ฐ์ด ๋ ํฐ ์์๊ฐ ๊ฐ์ด ์์ ์์์ ์๋ฅผ ๋ฎ์ด๋ฒ๋ฆฌ๊ฒ ๋ฉ๋๋ค.
๋ํ ๋ถ๋ชจ๊ฐ z-index๋ฅผ ๋์ฌ ์์ ์์ผ๋ก ๋์ฌ ์ ์์ต๋๋ค. ์์์ z-index๋ฅผ ๋ฎ์ถฐ ๋ถ๋ชจ ๋ค๋ก ๊ฐ๋ ๊ฒ์ ๊ฐ๋ฅํฉ๋๋ค.
![](https://velog.velcdn.com/images/penameyo/post/48025c4c-ff0c-425c-b7be-ec05cfd86a39/image.png์๋ ๊ฒ z-index์ ์ ์๊ฐ์ด ๋ ํฐ ๊ฐ์ฒด๊ฐ ์์ผ๋ก ๋์จ๋ค