Click here to Skip to main content
15,884,099 members
Articles / Web Development / HTML

collapsable-menu

Rate me:
Please Sign up or sign in to vote.
4.77/5 (8 votes)
11 Apr 2018Apache2 min read 10.8K   251   14   3
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:

HTML
<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

HTML
<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
<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.

This article was originally posted at https://github.com/ChrisMaunder/collapsable-menu

License

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


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, 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. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
Bugcollapse-menu not restore Pin
tsakal7-May-18 19:31
tsakal7-May-18 19:31 
QuestionMenu item bkgnd color overshoot Pin
Member 817711720-Apr-18 8:43
Member 817711720-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.