Making sidebar navigation with only the use of CSS grid layout in the template of Django

Sidebar navigation is a menu display method used on many websites, giving an impression that its implementation might be straightforward. However, creating sidebar navigation is more complex than it might seem at first.

One of the main difficulties arises when designing a sidebar with responsive behavior using a single code template. The complexity increases significantly when implementing the open/close functionality for different devices and adding swipe gestures to enhance the user experience.

To avoid this complexity, frameworks like Tailwind CSS offer sidebar navigation components to save you the trouble of building them from scratch. However, customizing these components can sometimes be even more challenging than creating them from scratch, especially when trying to meet specific design needs.

Considering these challenges, building a customizable and flexible sidebar navigation for your site is often best achieved by creating it yourself using CSS Grid layout. This approach allows for precise control and flexibility, resulting in a more maintainable solution.

In this blog post, we'll explain how to create your own sidebar navigation using CSS Grid as the main technique, including features like swipe-to-open functionality, without relying on CSS frameworks.

  1. Visualizing the Django Application

Images 1-3 depict PC designs, images 4-7 show mobile designs, and images 8-11 represent tablet designs.

For both PC and tablet designs, the sidebar is initially open. However, in the case of mobile designs, the sidebar is closed by default.

Each design starts in a specific state: image 1 for PC, image 4 for mobile, and image 8 for tablet.

2. Required Python Packages

The following packages will be used to implement this sidebar navigation:

  • - django==4.05
  • - django-environ==0.8.1
  • - django-user-agents==0.4.0

django-user-agents is an important package in the list above. It enables us to separate files and code to be loaded for each device.

3. JS library

The JS library uses alpine.js and jQuery for this project.

I like alpine.js because it is considered an alternative to jQuery and can achieve various operations with a minimum code added to the HTML tag inside.

Initially, I planned to stop using jQuery for this project, but I found it more convenient to use in Chapter 11 due to its simplicity in handling specific interactions. Therefore, I decided to keep using it. Also, there is a chapter at the end of this tutorial that implements the swipe functionality on mobile devices, which will use a module called hammer.js.

4. Project File Structure

5. Development Process

Create this website in the following steps.

5-1. Basic Django setup

To proceed with this tutorial, you will need a basic Django setup. However, as there are many articles and videos available online, we will not go into detail here.

If you prefer to use Docker for the setup, you can find instructions in this article on our blog.

Once you have the setup ready, please proceed to display "Hello world!" in your own browser.

5-2. Creating settings.py

5-3. Writing the Code Foundation, including base.html

5-4. Foundational CSS Code for Building Grid Layouts

5-5. Implementation of OPEN / CLOSE button

5-6. Separating the HTML and CSS code to load by the User-Agent

As we mentioned earlier, this blog has a slightly different design for each device, which makes it difficult to control the design with a single template. To solve this issue, apply different CSS and templates for each device depending on the User-Agent.

To detect the User-Agent, use a package called django-user-agents. You can see how to use django-user-agents on this page, so we won't describe it here. One thing to note is that the official django-user-agents documentation recommends using a cache. However, using a cache can often result in difficult-to-handle errors, and it works comfortably without using a cache. That's why I currently don't use a cache.

5-7. Loading a Different CSS File for Each Device

5-8 Adding CSS Based on Device

5-9. Open/Close the Sidebar menu automatically on PC at a certain width

5-10. Swipe to Open/Close

5-11. When a Link in the sidebar is clicked, the sidebar should close before going to the next page

All done! I hope you were able to follow along with the video at the beginning of this article.

As previously mentioned, one of the benefits of creating a site using only a grid layout is that the code is concise and easy to customize. I would be delighted if you were able to use this code to create a site with sidebar navigation in your design.

You can find the GitHub repository for this code here.