Smooth Scrolling Using CSS Scroll Behavior

Posted By : Gaurav Arora | 24-Dec-2017

Hi Guys, as we all are aware of the CSS Scroll-Behavior property like overflow and all. And we never define this in our CSS as it works in its default state which is auto. But if we have to make single page applications and that too with parallax effects. In that case, we can use CSS scroll-behavior smooth option. What this will do, whenever we click on any link like we do in parallax application then the page will scroll smoothly to that particular section.


So, we will start by creating the structure

<html>
Link 1 Text
Link 2 Text
Link 3 Text
Link 4 Text
Link 5 Text
</html>

Now we will write some css for the same.

.container{
  width: 100%;
  float: left;
  font-family: Arial;
}
.container ul li {
  list-style: none;
  float: left;
}
.container .content{
  width: 100%;
  float: left;
  max-height: 500px;
  overflow-y: scroll;
}

.container .content .content-box{
  padding: 200px; 
  text-align: center;
}                
    

When we click on the link the page will scroll to that particular section, but in its default way. Now we will define the css scroll-behavior property to our css.

.container .content{
  width: 100%;
  float: left;
  max-height: 500px;
  overflow-y: scroll;
  scroll-behavior: smooth;
}                
    

Now, when we click on the link the page will smoothly scroll to that particular section. This approach is very useful in creating parallax pages. Normally we use jquery for this kind of smooth scrolling. Then, why to use jquery when we can achieve the same result using CSS only.
 
In this way, we can make smooth scrolling pages using CSS scroll behavior.

About Author

Author Image
Gaurav Arora

Gaurav is an experienced UI developer with experience and capabilities to build compelling UI's for Web and Mobile Applications.

Request for Proposal

Name is required

Comment is required

Sending message..