Interactive side navigation with hover effects

Having trouble with my side navigation's functionality.

Expected behavior

  1. Show icons only when not hovered over.

  2. On hover, reveal the link one letter at a time until fully visible.

  3. When the hover state is inactive, make the link disappear gradually.

I tried using the overflow-x property but it didn't produce the desired result.

Current issue

The link appears under the icon until the navigation width expands enough to fit both on the same line.

Upon deactivating the hover state, the link instantly disappears.

nav {
  background-color: #f5f5f5;
}

nav a {
  text-decoration: none;
  color: black;
}

.side-nav {
  position: fixed;
  top: 54px;
  left: 0;
  bottom: 0;
  width: 5%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: width 2s;
}

.side-nav>.side-nav-top {
  display: flex;
  flex-direction: column;
}

.side-nav>.side-nav-top>a>span:first-child {
  display: inline-block;
  margin: 5px 20px 5px 20px;
  width: 20px;
}

.side-nav i {
  color: #909090;
}

.side-nav:hover {
  width: 40%;
}

.hide {
  overflow-x: hidden;
  display: none;
}

.side-nav:hover .hide {
  display: inline;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8><
  <title>Home</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.dev.js"></script>
  <link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
  <link rel="stylesheet" href="/css/index.css">
</head>

<body>
  <nav class="side-nav">
    <div class="side-nav-top">
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
    </div>
  </nav>
</body>

</html>

Answer №1

Transform

.side-nav > .side-nav-top > a
into a flexbox layout (to avoid the link text wrapping below the icon) and apply white-space: nowrap to prevent text wrapping of the link text.

Instead of toggling the display of the menu text, you can incorporate overflow: hidden in the main side-nav element - refer to the demo below:

nav {
  background-color: #f5f5f5;
}
nav a {
  text-decoration: none;
  color: black;
}
.side-nav {
  position: fixed;
  top: 54px;
  left: 0;
  bottom: 0;
  width: 3.5rem; /* using percentage may not be ideal here */
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: width 2s;
  overflow: hidden; /* added overflow property here*/
} 
.side-nav > .side-nav-top > a { /* converted into flexbox layout */
  display: flex;
  align-items: center; /* vertically aligns the icon and link text */
  white-space: nowrap; /* prevents text wrapping */
}
.side-nav>.side-nav-top {
  display: flex;
  flex-direction: column;
}
.side-nav>.side-nav-top>a>span:first-child {
  margin: 5px 20px 5px 20px;
}
.side-nav i {
  color: #909090;
}
.side-nav:hover {
  width: 40%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.dev.js"></script>
  <link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
  
  <nav class="side-nav">
    <div class="side-nav-top">
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
     <!-- more anchor tags with font awesome icons and hidden text -->
    </div>
  </nav>

Answer №2

If you're looking for a solution, try implementing the following suggestions:

Include overflow: hidden in your entire .side-nav section and disable text wrapping for a (or use flex with min-width).

nav {
  background-color: #f5f5f5;
}

nav a {
  text-decoration: none;
  color: black;
}

.side-nav {
  position: fixed;
  top: 54px;
  left: 0;
  bottom: 0;
  width: 5%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: width 2s;
  overflow: hidden;
}

.side-nav>.side-nav-top {
  display: flex;
  flex-direction: column;
}

.side-nav a {
  white-space: nowrap;
}

.side-nav>.side-nav-top>a>span:first-child {
  display: inline-block;
  margin: 5px 20px 5px 20px;
  width: 20px;
}

.side-nav i {
  color: #909090;
}

.side-nav:hover {
  width: 40%;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Home</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.dev.js"></script>
  <link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
  <link rel="stylesheet" href="/css/index.css">
</head>

<body>
  <nav class="side-nav">
    <div class="side-nav-top">
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
      <a href="#"><span><i class="fas fa-home"></i></span><span class="hide">The Home page</span></a>
    </div>
  </nav>
</body>

</html>

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

Combining :not() and :first-child in element selection is not supported

While working on CSS, I encountered an issue. I am using a combination of :not(.class) selector and :first-child selector. Individually they work fine. The row that should be red is not turning red. Here is the code snippet: table tr td { background: ...

Tips for inserting an HTML table into a div element using JQuery/JavaScript

I currently have a navigation bar with only two items. While I may potentially add more items in the future, for now I am content with just these two. Upon opening the page, my goal is to display the content of the first item in the navigation bar. Each it ...

The issue of the window not closing after the parent page is refreshed

Working on a basic html/php/js project for internal use, I encountered a particular issue that has stumped me. Within the parent window, there is a JS function responsible for opening and closing a popup: function openit(){ boersenfenster = window.open ...

Replicating Bootstrap's Website Navbar Using Angular

1. The Challenge I've been attempting to replicate the Navbar from Bootstrap's website, but unfortunately, I have not been successful. Even though I have applied the same classes, I am still facing issues. I am working with Angular, and this has ...

What is the best way to arrange 4 divs in a row?

Looking to align these divs horizontally, I attempted to wrap them in a div and use display:inline-block; without success. I'm trying to understand positioning and display better. Any assistance would be greatly appreciated. Here is the HTML: <di ...

What is the method to display just the final 3 characters within a paragraph?

Is there a way to display only the last three characters of a string using either a method or a string method? I consistently have a 13-digit number, but I specifically require showing only the final three digits. Appreciate any assistance provided. Than ...

Issues with updating web pages on Google Chrome

Having an issue with Google Chrome not updating the new CSS on my website without clearing the cache every time. It's a concern because we can't expect visitors to constantly clear their cache when visiting. Does anyone have a solution for ensuri ...

Identifying mouse proximity through processing.js

I am using processing.js for coding. I am trying to dynamically adjust the size variable so that it increases as the cursor approaches the ellipse and decreases as the cursor moves away from it. I also want to set a limit for the size, preferably between 5 ...

Angular form is not displaying the correct selected value

My update form seems to be showing the wrong selected value, even though there is a value with the selected attribute set to true. Can someone help me figure out what's going on? <div class="form-group col-md-6"> <label for=" ...

Animate.css plugin allows for owl-carousel to smoothly transition sliding from the left side to the right side

I have a question regarding the usage of two plugins on my website. The first plugin is Owl Carousel 2, and the second one is Animate.css. While using `animateIn: 'slideInLeft'` with Owl Carousel 2 is working fine, I am facing an issue with `ani ...

What's the best way to ensure that Bootstrap columns have consistent height with space in between?

Looking to create three columns of equal height in Bootstrap, with no gaps between them? Check out this screenshot for reference: Screenshot Below is the code snippet you can use: <div class="container"> <div class="row mt-4&qu ...

Acquiring data from a website using Python post user engagement

Hey everyone! One of my friends has a lot of typing to do for her IT classes in school. She needs to learn how to type quickly on the keyboard. Being quite lazy, she asked me if I knew of any way she could type her texts on without actually doing any wor ...

Stop the div from automatically scrolling to the top when the home page is refreshed

As a beginner in javascript, I am currently working on my project. I have two pages set up: home.php and data.php. In home.php, here is the code that retrieves data from data.php and refreshes it every 3 seconds: <div id="show"></div> ...

Generating random images using Javascript

I am facing some challenges while creating my first custom JavaScript function. My goal is to display three random thumbnails on my webpage by selecting images from a pool of options. However, I am encountering issues with duplicated images and handling s ...

I find it confusing how certain styles are applied, while others are not

Working on my portfolio website and almost done, but running into issues with Tailwind CSS. Applied styling works mostly, but some disappear at certain breakpoints without explanation. It's mainly affecting overflow effects, hover states, and list sty ...

What methods are available to incorporate arithmetic when defining a style attribute?

Is there a way to achieve arithmetic operations in CSS, like calculating margin-left: -60px + 50% of the width of the parent div? I'm eager to find a solution, whether it involves JavaScript or any other method. Any help would be greatly appreciated. ...

Problems with IE8 CSS display across various Windows machines

My website is displaying differently on various machines that have the same setup... I've tested the site on 6 different machines, all running IE8 (8.0.7600) on Windows 7, and I'm getting two different renderings of the site in Internet Explorer ...

Why is there a blank white space under my image in Safari?

I am struggling to understand why Safari is adding a white space at the bottom of the image on my current website project. Interestingly, Firefox and Chrome display it correctly. The image should stay fixed at the bottom of the page even when resized. He ...

Prevent the bootstrap header in a table from scrolling by enclosing it in

I am encountering an issue with fixing the headers of multiple tables in a page within their dedicated divs. Despite trying various methods, I haven't been successful as the fixed header overflows the div. I want to confine it within the div. Additio ...

Arrow pointing right with a see-through appearance beside the image

I need help placing a transparent arrow on the right side of an image, vertically centered and displaying the background image. After researching solutions like this one, I found this codepen which almost achieves what I want. However, I am struggling to ...