Increasingly Data Explorers and Practitioners are using websites and web applications to communicate their insights and findings.
TailwindCSS is a utility-first Cascading Style Sheet (CSS) framework that allows you to build responsive web pages quickly. In this guide, we will build a responsive menubar with dynamic icons and no Javascript using exclusively TailwindCSS.
This guide has demonstrated how to build a responsive menu with dynamic icons and no Javascript using exclusively TailwindCSS. The key concepts demonstrated were using a checkbox
and the peer-*
classes to trigger and record the state of the menu. The for
attribute of the label
tag was used to provide an alternative target to activate the checkbox. When combined with the group-*
classes, this provided a dynamic menu system where the icons changed based on the menu state.
It should be noted that the episode is based on the Responsive Menus with TailwindCSS blog post. If you would like help with any of the topics discussed or the methodology use then please contact me through LinkedIn.
This post contains the show notes for the following Curious Data Explorer YouTube video:
Show Notes
The following terms, services, concepts and libraries were mentioned in the episode:
- HTML
- Hyper Text Markup Language - the semantic structure of a website.
- CSS
- Cascading Style Sheets - the styling of a website.
- CSS Frameworks
- A pre-prepared library that is meant to be used as a foundation for a website.
Welcome
Welcome to the Curious Data Explorer channel. My name is Ashraf and I’m an experienced Data Practitioner.
The focus of this video is responsive menus for web pages and applications using Tailwind CSS utility classes. We’ll build a menu that is suitable for both desktop and mobile use including dynamically changing the menu symbol from the classic “hamburger” to a cross all without JavaScript. Furthermore we’ll illustrate the key supporting principles and the right behaviors required for validation.
Introduction
The scope is to build a responsive menu system for a web application or site.
Primarily, we’ll be using the flexbox concept from Cascading Style Sheets (CSS) accessed through the flex
Tailwind utility class.
There are three key principles that support this menu system that mean we can avoid the use of JavaScript and keep the overall application snappy. The first combines the checkbox input tag, peer
class and its modifier peer-checked
. This enables a sibling or peer - which is defined as two elements that have a common parent - to react to a change in state in the target element. We’ll use this to make the navigation links visible when the checkbox is activated.
The second provides a HTML native method to trigger a change in a peer element applicable between an input tag and it’s label. Specifically, we’ll illustrate a design pattern that combines the for attribute of the label.
The third illustrates how the child elements can react to an interaction in their common parent using Tailwind’s group
utility class and subsequent modifiers. We’ll also illustrate a lesser known combination of the peer
and the group
classes that allows the dynamically change to the mobile menu.
Summary
Looking at each of the principles in turn, the first is the use of the peer
utility class and its modifier peer-checked. The example code shows two tags, an input of type checkbox and a div that contains the navigation links.
The input tag has a class peer whereas the div by default is hidden using the namesake class and uses the peer-checked modifier to become unhidden using the block class.
The figure on the right shows the effective relationship between the elements. In this instance both the input and div tags have a common parent tag called header. Making them both siblings or peers of each other. The utility class peer is then used to capture actions performed on the target element to which its peer can respond. In this example, when the checkbox is activated, the div becomes unhidden showing the navigation menu.
The second principle is a HTML native mechanism that replicates the behavior of Tailwind’s peer class but applies to specific HTML tags. The example code shows two tags an input of type checkbox and a label that contains the mobile menu button, typically, a hamburger menu icon.
The Input class has a unique ID in this instance, “menu-toggle”, The label tag contains an attribute called for which identifies the input element that the label is applicable for. It contains the ID from the input tag.
The figure on the right shows the effective relationship between the elements. Much like the previous example, both the input and label tags have a have a common parent that makes them siblings or peers. And much like the utility class peer, the HTML native relationship between these elements using the for attribute means that an action on the label is in effect forwarded it to the checkbox.
In practice it means that when the hamburger menu is clicked, it activates the checkbox. To complete the example, note that the checkbox has the class hidden as it has been replaced by its label
The final code principle combines tailwinds peer and group utility classes in transmitting the change in state of the checkbox to change the hamburger menu symbol to that one of a cross. When the menu is open.
The example code shows the same two tags as before but includes one of the two icons in this instance an SVG. The input tag utilizes the peer utility class and normally would enable its sibling or peer elements to respond to the activation of the checkbox using the peer-checked modifier.
As illustrated previously, the modifiers are only applicable to peer elements only. To transmit the change of state to the child elements of the label we can couple it with the group class. The end result is a little bit counter intuitive in terms of the class definition on the SVG.
The outcome is to hide the Hamburger symbol when the checkbox is clicked. Using the hidden class. And when the mobile menu is open, whilst displaying the cross SVG instead. The trigger is using the group class to receive all actions from the parent label using the empty square brackets and then respond to the parent’s peer checkbox activation
If you found that confusing, the figure on the right shows the effective relationship between the elements. The input and label tags are peers and the SVG is a child of a label. The SVG cannot respond to peer-modifier such as peer-checked from the input. To overcome this applying group to its parent and using the universal group modifier the SVG can now respond to peer signals received by its parent.
Blog
This video is based on a blog post called “Responsive Menus with TailwindCSS” at the home of the Curious Data Explorer channel. You can access this from the URL provided or using the QR code. We tailor our content for a variety of mediums from blog posts to podcasts to this very video. So it’s worth reading the blog post in addition to this video for more of the finer details. If you find this guide or any of our content useful then please engage with us with a comment, like or by subscribing.
Most of all tell a friend!
My name is Ashraf I’m a Curious Data Explorer.
Let’s now begin the guide!
Guide
The starting point is a HTML or HyperText Markup Language document. Which consists of a document type declaration at the top. A HTML tag with a language attribute specifying the English language. Within a pair of head tags, we have two meta tags.
The first sets the character encoding of the document in this particular case UTF or Unicode. The second resets the behavior of mobile web browsers. Which automatically scale desktop sites. This ensures no scaling occurs as we will managing the site experience directly using a responsive menu system.
On line seven we have the title of the web page and on line ten, We use the content delivery network or CDN installation method for the TailwindCSS Library. On line thirteen we have a pair of empty body tags.
The output of all of this is in effect a blank page. Let’s begin with some dummy content. Within the body tab, we include a set of header tags semantic clear, the menu sits as part the header of the document, and that will be the focus of this tutorial.
The second part is some example content. Which consists of a level one header and a paragraph with an example text. If we save the document It produces an output And this will provide some context for the menu that we’ll be developing.
For the header we set a background color of dark blue, and a default text of text-gray-300. The header consists of three parts The first is the logo. An outer div followed by a link which consists of an image file and some text. If we save this we can start to see the beginnings of our desktop menu.
The second part is the menu. This again contains an outer div semantically, we’re gonna provide a navigation so we use the nav tag which consists of unordered lists using the UL tag. There are three list items specified by the LI tag consisting of links targeting home our team and about us. If we save we can start to see that all of the text is left aligned and stacks on top of each other.
The final element is a Call-To-Action or CTA button consisting of a link and a button tag. With the words contact us. If we save all of those elements we can see on the right hand side that each item is stacked on top of each other.
The key to our desktop menu is the use of the Flexbox. Cascading Style Sheets or CSS.
To begin with we’ll focus on the header tag. First of all we specify flex. Now by default the flex box is row orientated. And in my view it’s helpful to make that explicit. So we’ll add flex-row.
Secondly we’d liked all the items to be vertically centered. So we use the term item-center And finally, add some padding This aligns the three elements horizontally in the row direction. But as you can see they’re in close contact don’t quite look at the desktop menu.
The intention is to have the logo and associated text on the left hand side and the remaining elements push the right hand side side side. . There’s a number of ways to achieve this. But one of the methods is to use the flex-1 utility class. This class means that the logo element will consume all available free space In effect pushing the other items to the right hand side.
The next step is to put the logo next to the logo text Again we use flexbox. We specify flex By default the orientation is row. So we we specify the term flex-row We want to vertically center the item so we use item-center followed by a gap of two and vertical padding of two as well.
We reduce the size of the symbol using the h10 class The next step is to modify the menu again we use flexbox plus specified flex by default it’s row orientated We want to center the items vertically for these items-center We want to separate the two items out so we use the term justifiy-between with a gap of four and a padding of four. The result looks closer to a menu which is vertically aligned.
The third element is to make the button - the Call To Action button - look like a button. Let’s start off with a bit of background so we can see the changes that we’re making To give the item its button shape we’ll add some horizontal padding some vertical padding, change the font weight to medium. Change the color to text-gray-200 and add rounded corners.
For interactivity let’s add a hover effect We change the text to white. We change the background color for something lighter: bg-sky-500. Which we can see changes as we hover over the button. At this stage we have a realistic looking desktop menu.
The desktop menu requires a tiny bit of polish. First of all let’s add a hover effect to the symbol using hover and scale-105 which increases the scale by five percent For the text itself, let’s make a number of modifications.
We’ve converted the text to uppercase. We’ve increased its size We increased its font weight. On hover the text turns to white. There’s an underline effect. And the underline offset is four pixels To see that in effect , let’s move our mouse over the logo. We can see it increases by a small margin. Likewise let’s hover over the text and we can see the underline effect What we want however, is that when we hover over one feature we want both features to be interactive.
This can be achieved using Tailwind’s group class. There are three changes that have been made The first is on line 18 we’ve added the group class This is the parent element to both the image and the span tag containing the Root Insights text For the image We’ve changed from hover to group-hover for the scale-105 percent.
Likewise for the text we’ve gone from hover for the the text color change, the underline and the underline offset to group-hover The impact of that is that now if we hover over the text the symbol increases in size or if we hover over the symbol, the text has an underline. This is the principle of the group CSS class.
Attribution
Music and audio clips from:
Pete Linforth from Pixabay
Citation
@online{2024,
author = {, miah0x41},
title = {Video \#1: {Responsive} {Menus} with {TailwindCSS}},
date = {2024-06-25},
url = {https://blog.curiodata.pro/posts/06-video-1},
langid = {en}
}