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

Many websites on the Internet use sidebar navigation, leading some people to believe that it is easy to create. However, in reality, it is surprisingly difficult to implement properly.

Creating sidebar navigation requires more work than simply making a responsive website, as there are many things to consider, such as fine-tuning the behavior of opening and closing the sidebar for each device and implementing swiping to open and close the sidebar.

Despite the troublesome nature of responsive websites, many famous CSS frameworks such as Bootstrap and Tailwind CSS provide sidebar navigation as a component. However, sidebar navigation is rarely seen in other frameworks.

I personally had difficulty building sidebar navigation, especially when implementing the swipe function. At the time, I tried to use the sidebar navigation provided by these major CSS frameworks, thinking that it would reduce the difficulty. However, while it was easy to provide the appearance of sidebar navigation, it created other problems.

The first problem was that these frameworks couldn't implement the swipe functionality that I struggled with the most.

The second problem, especially with Tailwind CSS, was that there were many classes that needed to be embedded in the HTML, making the entire HTML code difficult to read and understand. As a result, customizing the code was very time-consuming.

The third problem is that CSS frameworks combine pre-built components to create a whole, which means that any tweaking can cause component issues. Therefore, customizing it may not always result in a good outcome. Sometimes, it was more difficult to modify than to build and assemble only pure CSS and JS.

Considering all of this, it is clear that sidebar navigation is an important element in current web design trends. However, there is no definitive way to build it easily. Through trial and error, I found that the quickest way to create sidebar navigation is to create it steadily using CSS and JS code.

In the course of my trial and error, I have created a sidebar navigation that can be easily created without CSS frameworks. I would like to introduce how to create it. This sidebar navigation also supports opening and closing by swiping.

  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 design:

  • - 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.

This application will use this package to separate the code that needs 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.

I was going to stop using jquery this time, but I found it more convenient if I use in chapter 11, so I am 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.