๐ง CSS Position Property์ ๋ํด์ ์ ํํ๊ฒ ์ ๋ฆฌํด ๋ณด์.
์์ ํ๋ก ํธ์๋๋ฅผ ๋ง๋ค๊ธฐ ์ํด์๋ ๋ฐ์ค๋ฅผ ์ ์ฌ์ ์์ ๋๋ ๊ฒ์ ํ์๋ค!
CSS ๋ ์ด์์์ ์ง๋ค ๋ณด๋ฉด, ๋ ์ด์์์ด ๋ด๊ฐ ์ํ๋ ๋๋ก ์์น์ ์๋ ๊ฒ์ฒ๋ผ ๋ณด์์ผ๋, ํ๋ฉด ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ฉด ๋ง๊ฐ์ง๋ ๊ฒฝ์ฐ๋๋ฌธ์ (((๋ใ
)))
์ ํํ๊ฒ ์ ๋ฆฌํด๋ณด๋ ค ํ๋ค.
Position Property์๋ ๋ค ๊ฐ์ง๊ฐ ์๋ค.
- static
- fixed
- relative
- absolute
default ๊ฐ์ด๋ค. ์์น๊ฐ ์ง์ ๋ ๊ฒ์ด ์๋๋ผ๊ณ ํํํ๋ค.
.box1 {
position: static;
border: 1px solid black;
width: 200px;
height: 200px;
margin-left: 50px;
text-align: center;
padding: 80px 0;
box-sizing: border-box;
}
- ํ์ด์ง ์คํฌ๋กค ๋ด๋ ค๋ fix๋์ด ์๋ ๊ฒ !
- ๋ทฐํฌํธ(viewport)์ ์๋์ ์ผ๋ก ์์น๊ฐ ์ง์ ๋๋๋ฐ, ์ด๋ ํ์ด์ง๊ฐ ์คํฌ๋กค๋๋๋ผ๋ ๋ ๊ฐ์ ๊ณณ์ ์์นํ๋ค๋ ๋ป์ด๋ค.
- relative์ ๋ง์ฐฌ๊ฐ์ง๋ก top์ด๋ right, bottom, left ํ๋กํผํฐ๊ฐ ์ฌ์ฉ๋๋ค.
.box2 {
border: 3px solid skyblue;
width: 200px;
height: 200px;
margin-left: 50px;
text-align: center;
padding: 80px 0;
box-sizing: border-box;
position: fixed;
right: 0;
}
์คํฌ๋กค์ ๋ด๋ ค๋ box2-fixed๊ฐ ์ ์ง๋จ์ ์ ์ ์๋ค.
- ์ ์ด๊ฑฐ absolute๋ ๋๋ฌด ํท๊ฐ๋ฆฐ๋ค......
- relative๋ ๋ณ๋์ ํ๋กํผํฐ๋ฅผ ์ง์ ํ์ง ์๋ ์ด์ static๊ณผ ๋์ผํ๊ฒ ๋์ํ๋ค.
- ์๋ ์์น๊ฐ ์ง์ ๋ ์๋ฆฌ๋จผํธ์ top์ด๋ right, bottom, left๋ฅผ ์ง์ ํ๋ฉด ๊ธฐ๋ณธ ์์น์ ๋ค๋ฅด๊ฒ ์์น๊ฐ ์กฐ์ ๋๋ค.
.box3 {
position: relative;
border: 1px solid black;
width: 200px;
height: 200px;
margin-left: 50px;
text-align: center;
padding: 80px 0;
box-sizing: border-box;
}
.box3-1 {
border: 2px solid black;
width: 300px;
height: 100px;
/* margin-left: 50px; */
text-align: center;
padding: 40px 0;
box-sizing: border-box;
position: relative;
top: -20px;
/* top์ -๋ก ์ค์ ํด์ฃผ๋ฉด ์๋ก ๊ฐ๋ค ! */
left: 100px;
}
- ๊ฐ์ฅ ๊ฐ๊น์ด ๋ถ๋ชจ ํ๊ทธ์ position์ด relative์ผ ๋, ๋ถ๋ชจ ํ๊ทธ๋ฅผ ๊ธฐ์ค์ผ๋ก absolute๊ฐ ์์ง์ธ๋ค.
- ์กฐ์ element๊ฐ ์์ผ๋ฉด body document๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ง์ธ๋ค.
.absolute {
position: relative;
border: 1px solid grey;
width: 500px;
height: 500px;
left: 50px;
}
.box4 {
border: 2px solid red;
width: 200px;
height: 200px;
margin-left: 50px;
text-align: center;
padding: 80px 0;
box-sizing: border-box;
position: absolute;
top: 100px;
left: 200px;
}