How to Create Responsive Mega Menus With UIkit



In this tutorial, we’ll continue exploring the UIkit framework and learn how to build responsive mega menus by taking advantage of its components.
As with other UIkit tutorials, a familiarization with this framework will be beneficial to follow along yet not mandatory.
Our UIkit Mega Menus
Here’s our first mega menu, which displays within a panel (full page view):
And the second one, which uses a full width container (full page view):
To check how the mega menus behave on different screens, be sure to view them from a large screen and resize your browser window. You'll notice that:
- On mobile screens, they are part of a single off-canvas menu.
- On large screens (≥1200px), their appearance differs. On the first demo, the mega menus’ width depends on the top menu’s width. However, on the second demo, the mega menus’ width is equal to the viewport width. Additionally, in this case, they appear with slide animation.
We’ll Need These UIkit Components
Assuming that we’ve installed the required UIkit files, let’s look at the main UIkit components that we’ll use to build the menus:
To thoroughly understand what each component or utility class that we’re going to use does, don't forget to inspect the generated markup and styles through your browser inspector.
Mega Menu Example #1
On small screens, the site header will look like this:



In a classic scenario, to reveal the off-canvas menu, we’ll click on the hamburger toggle button:

On large screens, the top menu will appear while the off-canvas toggle button will become hidden:



The mega menus will emerge like this:



