Open In App

How to make a div vertically scrollable without content using CSS ?

In this article, we will see how we can create a vertical scrollable section using CSS. HTML code is used to create the basic structure of the sections and CSS code is used to set the style,

HTML Code:



CSS Code:

Example: In this example, we create a vertical scrollable using the above approach.






<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        body {
            margin: 0;
        }
        /* Adding scroll snap type mandatory
        and scroll direction to y axis*/
        .container {
            scroll-snap-type: y mandatory;
            overflow-y: scroll;
            /* 100% height of the viewport */
            height: 100vh;
        }
        /* Adding general css to box and
        aligning snap to start */
        .box {
            height: 100vh;
            color: #fff;
            text-align: center;
            line-height: 100vh;
            font-size: 5rem;
            scroll-snap-align: start;
        }
        /* Setting different colors to all boxes */
        .box:nth-child(1) {
            background: #5e38ff;
        }
        .box:nth-child(2) {
            background: #fe802b;
        }
        .box:nth-child(3) {
            background: #00bf71;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

Output:


Article Tags :