1. Position์ด๋?
์ด๋ฆ์์์ฒ๋ผ, position์ด๋ ์นํ์ด์ง์์ ์ ํฌ๊ฐ ๋ง๋ค์๋ html ํ๊ทธ๋ id, class ๋ฐ์ค ๋ฑ์ ์์น๋ฅผ ์ง์ ํด์ฃผ๋ ์์ฑ์
๋๋ค. position ์์ฑ์ ์ด์ฉํด ์ฐ๋ฆฌ๋ ํ์ด์ง์ ๋ ์ด์์์ ๊ฒฐ์ ํ ์ ์์ต๋๋ค.
2. Position์ ์ข ๋ฅ
2.1 static
๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ํ๊ทธ๋ค์ ๋ฐ๋ก position ์์ฑ๊ฐ์ ์ฃผ์ง ์์๋ค๋ฉด static ๊ฐ์ ๊ฐ์ง๋๋ค. ์ฆ html์ ์ด ํ๊ทธ ์์ผ๋ก ์์น๊ฐ ์ง์ ๋๊ฒ ๋ฉ๋๋ค. ๊ทธ๋์ ๊ตณ์ด ๊ธฐ์
ํ ํ์๋ ์์ง๋ง, ๋ถ๋ชจ ๊ฐ์ฒด์์ ๋ค๋ฅธ position ์์ฑ๊ฐ์ด ์ฃผ์ด์ก์ ๋, ๊ทธ๋ฅผ ๋ฌด์ํ๊ธฐ ์ํด ์ฌ์ฉ๋ ๋๊ฐ ์์ต๋๋ค.
<!DOCTYPE html> <html lang="kor"> <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>
ย

ย
2.2 relative
๋จ์ด ์์ฒด์ ๋ป์ฒ๋ผ '์๋์ '์ธ ์์ฑ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๊ทธ๋ผ ๋์ฒด '๋ฌด์'์ ์๋์ ์ธ์ง ๊ถ๊ธํดํ์ค ํ
๋ฐ์. ๋ฐ๋ก static, ์ฆ ์๋ ์์ ์ด ์์ด์ผ ํ๋ ์์น์ ์๋์ ์
๋๋ค. relative๋ ๊ทธ ๋๊ตฌ๋ณด๋ค๋ ์์ ์ด ์๋ ์๋ ์๋ฆฌ๋ฅผ ๊ธฐ์ตํ๋ ์น๊ตฌ์
๋๋ค. ๊ทธ๋์
position: relative;
๋ผ๋ ๊ฐ์ ์ฃผ๊ณ left : 50px; ์ด๋ผ๊ณ ์ถ๊ฐ์ ์ผ๋ก ์ ์ด ์ฃผ๋ฉด, ๋ณธ์ธ์ static ์๋ฆฌ์์ ์ผ์ชฝ์ผ๋ก 50px๋งํผ ๋จ์ด์ง ์๋ฆฌ์ ์์นํ๊ฒ ๋ฉ๋๋ค.<!DOCTYPE html> <html lang="kor"> <head> <meta charset="UTF-8"> <title>relative</title> <style media="screen"> .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>
ย

2.3 absolute
absolute์ ํน์ง์ ๊ตณ์ด ํ ๋จ์ด๋ก ์ค๋ช
ํ์๋ฉด 'my way'๋ผ๊ณ ํ ์ ์์ต๋๋ค.
position: absolute;
๋ผ๊ณ ํ ๋ค top : 20px; right: 30px;
์ด๋ผ๊ณ ์ถ๊ฐ์ ๊ฐ์ ์ฃผ๋ฉด, ์ค๋ฅธ์ชฝ ์๋จ์ ๋๋จ์ด์ง ๋ฐ์ค๊ฐ ํ๋ ๋์ฌ์๋ ๊ฒ์ ๋ณด์ค ์ ์์ต๋๋ค. ์ด๋ค์ ๊ธฐ์ค์ ์ด html ์์น์ ์๊ธฐ์, ์ผ์ชฝ ์ ์ผ ์๋จ์ด ๋ณธ๋ ์์ ์ ์์น๋ผ๊ณ ์๊ฐํ๊ณ ์์ง์ด๊ฒ ๋ฉ๋๋ค.๊ทธ๋ฌ๋, ์ด absolute๋ ๋์น๋ฅผ ๋ณด๋ ์์ฑ์ด ์๋ค๋ฉด, ๋ฐ๋ก relative์
๋๋ค. relative ์์ฑ์ด ๋ถ๋ชจ๋ก ๋์ฌ์๋ค๋ฉด, absolute๋ relative ๋ฐ์ค ๋ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์์นํ๊ฒ ๋ฉ๋๋ค. ๋ง์น 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; ์ ์์ฑ์ ๊ฐ์ง๊ณ ์์ด๋, ์์ ์๋ฆฌ๋จผํธ๊ฐ relative ์์ฑ์ ๊ฐ์ก๋์ง ์๋์ง์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ์์น ๊ฒฐ๊ณผ๊ฐ ๋ํ๋๋ค๋ ๊ฒ์
๋๋ค.
2.4 fixed
์คํฌ๋กค์ ์ฌ๋ฆฌ๊ฑฐ๋ ๋ด๋ฆด ๋, ํน์ ๋ฐ์ค๊ฐ ๊ณ ์ ๋์ด ์์ง์ด์ง ์์์ผ๋ฉด ํ๋ค๋ฉด, ์ด fixed ์์ฑ์ ์ด์ฉํ์๋ฉด ๋ฉ๋๋ค! position:fixed;๋ฅผ ๊ธฐ์
ํ๋ฉด, ํด๋น ์๋ฆฌ๋จผํธ๋ ํ๋ฉด์ ๋ถ์ ๊ฒ์ฒ๋ผ ๊ทธ ์๋ฆฌ์ ๊ณ์ํด์ ์์นํ ๊ฒ์
๋๋ค.
ย