Concealing the side navigation menu with HTML and CSS

Hey there, I'm trying to make my navbar automatically hide when the mouse is moved. I found an example that I want to use here. Despite reading some online documentation, I just can't seem to figure out how to do it. Here's a snippet of my code:

li .glyphicon {
            margin-right: 10px;
        }
        
        /* Highlighting rules for nav menu items */
        li.link-active a,
        li.link-active a:hover,
        li.link-active a:focus {
            background-color: #4189C7;
            color: white;
        }

        /* Keep the nav menu independent of scrolling and on top of other items */
        .main-nav {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1;
        }
        
        @media (min-width: 768px) {
            /* On small screens, convert the nav menu to a vertical sidebar */
            .main-nav {
                height: 100%;
                width: calc(25% - 20px);
            }
            .navbar {
                border-radius: 0px;
                border-width: 0px;
                height: 100%;
            }
            .navbar-header {
                float: none;
            }
            .navbar-collapse {
                border-top: 1px solid #444;
                padding: 0px;
            }
            .navbar ul {
                float: none;
            }
            .navbar li {
                float: none;
                font-size: 15px;
                margin: 6px;
            }
            .navbar li a {
                padding: 10px 16px;
                border-radius: 4px;
            }
            .navbar a {
                /* If a menu item's text is too long, truncate it */
                width: 100%;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

        <div class='main-nav'>
            <div class='navbar navbar-inverse'>
                <div class='navbar-header'>
                    <a class='navbar-brand' [routerLink]="['/home']">Navbar</a>
                    <button type='button' class='navbar-toggle' data-toggle='collapse' data-target='.navbar-collapse'>
                        <span  (click)="isCollapsed = !isCollapsed">Toggle navigation</span>
                        <span class='icon-bar'></span>
                        <span class='icon-bar'></span>
                        <span class='icon-bar'></span>
                        <span class='icon-bar'></span>
                    </button>
                </div>
                <div class='clearfix'></div>
                <div class='navbar-collapse collapse' >
                    <ul class='nav navbar-nav'>
                        <li [routerLinkActive]="['link-active']">
                            <a [routerLink]="['/home']">
                                <span class='glyphicon glyphicon-home'></span> Home
                            </a>
                        </li>
                        <li [routerLinkActive]="['link-active']">
                            <a [routerLink]="['/login']">
                                <span class='glyphicon glyphicon-log-in'></span> Log In
                            </a>
                        </li>
                        <li [routerLinkActive]="['link-active']">
                            <a [routerLink]="['/margin']">
                                <span class='glyphicon glyphicon-list'></span> Report
                            </a>
                        </li>
                        <li [routerLinkActive]="['link-active']">
                            <a [routerLink]="['/smart-table']">
                                <span class='glyphicon glyphicon-list'></span> Smart table
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

I'm unsure where to add the specific code. I've come across suggestions about using a JavaScript function or built-in Bootstrap functions, but they don't seem to work for me. Any assistance would be greatly appreciated.

Thanks in advance!

Answer №1

To achieve this desired effect, you can utilize just a few lines of CSS.

Start by creating a container for your navigation item labels, such as .nav-label:

...
<span class='glyphicon glyphicon-home'></span> <span class="nav-label">Home</span>
...

Next, implement a change in the width of the .main-nav upon hover, apply a transition effect, and initially hide the .nav-labels:

.main-nav {
  height: 100%;
  width: 100px;
  transition: all .1s linear;
}
.main-nav:hover {
  width: calc(25% - 20px)
}
.main-nav:hover .nav-label {
  display: inline-block;
}

Refer to the interactive example below with your code (switch to fullscreen mode to see the sidebar in action):

li .glyphicon {
  margin-right: 10px;
}

/* Styling for active nav menu items */
li.link-active a,
li.link-active a:hover,
li.link-active a:focus {
  background-color: #4189C7;
  color: white;
}

/* Keep the nav menu fixed at the top and on top of other elements */
.main-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1;
}

.main-nav .navbar-nav>li {
  float: none;
}

.nav-label {
  display: none;
  margin-left: 2em;
}

@media (min-width: 768px) {
  /* On smaller screens, transform the horizontal nav menu into a vertical sidebar */
  .main-nav {
    height: 100%;
    width: 100px;
    transition: all .1s linear;
  }
  .main-nav:hover {
    width: calc(25% - 20px)
  }
  .main-nav:hover .nav-label {
    display: inline-block;
  }
  .navbar {
    border-radius: 0px;
    border-width: 0px;
    height: 100%;
  }
  .navbar-collapse {
    border-top: 1px solid #444;
    padding: 0px;
  }
  .navbar li {
    display: block;
    font-size: 15px;
    margin: 6px;
  }
  .navbar li a {
    display: block;
    padding: 10px 16px;
    border-radius: 4px;
  }
  .navbar a {
    /* Truncate menu item text if too long */
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

<div class='main-nav'>
  <div class='navbar navbar-inverse'>
    <div class='navbar-header'>
      <a class='navbar-brand' [routerLink]="['/home']">Navbar</a>
      <button type='submit' class='navbar-toggle' data-toggle='collapse' data-target='.navbar-collapse'>
          <span  (click)="isCollapsed = !isCollapsed">Toggle navigation</span>
          <span class='icon-bar'></span>
          <span class='icon-bar'></span>
          <span class='icon-bar'></span>
          <span class='icon-bar'></span>
      </button>
    </div>
    <div class='clearfix'></div>
    <div class='navbar-collapse collapse'>
      <ul class='nav navbar-nav'>
        <li [routerLinkActive]="['link-active']">
          <a [routerLink]="['/home']">
            <span class='glyphicon glyphicon-home'></span> <span class="nav-label">Home</span>
          </a>
        </li>
        <li [routerLinkActive]="['link-active']">
          <a [routerLink]="['/login']">
            <span class='glyphicon glyphicon-log-in'></span> <span class="nav-label">Log In</span>
          </a>
        </li>
        <li [routerLinkActive]="['link-active']">
          <a [routerLink]="['/margin']">
            <span class='glyphicon glyphicon-list'></span> <span class="nav-label">Report</span>
          </a>
        </li>
        <li [routerLinkActive]="['link-active']">
          <a [routerLink]="['/smart-table']">
            <span class='glyphicon glyphicon-list'></span> <span class="nav-label">Smart table</span>
          </a>
        </li>
      </ul>
    </div>
  </div>
</div>

Answer №2

If you're looking to enhance your navigation experience, consider incorporating mouseenter and mouseleave events to dynamically add classes to your navigation elements.

Here's an example:

$(document).on('mouseenter', '.main-nav', function() {
  $(this).addClass('expanded'); //Add Class for full nav
});

$(document).on('mouseleave', '.main-nav', function() {
  $(this).removeClass('expanded');  //Return to small nav
});

You can achieve a similar effect using JavaScript, or opt for a simpler CSS solution by adjusting the display and width properties of specific navigation elements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to align input fields and spans side by side

Is there a way to properly align multiple inputs next to each other with dots in between, resembling an IPv4 address? Currently, the span appears to float in the center. How can this be resolved? It's worth noting that all elements in the image have b ...

Customizing Big Cartel's Neat theme: A guide to eliminating the blur effect on featured product images upon site activation

My website is built on Big Cartel using the Neat theme. Every time the homepage loads, the Featured products images appear blurry for about 4 seconds, especially if you're not using Chrome browser. Is there a way to remove this blur effect? Thank yo ...

Each container has its own div counter

Help needed to complete this code. The task is to count the number of .resp-containers in each container separately. Then, based on that count, assign a corresponding class to each element within the containers. Check out the code here $(document).ready(f ...

Display/conceal within a jQuery fixed navigation bar (center-aligned)

I am facing challenges with creating a sticky menu that shows/hides with a click button. Considering abandoning the show/hide feature altogether and rebuilding it from scratch in the future. Two major problems I have identified: How can I make the sho ...

Unable to assign a height of 100% to the tr element or a width of 100% to the

While inspecting the elements, I noticed the presence of <tbody>, within which <tr> is nested. The issue arises when the tbody element has 100% height and width as intended, but <tr> always falls short by 4 pixels in height even when styl ...

What is the best way to send data to PHP while moving objects between different div elements?

I have a situation where I have two parent divs containing dynamic children divs, and I want to enable POST requests to PHP when items are dragged from one side to the other (and vice versa). Here is the Javascript code I am using: function allowDrop( ...

Issue with jQuery DataTables column.width setting failing to meet expectations

Currently, I am utilizing datatables for my project. According to the datatables documentation found here, it suggests using the columns.width option to manage column width. However, when I implement columns.width and render the table, it seems to disreg ...

How to Use Vue.js to Find the Nearest Div Element with a Specific

Below is the HTML code I am working with: <div id="app"> <div class="image"> <div class="overlay"> <p>Some overlay text</p> </div> <img src="https://placeimg.com/640/480/any" class="img-fluid"> ...

ShadowBox not displaying Vimeo videos

I can't figure out why my Vimeo videos are not appearing in a Shadowbox. I have followed the steps I know to be the simplest, which involve copying the example directly from the github page and then updating the shadowbox paths to match the locations ...

Modifying the colors of my navigation links is not within my control

How can I modify my CSS to change the color of the navigation links to white? Below is the code snippet: <div class="col-sm-12"> <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header" ...

`Manipulating the image source attribute upon clicking`

I need help changing the attr: { src: ...} binding of an img element when a different image is clicked. Here is an example: $(document).ready(function () { var viewModel = { list: ko.observableArray(), showRenderTimes: ...

I'm facing an issue with my 'root' div on ReactJS - it doesn't stretch all the way to the top. Any suggestions on how I can make it expand properly

While I realize this issue has been discussed extensively, none of the suggested solutions have worked for me. As a final attempt to resolve it, I am turning to you all here. The point at which my root div stops is puzzling: I've explicitly defined ...

The default values for CSS filters

I have a keen interest in various CSS filters: blur brightness contrast grayscale hue-rotate invert opacity saturate sepia Could someone provide the default values for each filter (preferably as a % value, where applicable)? The MDN documentation does no ...

When an image is clicked, I would like it to redirect to a different page

In my slider image list, each image has an ID. If the ID becomes equal to a specific number, such as 24, I want that particular image to move to another page when clicked. Here is the code snippet: <?php $sql = "select * from ".$tblImages." Where cid= ...

Modify anchor link color in a one-page website by using Jquery to detect scroll position changes when reaching different page sections

Each if statement contains specific numbers to change the text color of the visited/current/active anchor link. The height of the page is dependent on its width. How can this be resolved? Apologies if my explanation is confusing, English isn't my stro ...

Using jQuery to make an element follow you as you scroll down a page inside a div

I've made some progress on my code: HTML <div id="header"></div> <div id="content"> <div class="sidebar-wrapper"></div> </div> <div class="session-wrapper"></div> <div id="footer"></div> ...

Preserving Scroll Location Through Back Button Usage and Ajax Manipulation of the Document Object Model

Currently, I am incorporating a feature where results are loaded in using ajax with an infinite scroll. However, there is an issue when a user clicks on an item in the list and navigates away from the page - upon returning by clicking the back button, they ...

Ways to select a single checkbox when other checkboxes are selected in JavaScript

var select_all = document.getElementById("select_all"); //reference to select all checkbox var checkboxes = document.getElementsByName("testc"); //retrieving checkbox items //function to select all checkboxes select_all.addEventListener("change", function ...

Looking to scan through a directory of .html files in Node.js to find specific element attributes?

Imagine trying to tackle this task - it's like reaching for a needle in a haystack. Picture a folder containing a static website, complete with images, stylesheets, and HTML files. My Node application needs to dive into this folder and extract only th ...

What steps can I take to troubleshoot and repair my accordion feature within an Angular project?

As a newcomer to Angular, I recently attempted to create an accordion component but encountered unexpected behavior. Here is the HTML code for my attempt: <div class="faq-item-container"> <h1 class="mt-1 mb-5"><strong>Frequently A ...