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>
</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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Gaurav Arora
Gaurav is an experienced UI developer with experience and capabilities to build compelling UI's for Web and Mobile Applications.