How to Remove the Menu Container div in WordPress

WebTechRiser.com > WordPress > How to Remove the Menu Container div in WordPress

On WordPress, wp_nav_menu() function is used to display a navigation menu. But, by default, the menu will be wrapped in a <div> container.

by default, the menu will be wrapped in a <div> container.
By default, the menu will be wrapped in a <div> container.

Since wp_nav_menu() displays list items, this ends up outputting invalid HTML.

A <div> is not allowed as a direct descendant of a <ul> element.

Since this is invalid HTML, you’ll probably see an error when you try to validate your website in Pingdom Tools or in “The W3C Markup Validation Service“. You may get the following error:

Element div not allowed as child of element ul in this context.

In order to get rid of the container <div>, you can pass an empty string () value as parameter named, “container” in wp_nav_menu():

wp_nav_menu(
    array(
        'menu' => 'mymenu',
        'container' => ''
    )
);

If you inspect your menu in the browser, you should notice that the container <div> is gone.

You can visit the official documentation page of WordPress for details about the parameters contained in wp_nav_menu().

Also read:  How To Customize Excerpts in WordPress to Remove the […] Programmatically
Category WordPress

Leave Your Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.