Click here to Skip to main content
13,709,159 members
Click here to Skip to main content
Add your own
alternative version

Stats

4.8K views
225 downloads
14 bookmarked
Posted 11 Apr 2018
Licenced Apache

collapsable-menu

, 11 Apr 2018
Rate this:
Please Sign up or sign in to vote.
A simple HTML menu that progressively collapses based on available width

What is Collapsable-Menu?

Collapseable Menu is a simple HTML menu that progressively collapses based on available width. I had need of a simple menu that would expand and contract horizontally depending on the width of the viewport, but I wanted to ensure that all selections would still be available regardless of the width.

For example, consider the menu

wide menu

This is fine on a wide screen, but on a narrow screen you either chop off items or wrap. What if we wanted to keep it all on a single line, and have any overflow items appear in a dropdown:

narrow menu

Simply setup your menu like:

<div class="collapse-menu">
	<div class="menu-item"><a href="#">Long Item 1</a></div>
    <div class="menu-item"><a href="#">Item 2</a></div>
    <div class="menu-item"><a href="#">Extremely Long Item 3</a></div>
    <div class="menu-item"><a href="#">Item 4</a></div>
    <div class="menu-item"><a href="#">Item 5</a></div>
    <div class="menu-item"><a href="#">Medium Item 6</a></div>
</div>

and include the collapse-menu.min.js and collapse-menu.min.css (1.7Kb total) and you're done.

When the containing element gets too narrow to contain the menu an item will be added labelled "..." which will have a dropdown menu containing the overflowing items.

You can also supply a data-promoted-class attribute to the main collapse-menu meny wrapper to specify a class name that should be applied to the dropdown menu's anchor button if it appears on any item that has overflowed.

For example

<div class="collapse-menu" data-promoted-class="selected">
	<div class="menu-item"><a href="#">Long Item 1</a></div>
    <div class="menu-item"><a href="#">Item 2</a></div>
    <div class="menu-item"><a href="#">Extremely Long Item 3</a></div>
    <div class="menu-item selected"><a href="#">Item 4</a></div>
    <div class="menu-item"><a href="#">Item 5</a></div>
    <div class="menu-item"><a href="#">Medium Item 6</a></div>
</div>

If item 4 is forced into the dropdown menu then the dropdown menu will have the "selected" class added to it.

Limitations

This is a single-level menu. It doesn't support dropdown menus from any of the top level menu items. It's just not designed that that type of application.

Installation

Simply copy the .js and .css files into a handy directory and reference them from your HTML page. You'll need to ensure you have jQuery included as well:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="collapse-menu.min.js"></script>
<link type="text/css" rel="stylesheet" href="collapse-menu.min.css">
</head>
<body>

Contributing

Feel free to improve the menu and send a pull request. The code itself is inside a Visual Studio 2017 solution simply to enable CSS and Javascript minification using Mads' minifier addin. The actual code is in the src directory and can be used as-is in whatever editor and environment you wish.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

Comments and Discussions

 
Bugcollapse-menu not restore Pin
tsakal7-May-18 19:31
membertsakal7-May-18 19:31 
QuestionMenu item bkgnd color overshoot Pin
20-Apr-18 8:43
member20-Apr-18 8:43 
QuestionMy vote of 5! Pin
jediYL12-Apr-18 13:00
professionaljediYL12-Apr-18 13:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 11 Apr 2018
Article Copyright 2018 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid