Open In App

How to Create Responsive Sidebar using HTML & CSS ?

We will create a responsive sidebar which is created using HTML and CSS. The responsive sidebar will be interactive for small devices also.

a

Preview

Approach

Example: The example shows the implementation of the above-explained approach.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <!--link the css file using link tag-->
    <link rel="stylesheet"
          href="style.css">
</head>

<body>
    <div class="container">
        <!--container class will contain the links-->
        <a href="#" class="active">Home</a>
        <a href="#">Tutorials</a>
        <a href="#">Competations</a>
        <a href="#">Achievements</a>
    </div>
    <div class="content">
        <!--write the content you want inside.-->
        <h1>Responsive Sidebar</h1>
        <p>write the content here</p>
    </div>
</body>

</html>
* {
    border: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: "Lato", sans-serif;
}

body {
    background: linear-gradient(to left, rgb(238, 91, 91), rgb(8, 8, 44));
}

.container {
    margin: 0;
    padding: 0;
    height: 100vh;
    width: 250px;
    position: fixed;
    background-color: #000011;

    overflow: auto;
}

/*adding styles to links */
.container a {
    text-decoration: none;
    display: block;
    padding: 18px;
    color: #fff
}

.container a.active {
    background-color: #14a093;
}

.container>a:hover:not(.active) {
    background-color: #403b3b;
}

div.content {
    margin-left: 230px;
    padding: 40px 40px;
    height: 100vh;
}

@media screen and (max-width:500px) {
    .container {
        width: 100%;
        height: auto;
        position: relative;
    }

    .container a {
        float: left;
    }

    div.content {
        margin-left: 0;
        background-color: rgb(242, 242, 110);
    }
}

@media screen and (max-width:300px) {
    .container a {
        text-align: center;
        float: none;
    }
}

Output:

sidebar

Responsive Sidebar

Article Tags :