DEV Community

Cover image for Create Side Navigation bar with HTML and CSS
Rohit Sharma
Rohit Sharma

Posted on

9 3

Create Side Navigation bar with HTML and CSS

Welcome back Guys!
Today I'm going to show you how easily you can create a side navigation bar with using simple HTML and basic CSS. So let's get started with HTML part.

Reference Video:


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sticky Side Navigation Bar</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="nav-bar">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Pricing</a></li>
            <li><a href="#">Contact us</a></li>
        </ul>
    </div>
    <div class="content">
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptate, quibusdam est tempora sint repellendus impedit expedita obcaecati sapiente, eligendi nobis praesentium quidem aliquam earum explicabo dicta veniam sunt reprehenderit nemo hic et itaque nulla a? Unde, id. Officiis suscipit excepturi impedit provident cumque, sunt necessitatibus ad nobis nesciunt commodi, labore voluptates repellat? Aliquam, suscipit et recusandae, atque delectus unde ab veniam quam commodi aut eum dolore dicta adipisci fugit quas animi obcaecati harum voluptatem? Ad voluptatum, rerum adipisci dolorum culpa molestiae error vero. Nemo laudantium necessitatibus praesentium dignissimos sint similique pariatur rerum vitae officia vero earum facere, molestiae cumque? Quisquam.
    </div>
    <script src="main.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

We added 4 random links in div tag class (nav-bar) and dummy text in another div tag class (content). And that's enough for HTML.
And now it's time to jump into the most important part i.e. CSS.

      *{
        box-sizing: border-box;
        }
        html,body{
            margin: 0;
            padding: 0;
        }
        .nav-bar{
            background: lightgray;
            width: 30%;
            height: 100%;
            position: fixed;
            top: 0;
            left: 0;
            overflow-y: auto;
        }
        .nav-bar a{
            text-decoration: none;
            color: red;
            display: block;
        }
        .nav-bar ul{
            list-style-type: none;
            margin: 0;
            padding: 0;
        }
        .nav-bar ul li a{
            padding: 10px;
            background: lightgray;

        }

        .nav-bar ul li a:hover{
            background: #173459;
        }
        .nav-bar li{
            border-bottom: 1px solid #333;
        }
        .nav-bar li:last-child{
            border: none;
        }
        .content{
            margin-left: 30%;
            width: 70%;
            padding: 2%;
        }
Enter fullscreen mode Exit fullscreen mode

Important Note:-
• In class "nav-bar" we set width of the nav-bar 30% so it covers only 30% of screen.
• We set overflow-Y to auto. So, if content amount increases then there will be a slide-bar.
• Now for content we set margin-left: 30% because in that 30% part we want to display the navigation bar.
• And set width: 70% so that remaining part is covered by the content.
That's it. We just created a sticky side Navigation Bar using basic HTML and CSS.

I Hope you loved ♥ it ☻

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️