How to design a modern sidebar menu using HTML and CSS?


When you think about the layout of a typical website, it is likely that you include a column of important links (navigation links for various sections in the webpage) to the right or left of the main content area.

This component is known as ‘sidebar’ and is commonly used as menu across the web. Although it is commonly used, the developers generally add this element to the website for navigating from one page to other, even to different sections of a webpage.

Let’s understand this feature and try to create a modern sidebar by only using HTML and CSS.

What is a Sidebar Menu?

A sidebar is a static column present right or left to the main content area. This component contains navigation links, widgets or other necessary links (for home page, contents or other sections) in a website.

Given below is an example which demonstrates how to create a simple sidebar menu. This menu is placed at the left side of the main content area (as followed in most of the websites).

Example

In this example, we have divided the webpage into two sections using CSS grids. 15% of the webpage constitutes the sidebar menu while 85% of it makes the main content.

CSS Grids

It enables the developers to convert any element into grid container by setting display: grid. To add column, we use,

grid-template-columns: value value;

value represents the width of the column. It can be written in length (px, cm, em) or in percent.

<a> tag (Anchor Elements)

It is used to link external pages inside a webpage. It can also be used to link internal sections within the document. An id attribute uniquely defines the element.

<a href= "#"> </a>

The href attribute contains the url of the external pages or id of the internal sections within the document.

<!DOCTYPE html>
<html>
<head>
   <title> Sidebar menu </title>
   <style>
      #main-doc {
         display: grid;
         grid-template-columns: 15% 85%;
         grid-template-rows: auto;
         grid-template-areas: "advert content";
      }

      .item1 {
         padding: 10px;
      }

      #head {
         font-family: serif !important;
         color: #8b0000 !important;
         font-weight: 900;
         margin: 5px;
         padding: 0 5px 5px;
      }

      .main-section {
         font-family: Brush Script MT;
         font-size: 20px;
         color: #000080;
      }

      .item2 {
         background: linear-gradient(-35deg, #fff000, #ffb6c1, #afeeee);
         padding: 6px 8px 6px 16px;
         margin: 0
      }

      .contents {
         font-size: 26px !important;
         color: grey;
      }

      .item1 a {
         border-radius: 5px;
         padding: 6px 16px 6px 16px;
         display: block;
      }
      a:hover {
         color: red;
         transform: scale(1.1);
      }
   </style>
</head>
<body>
   <main id="main-doc">
   <div class="item1">
      <nav id="navbar">
         <header class="contents">
            <strong> Contents </strong>
         </header>
         <br>
         <a href="#background" class="nav-link"> Background </a>
         <br>
         <hr>
         <a href="#romance" class="nav-link"> Romance </a>
         <br>
         <hr>
         <a href="#relations" class="nav-link"> Relations </a>
         <br>
         <hr>
         <a href="#voice_actors" class="nav-link"> Voice Actors </a>
         <br>
         <hr>
         <a href="#costumes" class="nav-link"> Costumes </a>
         <br>
         <hr>
         <a href="#gallery" class="nav-link"> Gallery </a>
         <br>
         <hr>
      </nav>
   </div>
   <div class="item2">
   <header id="head">
      <h1> Animation Character </h1>
   </header>
   <section class="main-section" id="background">
      <header>
         <h1> Background </h1>
      </header>
      <hr>
      <p> This is placeholder text. This paragraph contains information about the background of the character. </p>
   </section>
   <section class="main-section" id="romance">
      <header>
         <h1> Romance <h1>
         <hr>
      </header>
      <p> This paragraph contains text related to the life of the character. </p>
   </section>
   <section class="main-section" id="relations">
      <header>
      <h1> Relations </h1>
      </header>
      <hr>
      <ul>
         <li> Mother <br>
         <p> Text about character's mother </p>
         <li> Father <br>
         <p> Information about the father. </p>
         <li> Sister <br>
         <p> Text about character's sister </p>
         <li> Friend <br>
         <p> Text about friend </p>
      </ul>
   </section>
   <section class="main-section" id="voice_actors">
      <header>
         <h1> Voice actors
            <hr>
         </h1>
      </header>
      <p> This contains information about voice actors in the animation </p>
   </section>
   <section class="main-section" id="costumes">
      <header>
         <h1> Costumes
            <hr>
         </h1>
      </header>
      <br>
      <br>
   </section>
</body>
</html>

Example

Here we will create a toggle sidebar. In this we have created a sidebar and positioned it to the left of the content area. We have a button in the content area which on clicking enables us to collapse the sidebar created.

We have used the CSS transition property to change the position of the sidebar smoothly. On clicking the button, the position of the sidebar changes from 0 to -160px (which is equal to the width of the sidebar). In other words, the sidebar moves towards left by a distance of its width.

<!DOCTYPE html>
<html>
<head>
   <title> Toggle Sidebar </title>
   <style>
      body {
         margin: 0;
      }
      .container {
         display: flex;
         min-height: 90px;
      }
      .sidebar {
         position: relative;
         left: 0;
         margin-right: 20px;
         width: 160px;
         background-color: #ccc;
         transition: all 0.20s;
      }

      .sidebar.collapsed {
         left: -160px;
         margin-right: -150px;
      }
   </style>
</head>
<body>
   <div class="container">
      <div class="sidebar" id="sidebar">
         <strong> Sidebar menu </strong>
         <ul>
            <a href="#" class="nav-link">
               <li> Link 1 </li>
            </a>
            <a href="#" class="nav-link">
               <li> Link 2 </li>
            </a>
            <a href="#" class="nav-link">
               <li> Link 3 </li>
            </a>
            <a href="#" class="nav-link">
               <li> Link 4 </li>
            </a>
         </ul>
      </div>
      <div class="content">
         <h2> This is an example. This contains the main content area. </h2>
         <br> Click the button below to toggle the sidebar <br>
         <br>
         <button onclick="document.getElementsByClassName('sidebar')[0].classList.toggle('collapsed')"> toggle Sidebar </button>
      </div>
   </div>
</body>
</html>

Conclusion

In this article, we have discussed two types of sidebar menu in a webpage. One of them is a basic sidebar while the other is toggling sidebar. Both of them is designed by using only HTML and CSS.

Updated on: 28-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements