Tips on updating CSS styles for navigation bar links in real time

Currently, my homepage displays a CSS content hover effect when the user interacts with the icons. However, I am facing an issue where all icons show the same text "home" on hover. How can I customize the text output for each individual icon/link? Your help is much appreciated.

Navbar.html:

<nav class="navbar">
    <ul class="navbar-nav">
        <li class="logo">
            <img src="{% static 'portfolio/logo.png' %}" alt="">   
        </li>
        <li class="nav-item">
            <a href="" class="nav-link">
                <i class="fas fa-home "></i>
            </a>
        </li>
        <li class="nav-item">
            <a href="" class="nav-link">
                <i class="fas fa-cog"></i>
            </a>
        </li>
        <li class="nav-item">
            <a href="" class="nav-link">
                <i class="fas fa-book"></i>
            </a>
        </li>
        <li class="nav-item">
            <a href="" class="nav-link">
                <i class="fas fa-tasks"></i>
            </a>
        </li>
        <li class="nav-item">
            <a href="" class="nav-link">
                <i  class="fas fa-phone"></i>
            </a>
        </li>
    </ul>
</nav>

_sidebar.scss:

.navbar{
    position:fixed;
    background-color: $navbg;
}

.navbar-nav{
    list-style: none;
    padding: 0;
    margin: 0;
    display:flex;
    flex-direction: column;
    align-items:center;
    height: 100%;
}

.nav-item{
    width:100%;
    &:last-child {
        margin-top: auto;
    }
}

.nav-link{
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 5rem;
    font-size: 2rem;
    padding:1.5rem;
    text-decoration: none;

    i{
        font-size: 3rem;
        color: $navicon;    
        &:hover{
            color:$icon-hover;
        }    
    }

    &:hover::after{
        content : "Home";
        position: absolute;
        color:#12181b;
        background: #b2becd;
        right:-7rem;
        border-radius: 0.5rem;
        padding:0.5rem;            
        margin-left:2rem;
    }
}

.logo{
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 2rem;
    margin-bottom: 2.5rem;
    width: 100%;
    img{
        width:60%;
    }    
}

Answer №1

When adding dynamic contents, using the content property is not the correct approach. Instead, one should add another element, keep it hidden by default, and show it when the user hovers over its parent.

By following this method, you can include proper content for each icon dynamically or statically. The code snippet below illustrates this concept:

.navbar {
  position: fixed;
  background-color: black;
}

/* CSS styling omitted for brevity */