The HTML
The header will contain a nav
element. Within it, we’ll put:
- The logo
- The menu
- The search bar toggle button
- A link to the wishlist page
- The mobile menu (off-canvas) toggle button
- The off-canvas
Let’s highlight the key things:
- From the above header elements, the search bar toggle button and the link that sends to the wishlist page won’t have any functionality as this is beyond the scope of this tutorial.
- The off-canvas toggle and the off-canvas itself will appear on screens smaller than 1200px. We’ll do this by attaching the
uk-hidden@l
utility class to the target elements. - On the contrary, the top menu will appear on screens at least 1200px wide. We’ll do this by attaching the
uk-visible@l
class to it. - To initialize the UIkit navbar we’ll pass the
uk-navbar
attribute to thenav
element. - To create the mega menus of the first two items of the top menu we’ll use the
uk-dropdown
attribute from the dropdown component. Here we’ll pass specific options to this attribute for defining the boundaries of the mega menus, their position, and the offset between the mega menu and its associated toggle. You can, even more, customize it as you wish. For instance, try to add themode: click
option for forcingclick
mode only. - We’ll use the
uk-column-1-*
classes to specify the desired width for the columns inside the mega menus. That said, the mega menu of the first item of the top menu will haveuk-width-1-3
as it contains three lists, while the mega menu of the second item will haveuk-width-1-2
as it contains two lists. - Similarly, for the sub-menu of the last item of the top menu, we’ll also use the
uk-dropdown
attribute. However, here we’ll customize only its offset to match the other menus offset. This won’t be a mega menu though, thus it will sit below the associated toggle by default. - The off-canvas menu will be built by following the structure of the off-canvas component. To customize its behavior according to our needs, we’ll pass a few options to the required
uk-offcanvas
attribute. For instance, themode
option determines the animation mode. Possible values areslide
,reveal
,push
, andnone
. In our case, we’ll go with thereveal
mode.
Here’s the full markup:
1 |
<header class="page-header uk-box-shadow-medium uk-background-default" uk-sticky> |
2 |
<div class="uk-container uk-container-expand uk-height-1-1"> |
3 |
<nav class="uk-height-1-1" uk-navbar> |
4 |
<a href="" class="uk-flex uk-flex-middle"> |
5 |
<img width="178" height="38" src="horizontal-logo-mobile.svg" alt="forecastr logo"> |
6 |
</a>
|
7 |
|
8 |
<ul class="uk-navbar-nav uk-navbar-center uk-visible@l"> |
9 |
<li class="multi"> |
10 |
<a href=""> |
11 |
Platform |
12 |
<span uk-icon="icon: chevron-down"></span> |
13 |
</a>
|
14 |
|
15 |
<div class="uk-navbar-dropdown" uk-dropdown="boundary: .uk-navbar-nav; boundary-align: true; pos: bottom-justify; offset: 10"> |
16 |
<ul class="uk-nav uk-navbar-dropdown-nav uk-column-1-3"> |
17 |
<li>
|
18 |
<span uk-icon="icon: cog"></span> |
19 |
<ul class="uk-nav uk-navbar-dropdown-nav uk-margin-top"> |
20 |
<li class="uk-nav-header uk-margin-small-bottom uk-text-bold">For Companies</li> |
21 |
<li><a href="#">Build</a></li> |
22 |
<li><a href="#">Train</a></li> |
23 |
<li><a href="#">Compare</a></li> |
24 |
</ul>
|
25 |
</li>
|
26 |
<!-- two more items here -->
|
27 |
</ul>
|
28 |
</div>
|
29 |
</li>
|
30 |
<li>
|
31 |
<a href="">Customers</a> |
32 |
</li>
|
33 |
<li class="multi"> |
34 |
<a href=""> |
35 |
Solutions |
36 |
<span uk-icon="icon: chevron-down"></span> |
37 |
</a>
|
38 |
<div class="uk-navbar-dropdown" uk-dropdown="boundary: .uk-navbar-nav; boundary-align: true; pos: bottom-justify; offset: 10"> |
39 |
<ul class="uk-nav uk-navbar-dropdown-nav uk-column-1-2"> |
40 |
<li>
|
41 |
<span uk-icon="icon: lifesaver"></span> |
42 |
<ul class="uk-nav uk-navbar-dropdown-nav uk-margin-top"> |
43 |
<li class="uk-nav-header uk-margin-small-bottom uk-text-bold">Business</li> |
44 |
<li><a href="#">For Marketing</a></li> |
45 |
<li><a href="#">For Sales</a></li> |
46 |
</ul>
|
47 |
</li>
|
48 |
<li>
|
49 |
<span uk-icon="icon: comments"></span> |
50 |
<ul class="uk-nav uk-navbar-dropdown-nav uk-margin-top"> |
51 |
<li class="uk-nav-header uk-margin-small-bottom uk-text-bold">Education</li> |
52 |
<li><a href="#">For Teachers</a></li> |
53 |
<li><a href="#">For Students</a></li> |
54 |
</ul>
|
55 |
</li>
|
56 |
</ul>
|
57 |
</div>
|
58 |
</li>
|
59 |
<li>
|
60 |
<a href="">Pricing</a> |
61 |
</li>
|
62 |
<li>
|
63 |
<a href=""> |
64 |
Resources |
65 |
<span uk-icon="icon: chevron-down"></span> |
66 |
</a>
|
67 |
<div class="uk-navbar-dropdown" uk-dropdown="offset: 10"> |
68 |
<ul class="uk-nav uk-navbar-dropdown-nav"> |
69 |
<li><a href="#">Blog</a></li> |
70 |
<li><a href="#">Documentation</a></li> |
71 |
<li><a href="#">FAQ</a></li> |
72 |
</ul>
|
73 |
</div>
|
74 |
</li>
|
75 |
</ul>
|
76 |
|
77 |
<div class="uk-navbar-right"> |
78 |
<button class="uk-icon-link uk-margin-right" uk-icon="search" type="button" aria-label="Toggle Search Form"></button> |
79 |
<a href="#" class="uk-icon-link" uk-icon="heart" aria-label="Open Wishlist Page"></a> |
80 |
<button class="uk-margin-left uk-hidden@l" uk-toggle="target: #offcanvas" uk-icon="icon: menu" type="button" aria-label="Toggle Mobile Menu"></button> |
81 |
</div>
|
82 |
</nav>
|
83 |
</div>
|
84 |
|
85 |
<div class="offcanvas uk-hidden@l" id="offcanvas" uk-offcanvas="flip: true; overlay: true; mode: reveal"> |
86 |
<div class="uk-offcanvas-bar"> |
87 |
<button class="uk-offcanvas-close" type="button" uk-close aria-label="Close Mobile Menu"></button> |
88 |
|
89 |
<ul class="top-menu uk-nav uk-margin-top"> |
90 |
<li>
|
91 |
<a href=""> |
92 |
Platform |
93 |
</a>
|
94 |
<ul class="sub-menu uk-nav"> |
95 |
<li class="uk-nav-header uk-text-bold">For Companies</li> |
96 |
<li><a href="#">Build</a></li> |
97 |
<li><a href="#">Train</a></li> |
98 |
<li><a href="#">Compare</a></li> |
99 |
</ul>
|
100 |
<!-- twp more lists here -->
|
101 |
</li>
|
102 |
<!-- more itesm here -->
|
103 |
</ul>
|
104 |
</div>
|
105 |
</div>
|
106 |
</header>
|
uk-drop
attribute instead of the uk-dropdown
one with the same result.The CSS
Coming up next, we’ll specify some straightforward styles. Nothing really important. Here they are:
1 |
.page-header { |
2 |
height: 80px; |
3 |
}
|
4 |
|
5 |
.page-header .uk-navbar-nav > li > a, |
6 |
.page-header .uk-icon-link, |
7 |
.page-header .uk-icon-link:hover { |
8 |
color: #333; |
9 |
}
|
10 |
|
11 |
.page-header .uk-navbar-nav > li > a { |
12 |
padding: 0 25px; |
13 |
}
|
14 |
|
15 |
.page-header .uk-navbar-nav > li > a:hover { |
16 |
background: #f6f7f9; |
17 |
}
|
18 |
|
19 |
.page-header .multi .uk-navbar-dropdown { |
20 |
padding: 0 25px; |
21 |
}
|
22 |
|
23 |
.page-header .uk-navbar-dropdown-nav .uk-nav-header { |
24 |
color: #7c2a8a; |
25 |
}
|
26 |
|
27 |
.page-header [class*="uk-column-"] > li { |
28 |
padding: 25px 0; |
29 |
}
|
30 |
|
31 |
.page-header [class*="uk-column-"] > li:not(:last-child) { |
32 |
border-right: 1px solid #e5e5e5; |
33 |
}
|
34 |
|
35 |
.page-header [class*="uk-column-"] .uk-icon { |
36 |
color: #c6c6c6; |
37 |
}
|
38 |
|
39 |
.offcanvas .uk-nav-header { |
40 |
color: rgba(246, 247, 249, 0.5); |
41 |
}
|
42 |
|
43 |
.offcanvas .top-menu > li + li { |
44 |
margin-top: 10px; |
45 |
}
|
46 |
|
47 |
.offcanvas .sub-menu { |
48 |
border-left: 1px solid #484848; |
49 |
}
|
50 |
|
51 |
.offcanvas .sub-menu li { |
52 |
padding-left: 30px; |
53 |
}
|
54 |
|
55 |
.offcanvas .sub-menu li.uk-nav-header { |
56 |
padding-left: 10px; |
57 |
}
|
Mega Menu Example #2
In this second demo, the only thing that will change is the way that the sub-menus will appear:



