Thuộc tính scroll-behavior được dùng để thiết lập hành vi cuộn của trang. Các giá trị có thể thiết lập:
scroll-behavior: auto | smooth | initial | inherit;
Ví dụ: Cuộn mượt mà giữa các section
<!DOCTYPE html>
<html>
<head>
<style>
html {
scroll-behavior: smooth;
}
#area1 {
height: 400px;
background-color: red;
}
#area2 {
height: 600px;
background-color: orange;
}
#area3 {
height: 550px;
background-color: magenta;
}
</style>
</head>
<body>
<h1>Tiêu đề Demo</h1>
<div class="main" id="area1">
<h2>Section 1</h2>
<a href="#area2">Đến Section 2</a>
</div>
<div class="main" id="area2">
<h2>Section 2</h2>
<a href="#area3">Đến Section 3</a>
</div>
<div class="main" id="area3">
<h2>Section 3</h2>
<a href="#area1">Đến Section 1</a>
</div>
</body>
</html>
Kết quả

Khi click "Đến Section 2" - trang cuộn mượt mà đến section 2:

Khi click "Đến Section 3" - cuộn mượt mà đến section 3:

Giải thích:
- auto: Cuộn ngay lập tức (mặc định)
- smooth: Cuộn mượt mà với hiệu ứng animation
- Áp dụng cho phần tử gốc (thường là
html) để ảnh hưởng toàn trang