absolute와 relative는 CSS의 position 속성에서 중요한 관계를 형성한다. 이 둘의 관계를 이해하면 레이아웃을 더 효율적으로 구성할 수 있다.
position: relative: 요소가 문서 흐름에 따라 배치되지만, 그 요소의 원래 위치를 기준으로 이동할 수 있다. 이 요소는 다른 요소의 기준점이 된다.position: absolute: 요소가 문서 흐름에서 벗어나 가장 가까운 position 이 설정된 조상 요소를 기준으로 위치한다. 조상이 없으면 <html> 문서를 기준으로 삼는다.relative 요소: relative로 설정된 요소는 그 위치가 기준이 되므로, 그 안에 있는 absolute 요소는 이 요소를 기준으로 하여 위치를 조정한다.absolute 요소: absolute로 설정된 요소는 relative 요소의 top, right, bottom, left 속성에 따라 위치가 결정된다<div class="relative-box">
<div class="absolute-box"></div>
</div>
.relative-box {
position: relative;
width: 300px;
height: 300px;
background-color: lightblue;
}
.absolute-box {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
background-color: coral;
}

설명:
<div class="relative-box">
<div class="inner-relative-box">
<div class="absolute-box"></div>
</div>
</div>
.relative-box {
position: relative;
width: 300px;
height: 300px;
background-color: lightblue;
}
.inner-relative-box {
position: relative;
width: 150px;
height: 150px;
background-color: lightgreen;
}
.absolute-box {
position: absolute;
top: 20px;
left: 20px;
width: 100px;
height: 100px;
background-color: coral;
}

설명:
만약, relative 요소가 없으면 absolute 요소는 가장 가까운 position이 설정된 조상을 찾고, 없으면 body를 기준으로 삼는다.
<div class="absolute-box"></div>
.absolute-box {
position: absolute;
top: 20px;
left: 20px;
width: 100px;
height: 100px;
background-color: coral;
}

설명: 이 경우 absolute-box는 body를 기준으로 하여 위치하게 된다.