The HTML
The markup will stay the same, yet we’ll do the following small changes:
- Add the
dropbar: true
option to theuk-navbar
attribute of thenav
element. This will generate the slide animation for our mega menus by appending an emptydiv
element with the class ofuk-navbar-dropbar
as a sibling of thenav
. - Remove the predefined top and bottom paddings from the mega menus of the first two items of the top menu by adding the
uk-padding-remove-top
anduk-padding-remove-bottom
utility classes. - Remove the offsets from all dropdowns.
- Remove the
uk-dropdown
attribute from the sub-menu of the last item of the top menu as we don't need it (we removed the offset).
Here’s the main header structure:
1 |
<header class="page-header uk-box-shadow-medium uk-background-default" uk-sticky> |
2 |
<div class="uk-container uk-container-expand uk-height-1-1"> |
3 |
<nav class="uk-height-1-1" uk-navbar="dropbar: true"> |
4 |
<a href="" class="uk-flex uk-flex-middle"> |
5 |
<img width="178" height="38" src="horizontal-logo-mobile.svg" alt="forecastr logo"> |
6 |
</a>
|
7 |
|
8 |
<!-- top menu with mega menus here -->
|
9 |
|
10 |
<div class="uk-navbar-right"> |
11 |
<button class="uk-icon-link uk-margin-right" uk-icon="search" type="button" aria-label="Toggle Search Form"></button> |
12 |
<a href="#" class="uk-icon-link" uk-icon="heart" aria-label="Open Wishlist Page"></a> |
13 |
<button class="uk-margin-left uk-hidden@l" uk-toggle="target: #offcanvas" uk-icon="icon: menu" type="button" aria-label="Toggle Mobile Menu"></button> |
14 |
</div>
|
15 |
</nav>
|
16 |
</div>
|
17 |
|
18 |
<!-- off-canvas here -->
|
19 |
</header>
|
Conclusion
In this tutorial, we wore the front-end developer’s hat and used UIkit components for creating responsive site headers with mega menus on their desktop version. Of course, this is just one implementation. You can certainly build them with UIkit in another way.
Without a doubt, UIkit does a great job of giving us the tools to build complex mega menus with minimal effort and a lot of customization settings. At this point, you should hopefully have a good understanding of how to set up mega menus with the help of UIkit.
Uikit Mega Menu in WordPress
But wait a moment! Is having the markup for one or two mega menus really enough? Do you agree that it will be even more effective if we make dynamic version of these menus? I'm sure you’ve raised your hand at this point!
The good news is that in another tutorial, we’ll wear the WordPress developer’s hat and challenge ourselves to convert one of these menus into a dynamic one by using the Walker Class. So stay tuned!
As always, thanks a lot for reading!