Newer Genesis child themes like Workstation Pro and Atmosphere Pro come with responsive mobile hamburger menu code which is better than the one in older themes like Beautiful Pro or Magazine Pro in that accessibility is taken care of.
In this article I share the code taken from Workstation Pro and slightly modified so that it works for not just Primary Menu but Header Menu and Secondary Menu as well.
When the hamburger icon or MENU text is clicked,
Step 1
Create a file named responsive-menu.js in your child theme’s js
directory (create if not existing) having the following code:
Step 2
Add the following in child theme’s functions.php:
// Enqueue Scripts and Styles | |
add_action( 'wp_enqueue_scripts', 'custom_enqueue_scripts_styles' ); | |
function custom_enqueue_scripts_styles() { | |
// wp_enqueue_style( 'google-fonts', '//fonts.googleapis.com/css?family=Roboto+Condensed:300italic,700italic,700,300', array(), CHILD_THEME_VERSION ); | |
wp_enqueue_style( 'dashicons' ); | |
wp_enqueue_script( 'responsive-menu', get_stylesheet_directory_uri() . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0', true ); | |
$output = array( | |
'mainMenu' => __( 'Menu', 'my-theme-text-domain' ), | |
'subMenu' => __( 'Menu', 'my-theme-text-domain' ), | |
); | |
wp_localize_script( 'responsive-menu', 'ResponsiveMenuL10n', $output ); | |
} |
Step 3
Add the following in child theme’s style.css:
/* Responsive Menu | |
--------------------------------------------- */ | |
.sub-menu-toggle, | |
.menu-toggle { | |
display: none; | |
visibility: hidden; | |
} | |
@media only screen and (max-width: 880px) { | |
.js nav { | |
display: none; | |
position: relative; | |
} | |
.js nav .wrap { | |
padding: 0; | |
} | |
.js nav.pagination { | |
display: block; | |
} | |
.menu-toggle, | |
.sub-menu-toggle { | |
background-color: #222; | |
color: #fff; | |
display: block; | |
margin: 0 auto; | |
overflow: hidden; | |
text-align: center; | |
visibility: visible; | |
} | |
.menu-toggle { | |
position: relative; | |
right: 0; | |
z-index: 1000; | |
width: 100%; | |
} | |
.menu-toggle:before, | |
.menu-toggle.activated:before { | |
color: #fff; | |
content: "\f333"; | |
display: inline-block; | |
font: normal 20px/1 'dashicons'; | |
margin: 0 auto; | |
padding-right: 10px; | |
text-rendering: auto; | |
-webkit-transform: translate(0, 0); | |
-ms-transform: translate(0, 0); | |
transform: translate(0, 0); | |
/*vertical-align: bottom;*/ | |
vertical-align: top; | |
padding-top: 3px; | |
} | |
.sub-menu-toggle { | |
float: right; | |
padding: 18px; | |
position: absolute; | |
right: 0; | |
top: 0; | |
z-index: 100; | |
} | |
.sub-menu-toggle:before { | |
content: "\f347"; | |
display: inline-block; | |
font: normal 16px/1 'dashicons'; | |
text-rendering: auto; | |
-webkit-transform: translate(0, 0); | |
-ms-transform: translate(0, 0); | |
transform: translate(0, 0); | |
} | |
.sub-menu-toggle.activated:before { | |
content: "\f343"; | |
} | |
.js .genesis-nav-menu .menu-item { | |
display: block; | |
float: none; | |
position: relative; | |
text-align: left; | |
} | |
.js .genesis-nav-menu .menu-item:hover { | |
position: relative; | |
} | |
.js .genesis-nav-menu .menu-item a { | |
border: none; | |
color: #fff; | |
/*font-family: 'Roboto Condensed', sans-serif;*/ | |
padding: 20px; | |
width: 100%; | |
} | |
.js .genesis-nav-menu .menu-item a:hover, | |
.js .genesis-nav-menu .sub-menu { | |
border: none; | |
} | |
.js .genesis-nav-menu .menu-item > a:focus ul.sub-menu, | |
.js .genesis-nav-menu .menu-item > a:focus ul.sub-menu .sub-menu { | |
left: 0; | |
margin-left: 0; | |
} | |
.js .genesis-nav-menu > .menu-item-has-children > a:after { | |
content: none; | |
} | |
.js .genesis-nav-menu .sub-menu { | |
clear: both; | |
display: none; | |
margin: 0; | |
opacity: 1; | |
position: static; | |
width: 100%; | |
padding-left: 20px; | |
} | |
.js .genesis-nav-menu .sub-menu .sub-menu { | |
margin: 0; | |
} | |
.js .genesis-nav-menu .sub-menu a { | |
border: none; | |
} | |
.js nav .genesis-nav-menu .menu-item .sub-menu li a, | |
.js nav .genesis-nav-menu .menu-item .sub-menu li a:hover, | |
.js nav button:hover, | |
.js .menu-toggle:hover, | |
.js .nav-primary { | |
background-color: #222; | |
color: #fff; | |
} | |
.js nav .genesis-nav-menu .menu-item .sub-menu li a:focus, | |
.js nav .genesis-nav-menu .menu-item a:focus, | |
.js nav button:focus, | |
.js .menu-toggle:focus { | |
background-color: #222; | |
color: #ff4800; | |
} | |
.js .nav-header .genesis-nav-menu .menu-item a, | |
.js .nav-secondary .genesis-nav-menu .menu-item a { | |
color: #333; | |
} | |
} |
Due to the vast number of available Genesis child themes, it is practically not possible to come up with CSS that makes the mobile menus look good in all of them. Therefore you will need to tweak the CSS so it matches your theme/design.