CSS scroll-behavior Property



The scroll-behavior property is used to set the behaviour of scroll. We can set the following different property values for the scroll-behavior property −

scroll-behavior: auto|smooth|initial|inherit;

Example

Let us now see an example to implement the sroll-behavior property −

 Live Demo

<!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>Demo Heading</h1>
<div class="main" id="area1">
<h2>Section 1</h2>
<a href="#area2">Reach Section 2</a>
</div>
<div class="main" id="area2">
<h2>Section 2</h2>
<a href="#area3">Reach Section 3</a>
</div>
<div class="main" id="area3">
<h2>Section 3</h2>
<a href="#area1">Reach Section 1</a>
</div>
</body>
</html>

Output

Now, click “Reach Section 2” −

Now, click “Reach Section 3” for smooth scroll to Section 3 −


Advertisements