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
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:
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.
Chris Maunder is the co-founder of
CodeProject, DeveloperMedia and ContentLab, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.
In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project,
CodeProject.AI.
In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. While at CodeProject, Chris' roles included Architecture and coding, Product Development, Content Creation, Community Growth, Client Satisfaction and Systems Automation, and many, many sales meetings. All while keeping his sense of humour.