Responsive Top Navigation Menu
In my quest to design a perfect three levels down responsive top navigation menu for SharePoint, I’ve stumbled on some annoyances that only people dealing with branding the SharePoint sites will understand me.
First, we need to give a bit of credit to MS for allowing us to use CSS instead of the old fashioned tables by setting the UseSimpleRendering property to “True”. An improvement, but far from a smooth sailing.
Second, why in the name of Merlin’s saggy left, MS decided to use the ClientIDMode property to generate and insert the pesky ID, zz2_RootAspMenu, zz13_RootAspMenu or any other random dynamic IDs for the unordered list?!? Inserting the above peskiness, wouldn’t be that bad if it was a static ID that you could make use in your CSS, but you might get different IDs when viewed logged in or anonymous.
So, why not use the class assigned to the UL (.root.ms-core-listMenu-root.static), one might say. That is because MS screwed the pooch again. They used the same class for the top navigation menu and the left navigation. Imagine that!
OK, you could use the CssClass property of the AspMenu to assign your own class but then you have to make use of the !important attribute way too often to override the MS default menu styles. Not healthy at all.
So, here is my solution. I used the jQuery to replace the annoying IDs assigned to the UL with my own. And now I have full control over the CSS.
// Replace that pesky automatic ul ID $( "#zz13_RootAspMenu" ).attr( "id", "myTopNav" ); $( "#zz2_RootAspMenu" ).attr( "id", "myTopNav" ); |
To get the files used in this example, download the topMenu.zip package and extract the files. There are two files in the package: topMenu.master and topMenu.css
topMenu.master is just a slight modified version of the original oslo.master. I used it to load the necessary code. You can use your own master page if you have one.
If you’re not using the topMenu.master included in the package then add the following code to your own master page.
Add the following code right before the end of tag.
-- Start Responsive Top Navigation Meta and CSS file -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<SharePoint:CssRegistration name="/_catalogs/masterpage/topMenu/topMenu.css" After="corev15.css" runat="server" />
-- Start Responsive Top Navigation Meta and CSS file -->
|
Next look for “DeltaHorizontalQuickLaunch” and replace the whole navigation control with this:
-- Start Responsive Top Navigation -->
<nav id="menu-wrap">
<SharePoint:AjaxDelta id="DeltaHorizontalQuickLaunch" runat="server">
<Sharepoint:SPNavigationManager id="QuickLaunchNavigationManager" runat="server" QuickLaunchControlId="V4QuickLaunchMenu" ContainedControl="QuickLaunch" EnableViewState="false" >
<SharePoint:DelegateControl runat="server" ControlId="QuickLaunchDataSource"><Template_Controls><asp:SiteMapDataSource SiteMapProvider="SPNavigationProvider" id="QuickLaunchSiteMap" ShowStartingNode="False" StartingNodeUrl="sid:1002" runat="server" />Template_Controls>SharePoint:DelegateControl>
<SharePoint:AspMenu id="topNav" runat="server" EnableViewState="false" DataSourceId="QuickLaunchSiteMap" Orientation="Horizontal" StaticDisplayLevels="1" DynamicHorizontalOffset="0" AdjustForShowStartingNode="true" MaximumDynamicDisplayLevels="3" SkipLinkText="" UseSimpleRendering="True" />
Sharepoint:SPNavigationManager>
SharePoint:AjaxDelta>
nav>
-- End Responsive Top Navigation -->
|
Next, right before the end of the
Hi,
There are some points, that You should consider:
1. That annoying menu ID “zz13_RootAspMenu” actually consists of 3 parts: a) constant prefix (i.e. “zz”); b) master page/publishing page master publishing version number (i.e. “13”); c) constant prefix (i.e.” _RootAspMenu”). Every time You update Your master page to new major version, that ID “#zz13_RootAspMenu” gets new value (it is possible to use jQuery’s selector, that checks attribute’s ending – example: $(‘[id$=”_RootAspMenu “]’) ).
2. You could user parent container’s ID/Class for CSS selector as from final outputted HTML (check rendered SharePoint page HTML source or use HTML inspector).
So, in short, You should consider not using jQuery for that purpose, instead, use CSS as much as possible, as SharePoint by default is a little bit slow (especially 2013 version) and by default, there is no jQuery in SharePoint.
Hi Luis,
I copied the code from topmenu.master and pasted it in oslo master. However i do not see a change in my top navigation menu.
Regards,
Ren.
Renato, I’m pretty sure these were not the directions provided. Please pay attention to the steps above.
Works great with the quick launch Navigation. Do we have to change the CSS too beside the navigation provider if it needs to work with Global Navigation?
Luis maybe your example needs a little more details about the type of navigation you are using, what type of site you use, also from where you get menu links (ABOUT US, CONTACT US, etc.). I done exactly you explained using both files you provided in a publishing site and menu does not appear. Only I have a thin blue line without links. I’m using Global Navigation – Structural Navigation – Show pages. Can you help us?
Luis:
I really liked this – I installed it in Office 365 and it appears to work OK. – if slightly different from what you show.
Hi,
Thanks in advance, i found that
if the user with read permission logs in site, css not loading for menu. It means not able to change the id of menu onload.
what could be the fix for such case.
Same problem with me.
Domain users with read permission logs in site, css not loading for menu. Only me as admin can see the nav bar.
What could be the fix for such case?
I am using SharePoint 2013.
Thank you for your post, but unfortunately I have some trouble with this solution. I downloaded the zipfile, uploaded both files, published the main version, changed the master page and told the path to the additional CSS file. So far so good. The master page changed and obviously the CSS was also loaded. But the page doesn’t look like your image “Responsive Top Navigation 31”. Instead it looks like “Responsive Top Navigation 2”. The menu isn’t top-down oriented but left-right with lue background and white font style.
Did I miss something? I cleared the cache already and tried different Browsers (Firefox, Internet Explorer and Edge).
airliner, most likely your CSS is overridden by other CSS.
Luis, it seems that there are invalid values for some points. I get some warnings at background-image (where you use o-linear-gradient and -ms-linear-gradient), at -moz-border-radius and at zoom.
Thanks!
Are all files checked in and published? Users have read permission on the Style Library?
Thanks for the article! I know it’s almost 2019, but I’m struggling with the responsive menu in SharePoint 2016 combined with the structural navigation.
With bootstrap everything seems fine until you try to go down to the third level of the navigation. Therefore everything’s waiting for hover, it’s quite complicated to use on mobile devices which doesn’t have the ability to “hover” something. Either it’s clicked/tapped or not.
Do you have any suggestions how to handle this?