transition effect on div inside a link element upon hovering

My navigation menu currently looks like this:

  <nav>
    <ul>
      <li>
        <a href="#">About</a>
        <div style="background-color: #c03546;height: 10px;"></div>
      </li>
      <li>
        <a href="#">Companies</a>
        <div style="background-color: #f26d5b;height: 10px;"></div>
      </li>
      <li>
        <a href="#">Brands</a>
        <div style="background-color: #f6ea8c;height: 10px;"></div>
      </li>
    </ul>
  </nav

I am looking to use only CSS to create a hover effect where the corresponding div for each link appears gradually upon hovering (the divs are initially hidden). However, I am facing issues with adding a transition duration.

Currently, I have managed to make the divs appear on hover but I am struggling to incorporate the transition duration into the effect.

  nav {
    position: fixed;
    background-color: #fafafa;
    top: 0;
    width: 100%;
    border-bottom: 1px solid #cacaca;
  }

  nav li {
    display: inline-block;
    padding: 0 1em;
  }

  nav li a {
    color: #939393;
    font-weight: 400;
    transition-duration: 3s;
  }

  nav li div{
    display: none;
    transition-duration: 3s;
  }

  nav li a:hover +div{
    display: block;
  }

Answer №1

Animating the display property is not supported.

However, you can achieve a similar effect using the opacity property:

nav {
  position: fixed;
  background-color: #fafafa;
  top: 0;
  width: 100%;
  border-bottom: 1px solid #cacaca;
}

nav li {
  display: inline-block;
  padding: 0 1em;
}

nav li a {
  color: #939393;
  font-weight: 400;
  transition-duration: 3s;
}

nav li div{
  opacity: 0;
  transition-duration: 3s;
}

nav li a:hover +div{
  opacity: 1;
}
 <nav>
    <ul>
      <li>
        <a href="#">About</a>
        <div style="background-color: #c03546;height: 10px;"></div>
      </li>
      <li>
        <a href="#">Companies</a>
        <div style="background-color: #f26d5b;height: 10px;"></div>
      </li>
      <li>
        <a href="#">Brands</a>
        <div style="background-color: #f6ea8c;height: 10px;"></div>
      </li>
    </ul>
</nav>

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

What is the best way to horizontally align my menu items within a Bootstrap dropdown?

I am trying to modify a bootstrap nav menu so that the menu items become center aligned in the dropdown menu when the screen size is below 768px. Currently, the menu items are either staying floated or right aligned, and I can't seem to get them cente ...

An easy guide on including social media icons in a Bootstrap navbar

I am attempting to include Twitter, Google+, and Facebook buttons in a navigation header to have them appear on the right side of the top right corner of the header, positioned above the navigation menu. My current framework is Bootstrap. I experimented ...

To close the menu, simply tap on anywhere on the screen

Is there a way to modify a script so that it closes the menu when clicking on any part of the screen, not just on an 'li' element? $(function() { $('.drop-down-input').click(function() { $('.drop-down-input.selected').rem ...

The <a> tag behaving uniquely when encountering the <div> tag

Apologies for the vague title, I struggled to find the right words to express my issue. I have been working on creating a web page using an HTML document. Initially, I used <div> tags to structure the page and everything seemed to be in order. Howev ...

Rapid jQuery Hover Bug

I am facing a problem with my sidebar. The issue occurs when I hover over the sidebar slowly, it works correctly. However, when I move the mouse quickly on it, everything gets messed up. How can I resolve this? This is the HTML code: <div class="sideb ...

Hover effect not activating on image within modal

only img:hover is working, but after adding this to the CSS: #user-profile #user-myModal .modal-body img:hover #user-profile #user-myModal .modal-body .change-pic i{ display: block; } it's not working. I changed the class and id names, tried eve ...

Is there a potential security threat in using the same names for your form fields as those in mysql?

Are there any considerations to take into account when deciding whether or not to name your form fields the same as the HTML fields? <input type="text" name="my_field_1" id="my_field_1" /> --> mysql row my_field_1 or <input type="text" name= ...

Attempting to remove a specific row from a table by targeting the value of a cell with Jquery

I need help with deleting specific rows in a table using jQuery. Each row has a checkbox as its first cell, and I want to delete only the rows that have their checkboxes checked when I click the delete button. The code snippet below is what I have been att ...

Incorporating a Bootstrap input group within a <td> element of a table

Encountering an issue when attempting to include a bootstrap input-group-addon with the input field nested inside a <td> tag within a table. Once all necessary classes are applied, there appears to be some spacing between the sign and the input field ...

Experience the Firefox nightmare with a fixed background-attachment!

I've been struggling with a puzzling issue all day, and I'm hoping that someone with more expertise can help me solve it. As I work on updating the design of my website, I've encountered what seems to be a bug specific to Firefox. I've ...

How can one HTML form be used to request a unique identifier from the user, retrieve a record from the database, parse it into JSON format, and then populate an HTML page form using the

As someone who isn't an expert in any specific coding language, I have a knack for piecing things together to make them work. However, my current challenge is stretching my technical abilities a bit too far. Here's what I'm trying to achieve ...

Tips for retrieving the value of a selected option within a table row using my jQuery function

When utilizing the select option within a datatable, I encountered an issue where I needed to retrieve the value of the selected option from each row of the table. The problem was that since the select "id" remained constant, it only returned the same valu ...

Positioning Elements at the Bottom with Bootstrap 5

Here are the codes you requested: <div class="d-flex"> <h1>Hello World</h1> <p class="ms-auto small">Stupid Text</p> </div> <hr class="my-0"> I am attempting to vertically ali ...

Is it possible to use HTML text as a CSS selector with JavaScript?

I've been searching for a solution to this issue but haven't found anything that works for me. Check out this code on jsfiddle. Person1 and Person2 both have class="from" and the CSS selector is .from so it applies to both. However, I want it ...

Update the color of the menu ID

Feeling frustrated trying to figure out how to modify this <nav class="mainnav "> <a id="top"/> <div class="navwrap"> <ul id="menu-top" class="menu"> <li id="menu-item-70" class="menu-item menu-item-type-custom menu-item-object- ...

What is the optimal method for delivering HTML content in a Node.js application using Express.js?

My current approach involves serving all HTML content directly from my app.js/server.js file, as shown below: app.get('/', function(req, res) { res.render('index.html'); }); app.get('/about', function(req, res) { res. ...

Issue with custom font display malfunction

I'm having difficulty getting my custom @font-face to function properly. When I apply this class style to a <p>, it always defaults to Arial. Can anyone point out what might be going wrong here? <style> .stonefont @font-face { font-fa ...

What steps can I take to address the issue with the jquery dropdown?

I am struggling with implementing JavaScript functionality on my website. Specifically, I have a dropdown menu in the hamburger menu that is causing some issues. Whenever I click on the services option, the dropdown opens but quickly closes again. I'v ...

Step-by-step guide on using variables to apply CSS

In our table consisting of 6 rows and 2 columns, the second column displays a Font Awesome circle followed by various values in the database field (such as Outstanding, Very Good, Good, Needs Improvement, and Adequate). I am looking to dynamically change ...

Utilizing fab-icons with CDN in Next.js version 13.4

Currently, I am working with the latest version of Next.js (13.4) and I would like to incorporate a single Icon into my project using Font Awesome CDN to avoid increasing the overall app size. However, when I include the following code snippet in my layou ...