Horizontal One-click Collapse/Expand Menu

Written by @kerixa 17 May 2020

If you have an important menu items but you thing that the items occupied the useful space of your website, use a collapse/expand button for your menu. When the menu is needed, visitors open it. When the items are not more useful, they can be collapsed into a small circular button.

Code Snippet:

                                                <!-- this script is provided by https://www.javascriptfreecode.com coded by: Kerixa Inc. -->
body {
    margin: 0;
    padding: 0;
    min-height: 100vh;
    background-color: #dcdc39;
    font-family: 'Open Sans', Verdana, Geneva, Tahoma, sans-serif;

body, .nav, .menu {
    display: flex;
    justify-content: center;
    align-items: center;

.nav {
    position: relative;
    background-color: #fff;
    padding: 20px;
    transition: 0.5s;
    border-radius: 50px;
    overflow: hidden;
    box-shadow: 0 8px 15px rgba(0,0,0,.2);

.menu {
    margin: 0;
    padding: 0;
    width: 0;
    overflow: hidden;
    transition: 0.5s;

.nav input:checked ~ .menu {
    width: 450px;

.menu li {
    list-style: none;
    margin: 0 10px;

.menu li a {
    text-decoration: none;
    color: #666;
    text-transform: uppercase;
    font-weight: 600;
    transition: 0.5s;

.menu li a:hover {
    color: #161919;

.nav input {
    width: 40px;
    height: 40px;
    cursor: pointer;
    opacity: 0;

.nav span {
    position: absolute;
    left: 30px;
    width: 30px;
    height: 4px;
    border-radius: 50px;
    background-color: #666;
    pointer-events: none;
    transition: 0.5s;

.nav input:checked ~ span {
    background-color: #f974a1;

.nav span:nth-child(2) {
    transform: translateY(-8px);

.nav input:checked ~ span:nth-child(2) {
    transform: translateY(0) rotate(-45deg);
.nav span:nth-child(3) {
    transform: translateY(8px);

.nav input:checked ~ span:nth-child(3) {
    transform: translateY(0) rotate(45deg);
<div class="nav">
<input type="checkbox">
<div class="menu">
<li><a href="#">home</a></li>
<li><a href="#">about</a></li>
<li><a href="#">cursos</a></li>
<li><a href="#">blog</a></li>
<li><a href="#">contactos</a></li>
</div><a target='_blank' href='https://www.javascriptfreecode.com' style='font-size: 8pt; text-decoration: none'>JavaScript Best Codes</a>                                                


About @kerixa

I am Krishna Eydat. I studied Software Engineering at University of Waterloo in Canada. I lead a few tech companies. I am passionate about the way we are connected. I would like to be part of something big or be the big deal!



Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

Online Users: 12
Recent Members: admin_js, bloxio, yqaice, flooketsu, phuang_test
advertisement 2