What could be causing the hover effect to malfunction in this code snippet?

I have been working on creating a hover effect for a list of items where an underline emerges from the center. While I have done something similar in the past, there seems to be an issue preventing it from working properly this time. I have included the HTML and CSS code below, could someone assist me in identifying the problem?

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

nav {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 10vh;
  font-family: 'Josefin Sans', sans-serif;
}

.logo {
  font-family: 'Abril Fatface', cursive;
  font-size: 2rem;
}

.logo a {
  text-decoration: none;
  color: #000;
}

.nav-links {
  display: flex;
  width: 40%;
  justify-content: space-around;
}

.nav-links li a {
  text-decoration: none;
}

.nav-links li {
  list-style: none;
  font-size: 25px;
}

.nav-links li a::after {
  content: '';
  display: block;
  height: 2px;
  color: #1ebbd7;
  margin: auto;
  display: none;
  transition: 0.5s;
}

.nav-links li a:hover::after {
  width: 80%;
}
<!--NAVBAR-->
<nav>
  <div class="logo">
    <a href="index.html">
      <h1>The Bakery.</h1>
    </a>
  </div>
  <ul class="nav-links">
    <li>
      <a href="#">About</a>
    </li>
    <li>
      <a href="#">Order</a>
    </li>
    <Li>
      <a href="#">Recipes</a>
    </Li>
  </ul>
</nav>

Answer №1

It seems you have both display:none and display:block on the :after element. This can cause confusion, as you only need display:block. For further clarification, check this article on display none/block

Additionally, you seem to have added color: instead of background-color: on the :after element. It's important to differentiate between these properties. You can learn more about their distinctions here

Lastly, make sure to specify an initial width on the :after element, such as width: 0%

Feel free to refer to the code snippet below for more clarity:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

nav {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 10vh;
  font-family: 'Josefin Sans', sans-serif;
}

.logo {
  font-family: 'Abril Fatface', cursive;
  font-size: 2rem;
}

.logo a {
  text-decoration: none;
  color: #000;
}

.nav-links {
  display: flex;
  width: 40%;
  justify-content: space-around;
}

.nav-links li a {
  text-decoration: none;
}

.nav-links li {
  list-style: none;
  font-size: 25px;
}

.nav-links li a::after {
  content: '';
  display: block;
  height: 2px;
  background-color: #1ebbd7;
  margin: auto;
  width:0%;

  transition: 0.5s;
}

.nav-links li a:hover::after {
  width: 80%;
}
 <nav>
   <div class="logo"><a href="index.html">
       <h1>The Bakery.</h1>
     </a></div>
   <ul class="nav-links">
     <li>
       <a href="#">About</a>
     </li>
     <li>
       <a href="#">Order</a>
     </li>
     <Li>
       <a href="#">Recipes</a>
     </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

The CSS class is not properly implemented in the React component

I value your time and assistance. I have spent many hours trying to solve this issue but seem unable to reach a resolution. Here is the React component in question: import styles from './style.scss'; class ButtonComponent extends React.Compone ...

Unable to use href attribute as intended