.logo img {
  width: 60%;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<nav class="navbar">
  <ul class="navbar-nav">
    <li class="logo"><img src="{% static 'portfolio/logo.png' %}" alt="">

    </li>
    <li class="nav-item">
      <a href="" class="nav-link"><i class="fas fa-home "></i><span class="label">First Icon</span></a></li>
    
    /* Additional list items with icons and labels omitted for brevity */
  
  </ul>
</nav>

Answer №2

You can incorporate data attributes in your HTML markup and easily reference them in your CSS for various content values.

  // HTML

    <li class="nav-item">
          <a href="" class="nav-link" data-title="home">
            <i class="fas fa-home"></i>
          </a>
     </li>
     <li class="nav-item">
          <a href="" class="nav-link" data-title="setting">
            <i class="fas fa-cog"></i>
          </a>
     </li>
     ...
// CSS

.nav-link:hover::after {
  content: attr(data-title); // added
  position: absolute;
  color: #12181b;
  background: #b2becd;
  right: -7rem;
  border-radius: 0.5rem;
  padding: 0.5rem;
  margin-left: 2rem;
}

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

displaying a confirmation alert upon unchecking a checkbox using javascript

I am trying to implement a feature where a message saying "are you sure?" pops up when the user tries to uncheck a checkbox. If they choose "yes", then the checkbox should be unchecked, and if they choose "no" it should remain checked. I'm new to Java ...

What is the best way to create a 6-column grid system without using bootstrap?

I am looking to create a row with 2 columns that should turn into 2 separate rows, each containing one column when viewed on smaller devices. I have researched CSS Grid but find it quite complex. Can someone simplify it for me? Thank you! ...

What is the process for switching directories and renaming a file when uploading in nodeJs?

I am currently using multer and fs to handle the upload of an image file. How can I modify the directory where uploaded files are stored? Currently, all files are saved in my "routes" folder instead of the "uploads" folder created by multer. Additionally, ...

The dropdown menu vanishes from sight as soon as the cursor moves away from a link

Recently, I encountered an issue while trying to create a dropdown menu using Jquery. The problem arose when attempting to select the second link, as the entire menu would disappear. Additionally, is there a way to ensure that only one dropdown menu is vis ...

Verify login availability using Javascript auto-check

For quite some time now, I've been grappling with a significant issue that has been consuming my attention. I am determined to implement an auto-check login availability feature in the registration form of my website. My goal is for it to verify the ...

When hovering over individual links within a set of blocks, ensure that the other links are not affected or disrupted

I need help creating a forum category link guide. Similar to this example... Specifically, I want it to display as Forums > The Financial Roadmap Two issues I am facing are: when hovering over the link, it shifts down one row and how can I add blocks lik ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

Is it possible to globally modify the component reference <dropdown-component> name in Angular during runtime in a dynamic manner?

I am currently working on an application that utilizes a component called "dropdown-component" throughout its pages. However, due to specific business requirements, I have been tasked with replacing "dropdown-component" with "custom-dropdown-component". Un ...

No information available at the moment

When the data is not present, it displays as "display none"... However, I want it to show "no data found" This is the current code if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = ""; } else { li[i].styl ...

I'm confused why this code is functioning in JSFiddle but not on my HTML webpage

For the first time, I am encountering this issue. I am currently attempting to integrate this code into my application http://jsfiddle.net/TC6Gr/119/ My attempts include: Pasting all the jsfiddle code in a new page without my code, but it doesn't w ...

Discover the method for measuring the size of a dynamically generated list

I'm currently working on a chat box that displays messages as a list. One issue I'm facing is that when I click on the start chat button, the chat box opens but the length of the list always remains zero. How can I resolve this problem? $(docu ...

Which specific CSS attributes should be applied to create a scroll bar within this table?

Below is the table that I am working with: 'table' I want to ensure that its width remains the same even when the screen size is reduced, and only add a scroll bar to view it. I have already included overflow: scroll; in the container <div> ...

Displaying content conditionally - reveal a div based on user selection

I have a dropdown menu and need to determine whether to display a specific div using the value selected via v-if. <select class="custom-select custom-select-sm" id="lang"> <option value="1">English</option> <option value="2">Sv ...

Requesting an API token through the body using Javascript's Fetch function

I'm currently working on developing a frontend application using Javascript Fetch to interact with an API service. One of the tasks I need to accomplish is to create a token by using the POST method and sending an apiKey parameter in the Body. Once I ...

How come the mouseover effect on the div remains even after the mouse has been removed?

How can I keep the original CSS class when the mouse moves away? function highlight( x, y) { var sel=document.getElementById(y); sel.style.borderBottom= "2px solid "+x; sel.style.opacity="1"; sel.style.transition="all eas ...

Styling with CSS: Enhancing list items with separators

Is there a way to include a separator image in my list using CSS? li { list-style: none; background-image: url("SlicingImage/button_unselect.png"); height: 53px; width: 180px; } This is the code for the separator image: url("SlicingImage ...

Determine if offcanvas element is visible upon initialization in Bootstrap 5 Offcanvas

I have a search-filter offcanvas div for location and property type on this webpage: On larger screens (desktop), the offcanvas is always visible, but on smaller screens (mobile), it is hidden and can be toggled with a button. This functionality works per ...

Using a series of identical divs to dynamically update the image URL

Greetings! I am a newcomer to the world of web development and I have decided to hone my skills by creating a small website for my mother! My goal is to replicate a specific div multiple times while changing only the image URL and the heading caption. < ...

Issue displaying content in a two-column setup on Chrome and Mozilla browsers due to ASP problem

I've encountered an issue with a footer appearing incorrectly in Chrome and Mozilla browsers when using a 2-column layout, although it displays fine in IE8. Currently, I'm coding in asp with css includes to render the footer. Here's the CSS ...

My CSS nesting seems to be posing a problem - any idea what

Something unexpected happened while I was working on a project with others. Last night, some files were edited and when I updated my subversion today, the nested divs seemed to have lost their hierarchy. I have been trying all day to fix this issue but wit ...