Should you use a cell system to browse the web, you’ll have observed plenty of websites with burger menus. These are menus which might be hidden behind a ‘burger’ icon that the consumer can faucet on, to disclose the entire menu.
The explanation they’re known as ‘burger menus’ is due to the icon that usually represents them – three strains. It seems like slightly burger, or a minimum of that’s the speculation. I like my burgers rather less skinny!
However naming apart, having the ability to add a burger menu to your WordPress web site is one thing that may improve the consumer expertise for folks visiting on a cell system.
You can add a plugin to create a burger menu. Or you may set up a theme with one already there (like one in all ours). However what if you happen to’ve acquired your individual theme and also you’d somewhat add the burger menu your self?
What You’ll Want
To comply with together with this put up, you’ll want:
A improvement set up of WordPress operating a web site that has a menu already created.
Your personal theme or a baby theme of a 3rd get together theme. Don’t instantly edit the third get together theme or your modifications can be deleted while you replace it. As an alternative, create a baby theme if you have to.
I’m going to use this code to my very own web site. It targets the primary navigation menu, which in my case has a CSS class of .menu.fundamental. If yours is completely different you’ll must edit any CSS focusing on these courses so it applies appropriately to your individual theme.
So, let’s get began!
The Desktop Menu Model
Proper now my menu seems fantastic on desktop – it sits beneath my header banner above the content material:
However on cell issues aren’t so fairly. On an iPhone 7 the menu is cut up throughout a number of strains and doesn’t even try this constantly, It additionally will get in the way in which of the content material:
I may enhance that by centering the menu objects, however then it will take up means an excessive amount of area. As an alternative I’m going so as to add a burger menu in order that on small screens the menu is hidden till the consumer faucets on the burger icon.
Including the Burger Icon
Step one is so as to add the burger icon. You do that in your theme’s header.php file.
Notice: Should you’re utilizing a 3rd get together theme, create a baby theme, copy the header.php file from the guardian theme into that and edit the brand new file in your baby theme.
Add a hyperlink slightly below the primary navigation menu. Right here’s mine:
This creates a hyperlink with the category togglenav – as a result of it toggles the navigation on and off. Inside this hyperlink is the burger icon, which is created with an HTML image. No customized graphics required – neat, huh?
Notice that the hyperlink goes nowhere – it’s only a hashtag, not a url.
That’s all you have to add to your header file, so it can save you and shut it now.
Should you refresh your display screen you’ll see the burger icon has appeared:
We don’t need that to be seen on the desktop model of the positioning, so we’ll repair that within the subsequent step.
Hiding the Burger Icon on Giant Screens
Now for the bit the place it begins to come back collectively – the styling. You possibly can add all this in your theme’s stylesheet. Should you’re utilizing a baby theme, you’ll have already got created a stylesheet for it and might add every part there.
Notice: my theme is responsive nevertheless it isn’t cell first so I’ll be utilizing max-width in my media queries. In case your theme is cell first you’ll want to alter the way in which you add this code to media queries.
Let’s begin with the massive display screen (or desktop) model of the toggle icon. Add this to your stylesheet:
This makes the brand new hyperlink (and the icon) invisible by default. I’ve included !vital as in any other case it may be overridden by different hyperlink styling.
Now right here’s my web site on a big display screen:
It’s gone. We’ll want to change it off once more for smaller screens however we’ll come to that shortly.
Including Styling for Your Cell Burger Menu
Now you have to add all the styling for the cell model of the menu, which can seem when a consumer faucets on the icon.
First, in your stylesheet create a media question:
I’ve focused screens with a most width of 480px however you may go for wider screens if you happen to needed, particularly in case your menu is massive.
Now let’s add some styling in that media question. First we’ll flip the icon again on and magnificence that:
That may flip the icon again on for small screens, and provides positioning and color, in addition to organising hover and lively kinds to override any present kinds within the theme for hyperlinks.
Now let’s model the menu itself. Add this in your media question:
Let’s stroll by way of what this does:
It makes the menu as a complete show as an inline-block, with a strong white background and relative positioning – so we will use absolute positioning for baby parts.
It removes floats for record objects and shows them as a block.
Now save your stylesheet. Earlier than your burger menu is working correctly you’ll want so as to add the ultimate step – a script.
Including the Script
This step consists of two steps: enquiring the script and including the code to it. Let’s begin by enqueuing it.
In your theme add a folder known as scripts and inside that, an empty file known as burger-menu-script.js.
Now open your theme features file and add this to it:
This appropriately enqueues the script you simply created. Now you have to add some code to it.
Open that file and add this script:
This takes the .toggle-nav aspect we created and creates a click on name for it, which can be triggered when somebody faucets it. It then makes use of .slideToggle to toggle the navigation menu out and in when the hyperlink is clicked. It additionally stops the hyperlink behaving in its default means.
Lastly, save your file.
So now right here’s the positioning on a small display screen:
And once I faucet on that icon, the menu seems:
And right here’s a video of the way it works once I go to the positioning on my telephone (the video is a bit jerky so it’s possible you’ll wish to go to the positioning on cell to see it dwell).
All accomplished! Now all I must do is figure on my header banner, which additionally seems fairly ugly on small screens!
Including a Burger Menu Will Improve the Person Expertise on Cell
Should you comply with the steps above (modifying the CSS to fit your theme if you have to), you’ll create a easy burger menu that improves consumer expertise in your web site when folks go to it on a cell system. And if you have to, you may amend the styling, altering the colour of the icon, adjusting the width of the menu, and no matter you have to make it be just right for you.