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

Issue with Vuetify carousel not resizing to fit the entire width and height

I am looking to create a carousel that spans the entire width and height of a viewport in my Vue.js 2 project using Vuetify. Below is the code I have so far: <head> <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500, ...

Navigate to a specified div using JavaScript

I'm having an issue with my navigation bar not scrolling to the designated div. Despite looking at other examples, I can't seem to find a solution <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> ...

Tips for displaying specific information using Javascript depending on the input value of an HTML form

I am working on an HTML form that includes a dropdown list with four different names to choose from. window.onload = function(){ document.getElementById("submit").onclick = displaySelectedStudent; } function displaySelectedStu ...

Is there a way for me to establish a maximum word count for a paragraph?

When my paragraphs get lengthy, I only want the first 30 words to display on two lines. Is there a way to achieve this using HTML, CSS, or JS? The code snippet I tried did not work as expected. .long-text{ width: 70ch; overflow: hidden; white-sp ...

Using the Markdown attribute in this context prevents the translate pipe from translating the string

I have a paragraph within an Angular component that includes a markdown attribute. Occasionally, the markdown is causing the translation pipe to not translate the title.description string: <p class="someClass" markdown>{{tile.description | ...

What are alternative methods for creating a table layout without relying on traditional table elements?

Is there a way to eliminate tables from my code and achieve the same layout in the name of progress and learning? Here is an example of the table I currently have: <table cellspacing="0px"> <tr> <td> <img src= ...

What is the best way to retrieve data from within HTML tags using PL/SQL?

Within my Oracle PL SQL project, I am dealing with an html file that contains the following comment tag. I need to figure out how to retrieve the value between these comment tags. <comment label="Comments">Text_to_Extract</comment>&l ...

Using the concept of a while loop in PHP, you can easily retrieve the values of Radio Buttons on one page and

Is there a way to retrieve and store radio button values in a MySQL database using PHP looping concepts? $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_row($result)) { ?> ...

Why is my DIV not registering clicks when I try to interact with it?

Trying to implement a Javascript click event <script type="text/javascript"> $(document).ready(function () { $(".role").click(function () { alert("idiot"); }); }); </script> Here is the CSS code for styling the DIV with clas ...

Is it possible to conduct a feature test to verify the limitations of HTML5 audio on

Is there a way to detect if volume control of HTML5 audio elements is possible on iOS devices? I want to remove UI elements related to the volume if it's not alterable. After referring to: http://developer.apple.com/library/safari/#documentation/Aud ...

Discovering the name of an object property by locating its corresponding id

I am working with a basic JSON data structure where I need to retrieve the name from an object by comparing its ID. For example, if I have the number 2, I need to check if it matches any object's ID. If the ID is equal to 2, then I must fetch the corr ...

Display the chosen alternative in a Bootstrap dropdown menu

I am currently facing an issue with the dropdown list in my bootstrap menu. <li class="dropdown"> <a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">Chose option<span class="c ...

eliminate gaps between hyperlinks using cascading style sheets

I developed a webapp for iOS which has a specific page containing a centered div with 3 buttons. Inside this div, there is a total of 460px with each link measuring as follows: the first and last are 153px while the middle one is 154px. The main issue ar ...

Creating CSS-in-JS Components in React JS without External Stylesheet interference

import React, { Component } from "react"; import './navbar.css' class NavBar extends Component { render() { return ( <div> navbar content </div> ); } } export default NavBar; If I were to ...

What is the reason for the index.html file not connecting to the local .css files and .js file?

I'm currently using node.js to send an .html file that includes the following tags: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="description" content="This is my personal profile website" /> <link r ...

Center the image and align the floated texts on each side horizontally

I've recently started learning about HTML and CSS, but I'm having trouble grasping one concept. My goal is to align two floating <p> elements on either side of a centered <img>. Here's an example of what I'm trying to achiev ...

Preserve proportions while resizing the browser window

I needed my code to ensure that the images keep their aspect ratio regardless of the screen size or if the user resizes their browser instead of viewing it full screen. I have some code in place, but I'm unsure how to maintain the current effects of ...

Is there a way to tally up the number of green items and display a <p> tag when every item has been marked green?

I have created a checklist that includes checkboxes. I am looking to display some text when all the checkboxes are checked and green. Can someone assist me with writing the code for this functionality? $(document).ready(function() { $("i").click(funct ...

How can I showcase the Junit HTML report in Karate DSL, as outlined in the official Karate documentation?

After following the Karate Doc and pasting the Junit HTML Report into my browser, I was surprised to see that the output was in plain text and did not look anything like what was described in the tutorial video. I attempted to view it on both Firefox 57.0. ...

What is the best way to create a 3x3 grid layout with 9 input text boxes that do not have a double border thickness?

I am having trouble creating an input square with 3 rows and 3 columns. The borders between each two input boxes are overlapping, resulting in a double thickness border. To see this issue in action, you can run the provided html. How can I fix this so that ...