How to add a div that stays at the top while a web page scrolls?

Sometimes, we want to add a div that stays at the top while a web page scrolls

In this article, we’ll look at how to add a div that stays at the top while a web page scrolls

How to add a div that stays at the top while a web page scrolls?

To add a div that stays at the top while a web page scrolls, we set the position CSS property of the div to fixed.

For instance, we write:

<div>
  fixed
</div>

to add a div.

Then we make it fixed by writing:

div {
  top: 0;
  position: fixed
}

We set the top position of the div and the position to fixed to make the div stay at the top of the screen.

Conclusion

To add a div that stays at the top while a web page scrolls, we set the position CSS property of the div to fixed.