HTML: <a id="Switch">Click here to switch</a> <a href="image1_large.png" class="mainA blade"> <img id="mainImage" src="image1.png"/></a> Javascript: <script> $(function() { $('#Switch').click(functio ...

Issue with mouseout gap in Microsoft Edge when using the <select> element

A noticeable gap appears in Microsoft Edge between the <select> menu and its options when expanding the menu, as shown here: https://i.stack.imgur.com/SprJ2.png This particular issue can cause problems with the mouseout event due to inconsistent be ...

How to customize the page background color in Next JS

I am currently working on a project using Next Js and have encountered an issue. I have a global.css file set up in the project, but I need to change the background color of a specific page without affecting the rest of the project. Although I have tried u ...

Delivering HTML Email

I want to send an HTML email with images using Google's SMTP servers. Here is the HTML code with inline CSS that I have generated: <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type& ...

Tips for creating responsive text within a hero image

I'm exploring the world of creating responsive websites using Bootstrap, a new venture for me. One challenge I've encountered is trying to create a hero image with text positioned perfectly in the center. Despite my efforts, the text does not adj ...

Issue with activation of onClick event in case/switch statement

Currently working on a JavaScript project to recreate the Fallout terminal game, with one of the main aspects being comparing words selected by the user to those chosen by the computer. The concept of this hacking game is reminiscent of the board game Mas ...

AngularJS issue: Form submission does not trigger the controller function

I am currently learning AngularJS and I am experimenting with the sample code below to send my form data to the server. <!doctype html> <html lang=''> <head> <meta charset="utf-8"> <script src="../js/angular.min.v1 ...

Notification during image loading

I have successfully integrated the Nivo slider on my website, however, I am encountering a minor issue. Whenever the images are loading, it causes the footer to shift upwards which affects the overall look of the site. Is there a simple solution to add a m ...

My keyframes seem to be malfunctioning, despite exhausting all my troubleshooting options

Could someone please help me figure out why the @keyframes rule is not working for me? I've tried adding -webkit- as a fix, but it doesn't seem to be helping at all. .keyframe { height: 15px; width: 50px; background-color: red; -webkit ...

What is the best way to choose all text within a code box without highlighting the entire webpage?

I developed a CSS code specifically for my blogspot site to organize and store all of my code and technical snippets. .code { background:#dae6ef; background-repeat:no-repeat; border: solid #5C7B90; border-width: 1px 1px 1px 20px; color ...

Tips for enlarging an image element without causing the header to expand

I'm looking to enhance a logo image within a header element by expanding it on hover or creating a pulse effect. I thought of increasing the max-width and adding a transition for simplicity. However, my main concern is how to make only the img element ...

This issue with ng-include not functioning properly in Chrome but working fine in Firefox

My code is running only in Firefox and not working in Chrome. HTML Code <div ng-controller="myController1"> Select View: <select ng-model="employeeview"> <option value="1.html">1</option> < ...

How can I adjust padding values specifically for mobile devices within the Bulma CSS framework?

After constructing a website with the Bulma CSS framework, I encountered an issue with padding on mobile devices. To optimize the user experience, I need to reduce the padding on smaller screens to 5% or less instead of the initial 10%. However, I am unsur ...

Activating Jquery toggle on several elements instead of just the specified element

Hey everyone, I have a question regarding jQuery. I have a toggle function that is supposed to activate when clicking on a specific element - in this case, my logo image with the id of #logobutton. The toggle works great, but there's a problem. The an ...

"Communication + AngularJS + Social Networking - The Perfect Trio

I'm currently in the process of developing an Angular app that will eventually be compiled using PhoneGap for both Android and iOS platforms. While testing various plugins to incorporate Facebook integration (specifically for login and sharing), I enc ...

Maintain consistent theme across various pages using javascript

So I successfully implemented a dark mode on the front page using the following script (sourced from W3schools) : <script> function darklightmode() { var element = document.body; element.classList.toggle("dmode"); } </script> ...

What are some ways I can decrease the background width and shrink the div width?

I am hoping to maintain the current box size and borders, but I want to add a touch of color. Specifically, I would like to have a red line running through the center of the box without coloring the entire background. Although I know one way to achieve thi ...

Cease the use of the jQuery.css() method

Does anyone know how to halt the jQuery css() function? I've successfully used the jQuery.stop() function to pause the animate() function, but I'm unsure about stopping css(). Any help is appreciated. Thanks! ...

Retrieving Hyperlinks from JavaScript Object for Integration with D3-Created Table

Issue: I'm facing a problem where the HTML link extracted from an Associative Array is treated as a string instead of being applied as a clickable link in the browser. For a detailed visual example and code snippet, visit: http://example.com/code Da ...