페이지를 구성할 때, 각 섹션이 한 화면을 꽉 채우고 스크롤할 때 부드럽게 넘어가는 구조를 만들고 싶을 때가 있다.
이럴 때 유용한 CSS 속성이 바로 scroll-snap이다.
[ HTML ]
<main>
<section>Section 1</section>
<section>Section 2</section>
<section>Section 3</section>
<section>Section 4</section>
</main>
[ CSS ]
html,
body {
scroll-behavior: smooth;
height: 100%;
}
main {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory; // 세로 방향 스냅
}
section {
height: 100vh;
scroll-snap-align: start; // 섹션 시작 지점에 맞춰 스냅
}
scroll-snap-type: y mandatory;
scroll-snap-align: start;
scroll-behavior: smooth;
✅ 헤더가 고정되어 있다면, 높이만큼 스냅 기준 조정
section {
scroll-margin-top: 80px; // 헤더 높이만큼 여백 확보
}
✅ 모바일 대응
모바일 브라우저 주소창 문제로 100vh 대신 100dvh를 쓰면 더 정확하다.
section {
height: 100dvh;
}