position은 요소를 배치하는 속성이다. position의 여러 속성값에 대해서 살펴보자.
.box1,
.box2{
width: 100px;
height: 100px;
}
.box1{
background-color: pink;
}
.box2{
background-color: aqua;
position: static;
}
.box2{
background-color: aqua;
position: relative;
top: -50px;
left: 50px;
}
.parents{
position:relative;
}
.absolute{
position:absolute;
top: 50px;
left: 50px;
}
위의 예제의 경우 parents라는 부모를 기준으로 하기 때문에 top에 음수값을 주지 않아도 두 개의 박스가 겹치게 된다.
.fixed{
position: fixed;
bottom: 5px;
right: 5px;
}
.sticky{
position: sticky;
top: 0; /*원하는 y 좌표*/
}
위의 예제의 경우 부모 요소의 top : 0인 지점에 sticky를 준 요소가 고정된다. 따라서 스크롤 하는 동안에 해당 위치에 고정된다.
absolute와 sticky를 쓸 때는 가장 가까운 반드시 부모나 조상 요소에 static을 제외한 속성값을 지정해야함을 잊지 말자!