BAC Software Consulting Blog

How To Create an Animated and Stateful Menu Toggle Using Only CSS

How To Create an Animated and Stateful Menu Toggle Using Only CSS

In today’s competitive web world, keeping your users engaged with your web solution can be a challenging job. Nowadays, people prefer viewing content that is not only unique but also interactive. So, if you want to communicate your message to clients in an interactive manner and seeking ways to attract good traffic to your site, then animation is an investment worth making.

Animations when used wisely can help in improving your users’ online experiences. Today, you can find several websites that make use of animated and toggle effects to make a design persuasive enough to drive traffic. Basically, many designers and developers today use animated elements with effects like slide, drop-down, etc. to make their design visually more appealing.

For example, mobile websites make use of navigation containing toggle effects wherein the hamburger icon (also known as 3 line menu navicon) is used to open up the hidden navigation items and “CLOSE” is used to hide those items again, as you can see in the following image:

Navigation with Open/Close toggle effects.

So now that you are convinced on adding animations in your design, there are two ways in which you can start experimenting with web animation. You can either write CSS for embedding animations in your design or use JavaScript for creating animations. For instance, most web developers use JavaScript for implementing toggle effects, however, working with JavaScript can be daunting for novice designers and developers.

In this post, I will show you an example of toggling the OPEN/CLOSE states of a menu using only CSS.

Example: Demonstrating the Process of Creating a Menu Toggle Switch With CSS Only

As users are becoming more accustomed to using mobile devices than desktop computers for browsing the web, having a website that is not optimized for mobile use can make you lose many potential customers. This is why mobile-optimized design have become an important part of almost any web development project.

When creating a mobile website design, focus on creating content for small screen displays. For instance, the main navigation menu must be short, of course, and you can’t display all the navigation items as you would on a desktop version of your website. You can hide your navigation menu items and add an option to enable users to view the hidden menu items when needed. One great way to do so requires creating an off-canvas menu.

Below are few steps which to create a standard off-canvas menu with a toggle (sliding) effect using only CSS:

Step1. Write HTML to lay down the foundation of your design.

First, you will have to write HTML markup to create a menu toggle switch for your off-canvas menu.

<!-- enter the start of your HTML markup here... -->
<ul class="nav-menu">
<li class="nav-item"><a href="#">Home</a></li>
<li class="nav-item"><a href="#">Company Profile</a></li>
<li class="nav-item"><a href="#">Product Offerings</a></li>
<li class="nav-item"><a href="#">Contact</a></li>
</ul>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger"></label>
<div class="site-wrap">
<!-- enter the rest of your HTML markup here. -->
</div>

The above HTML markup comprises of three main elements: the main navigation menu, a check-box along with a label, and your website content.

Step2. Use CSS to embed toggle effect in your navigation menu.

Once you completed writing the HTML markup, next you just need to add style to your navigation menu. Most importantly, you need to turn the menu into an off-canvas menu that when clicked triggers the toggle effect. To implement this, use the following code:

.nav-trigger:checked + label {
    left: 210px;
}
.nav-trigger:checked ~ .site-wrap {
    left: 200px;
    box-shadow: 0 0 5px 5px rgba(0,0,0,0.5);
}

The above code snippet has two declaration blocks:

  1. The First declaration block contains a CSS selector “.nav-trigger:checked + label”. This selector provides control over the trigger position of your menu. Furthermore, “left: 210px” determines the position of the toggle when the menu is open.
  2. The Second declaration block has a “site-wrap” element that ascertains that the content of your website will be covering the complete menu. In addition, a box-shadow element is defined for improving the look and feel of the menu.

Output:

The image below (Fig2.) is what the browser will render once you implemented Step1. and Step2.
View the TOGGLE MENU DEMO at the end of this post. Use the “View Page Source” in your browser to view the full source code.

Step3. Adding transition effect

You may want to change your menu trigger to a more advanced form of the element. Let’s suppose, you would like to change the menu trigger from traditional check-box to the widely-used hamburger icon. To do so, see the code given below:

.nav-trigger + label, .site-wrap {
     transition: left 0.2s;
}

Note: Remember that you will have to hide “overflow on the x-axis” in your <body> tag. Neglecting it won’t let your users scroll the window of your web page to left or right as soon as the menu is expanded. This can be achieved with the following code:

body {
    overflow-x: hidden;
}

Summing Up!

Hopefully that the example included in this post will give a good idea as to how you can add the Menu toggle effects using only CSS. I discussed how you can add animated and toggle states in the navigation menu. But keep on experimenting to find what other toggle effects you can create with the help of CSS.

High-Performance Web Designs for the Coolest Tech Devices

Creating advanced web designs for mobile was once an impossible dream. Right now, there’s a wealth of creative workarounds to test and try in order to render a native vibe to mobile web browsing. Fortunately, pioneering smartphones are now permitting web designers to unleash high-performance smartphone UI patterns that feature a native feel; they’re built and particularly designed for the web. These special patterns are smearing the line between the web and native apps. Here are a few amazing mobile web designs that work flawlessly on today’s newest smart devices.

New Calendars

In general, calendars that are web-based don’t work that well on mobile. That’s why most developers avoid them. Thankfully, there’s an alternative to annoying calendars: different pattern designs with straightforward lists of dates. By working their JavaScript magic, and through the use of creative carousels, web developers can now make incredibly versatile and usable calendars on mobile devices.

Off-Canvas Flyouts

These are special areas of a page that can’t be seen until users swipe or tap that area. These flyouts are primary mobile navigation patterns in web and native apps. They can always be found on a wealth of desktop web pages, too. However, the majority of today’s websites use flyouts that are off-canvas in order to hide static content, including static menus. Nonetheless, it is possible to be used for the display of additional content that is dynamic.

Pinch-to-Zoom Galleries

Text that is pinched-to-zoom is often seen as poorly formatted. However, in this case we’re talking about images, which are totally different. Similar to native images and maps found in apps, users are often interested in zooming into pics for more details. Considering that the feature demands quite a lot of processing power, only users who own a smart device with Retina iOS can benefit from it. It’s extremely important for overall performance to be treated as a design attribute, to ensure that challenging features are operational on gadgets that are powerful enough.

Colossal Image Carousels

On mobile, large image carousels are incredibly tough to implement. However, there are ways for web developers to create image browsing that feels native, and thus take advantage of exquisite DPI screens. There are certain challenges you may have to deal with: first, there’s a performance obstacle you must surpass because smart devices are not that strong at rendering a lot of objects one after the other. Second, there’s the navigation challenge. When you have a lot of images placed in a row, how do you move between them faster? In this scenario, the key is to create a grid view from a HTML carousel. This action is easily attained through changes in CSS and image source.

Alphabetical Lists that is Native-Inspired

Alphabetical indexes are excellent ways of helping users scroll through extensive lists of items. Apple offers a list meant to help users pick music and contacts on iOS, although bringing this functionality to web is rather challenging. The difference between using native apps and browsing through the web has always been crystal-clear to smartphone users. Nonetheless, as these gadgets are becoming increasingly more responsive, powerful and adaptive, the technology behind them is also increasing in level of sophistication.

Final Thought

Right now, increasingly more people use the web to benefit from the great capabilities of modern smart devices. Soon enough, it’s probable that we’ll see a bang in sophisticated, noteworthy and advanced web designs. That’s something we’re looking forward to experience; as advanced mobile designs work smoother on smartphones, it’s only natural to assume that one day we might use only our phone to surf the web.