rx-36

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

Why Build from Scratch?

Sidebar navigation is a staple of modern web design, but implementing it properly—especially with responsive behavior and swipe gestures—is surprisingly difficult.

While popular frameworks like Bootstrap and Tailwind CSS offer sidebar components, they often come with trade-offs:

  1. Limited Functionality: Many lack built-in swipe support for mobile devices.
  2. Code Bloat: Frameworks like Tailwind can clutter HTML with numerous classes, making maintenance difficult.
  3. Rigidity: Customizing pre-built components often breaks their internal logic, making them harder to modify than building from scratch.

Through trial and error, I found that the most efficient way to create a flexible, swipe-enabled sidebar is to build it from the ground up using CSS Grid and JS. In this article, I'll show you how to implement a robust sidebar navigation system without relying on heavy CSS frameworks.

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.

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.

JS library

The JS libraries used for this project are Alpine.js and jQuery.

I like Alpine.js because it is considered an alternative to jQuery and can achieve various operations with minimal code added inside HTML tags.

I initially considered using Vanilla JS instead of jQuery, but I realized in Chapter 10 that implementation would be difficult without jQuery, so I decided to use jQuery in addition to Alpine.js. Also, there is a chapter at the end of this tutorial that implements swipe functionality on mobile devices, where we will use a library called Hammer.js.

Project File Structure

Development Process

Create this website in the following steps.

  1. Basic Django setup
  2. Creating settings.py
  3. Writing the Code Foundation, including base.html
  4. Foundational CSS Code for Building Grid Layouts
  5. Implementation of OPEN/CLOSE button
  6. Separating the HTML and CSS code to load by the User-Agent
  7. Adding CSS Based on Device
  8. Open/Close the Sidebar Menu Automatically on PC at a Certain Width
  9. Swipe to Open/Close
  10. When a Link in the sidebar is clicked, the sidebar should close before going to the next page

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.

2. Creating settings.py

3. Writing the Code Foundation, including base.html

4. Foundational CSS Code for Building Grid Layouts

5. Implementation of OPEN / CLOSE button

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

As we mentioned earlier, this application 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 in the official documentation, 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 fine without a cache. That's why I currently don't use one.

7. Loading a Different CSS File for Each Device

8. Adding CSS Based on Device

9. Open/Close the Sidebar menu Automatically on PC at a certain width

10. Swipe to Open/Close

11. Sidebar Open/Close Issue During Page Transitions

That's it! 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.

The GitHub repository for this code is available in the grid_layout_new project.