CSS hover off effect not functioning as expected

I am currently working on designing navigation buttons for my portfolio website. The hovering effect is functioning perfectly, but I have encountered an issue where once the mouse moves away from the button, it quickly pops back to its original position instead of smoothly transitioning back.

Below is the code snippet that I am using:

li {
  list-style-type: none;
  margin: 20px 5px 0px 5px;
}
nav ul li a {
  display: block;
  margin: 30px 10px;
  text-decoration: none !important;
  text-align: center;
  width: 90px;
  padding: 6px 0;
  border-radius: 4px;
  -o-transition: all 300ms ease-in-out;
  -ms-transition: all 300ms ease-in-out;
  -webkit-transition: all 300ms ease-in-out;
  -moz-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
  background: #E6E5CC;
  float: right;
  top: 0px;
  left: 0px;
}
nav ul li a:hover {
  color: inherit;
  position: relative;
  top: 0px;
  right: 0;
  bottom: 0;
  left: 0px;
  top: -5px;
  left: -5px;
  box-shadow: 1px 1px 0px #e3e2c5, 2px 2px 0px #dfdebe, 3px 3px 0px #cbc995, 4px 4px 0px #b4b165, 5px 5px 0px #56542a;
  background: #f7f6ee;
}
nav ul li a:active {
  color: inherit;
  top: 5px;
  left: 5px;
  box-shadow: 0px 0px 5px white;
}
<nav>
  <ul>
    <li>
      <a href="#">Contact</a>
    </li>
    <li>
      <a href="#">Projects</a>
    </li>
    <li>
      <a href="#">Blog</a>
    </li>
    <li>
      <a href="#">About</a>
    </li>
  </ul>
</nav>

Answer №1

To achieve the desired effect, simply add position: relative; to your nav ul li a.

CSS

nav ul li a {
  position: relative;
}

li {
  list-style-type: none;
  margin: 20px 5px 0px 5px;
}
nav ul li a {
  display: block;
  margin: 30px 10px;
  text-decoration: none !important;
  text-align: center;
  width: 90px;
  padding: 6px 0;
  border-radius: 4px;
  -o-transition: all 300ms ease-in-out;
  -ms-transition: all 300ms ease-in-out;
  -webkit-transition: all 300ms ease-in-out;
  -moz-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
  background: #E6E5CC;
  float: right;
  top: 0px;
  left: 0px;
  position: relative;
}
nav ul li a:hover {
  color: inherit;
  position: relative;
  top: 0px;
  right: 0;
  bottom: 0;
  left: 0px;
  top: -5px;
  left: -5px;
  box-shadow: 1px 1px 0px #e3e2c5, 2px 2px 0px #dfdebe, 3px 3px 0px #cbc995, 4px 4px 0px #b4b165, 5px 5px 0px #56542a;
  background: #f7f6ee;
}
nav ul li a:active {
  color: inherit;
  top: 5px;
  left: 5px;
  box-shadow: 0px 0px 5px white;
}
<nav>
  <ul>
    <li>
      <a href="#">Contact</a>
    </li>
    <li>
      <a href="#">Projects</a>
    </li>
    <li>
      <a href="#">Blog</a>
    </li>
    <li>
      <a href="#">About</a>
    </li>
  </ul>
</nav>

JSFiddle

Answer №2

The position:relative statement was mistakenly placed in the :hover rule instead of the original state rule. In addition, there were redundant and overlapping positional values present.

li {
  list-style-type: none;
  margin: 20px 5px 0px 5px;
}
nav ul li a {
  display: block;
  margin: 30px 10px;
  text-decoration: none !important;
  text-align: center;
  width: 90px;
  padding: 6px 0;
  border-radius: 4px;
  -o-transition: all 300ms ease-in-out;
  -ms-transition: all 300ms ease-in-out;
  -webkit-transition: all 300ms ease-in-out;
  -moz-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
  background: #E6E5CC;
  float: right;
  position: relative; 
  top: 0px;
  left: 0px;
}
nav ul li a:hover {
  color: inherit;
  top: -5px;
  left: -5px;
  box-shadow: 1px 1px 0px #e3e2c5, 2px 2px 0px #dfdebe, 3px 3px 0px #cbc995, 4px 4px 0px #b4b165, 5px 5px 0px #56542a;
  background: #f7f6ee;
}
nav ul li a:active {
  color: inherit;
  top: 5px;
  left: 5px;
  box-shadow: 0px 0px 5px white;
}
<nav>
  <ul>
    <li>
      <a href="#">Contact</a>
    </li>
    <li>
      <a href="#">Projects</a>
    </li>
    <li>
      <a href="#">Blog</a>
    </li>
    <li>
      <a href="#">About</a>
    </li>
  </ul>
</nav>

Answer №3

Update the CSS for nav ul li a to include position: relative; as shown below:

nav ul li a {
  display: block;
  margin: 30px 10px;
  text-decoration: none !important;
  text-align: center;
  width: 90px;
  padding: 6px 0;
  border-radius: 4px;
  -o-transition: all 300ms ease-in-out;
  -ms-transition: all 300ms ease-in-out;
  -webkit-transition: all 300ms ease-in-out;
  -moz-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
  background: #E6E5CC;
  float: right;
  top: 0px;
  left: 0px;
  position: relative;
}

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

Instead of username, users are logging in using their email addresses. However, there is an issue where the system is

Having trouble logging in with email instead of username. My html page indicates that it does not work when trying to pass email as the login method. https://i.sstatic.net/6taJv.png In my views.py file, changing the authentication from username to email r ...

Show the contents directly within the Bootstrap table

I'm currently facing an issue with displaying monetary value information in a Bootstrap 4 table in responsive mode. Here's a breakdown of my problem: Here is the HTML code snippet I am using: <div class="table-responsive"> <table clas ...

Is it possible to achieve complete separation of concerns when it comes to HTML, CSS, and JS?

What is the best way to achieve separation of concerns between HTML, CSS, and JS using this HTML structure? <nav role="navigation" class="site-nav js-site-nav"> <ul class="site-nav__list"> <li class="site-nav__item"><a href="#" ...

Managing a new browser window using Selenium

I am attempting to automate a download process on a webpage using selenium. Upon clicking a button, a blank popup window appears. After approximately 60 seconds, a link appears within the popup that I want to click on. The code snippet below outlines my ap ...

Using Google Apps Script to invoke a JavaScript function from an HTML file

In my Apps script project, I am working on creating a Google chart that will appear in a modal above Google Sheets. I have ChartC.html: <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> ...

Navigating the web page and locating the appropriate Xpath element to extract

Here is the HTML that needs to be extracted: <input type="hidden" id="continueTo" name="continueTo" value="/oauth2/authz/?response_type=code&client_id=localoauth20&redirect_uri=http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callbac ...

What is the best way to decrease the font size of every element in the DOM?

Is there a way to decrease the size of all DOM elements by a specific amount? Currently, I am using Bootstrap styles where h5 is set at 14px and I need it to be 12px, and h1 is at 36px when I want it to be 34px, and so forth. I have considered two option ...

Having trouble navigating to the bottom of a VUEJS app?

I've been working on developing a chatbot app that utilizes a REST API to stream content. While the functionality of the app is running smoothly, I have encountered an issue with the scroll to bottom feature. Instead of automatically scrolling to disp ...

Prevent Click Event on Angular Mat-Button

One of the challenges I'm facing involves a column with buttons within a mat-table. These buttons need to be enabled or disabled based on a value, which is working as intended. However, a new issue arises when a user clicks on a disabled button, resul ...

Is it possible to enable caching for CSS and JavaScript files while browsing?

I'm curious to know if browser caching of CSS and JavaScript is done automatically with PHP or if we need to manually implement it through code. What are the benefits of manually caching CSS and JS files to the client's browser using code? Any ...

What is the best way to retrieve the elements stored within the 'this' object I am currently manipulating?

How can I access the elements nested within the 'this' that I am currently operating on? Below is the HTML code that I am currently working with: <div class="expander" id="edu">educational qualifications <ul class="list"&g ...

Managing several instances of NgbPagination on a single webpage

I am facing a challenge with having multiple NgbPagination components on a single page. For more information, please visit: Initially, I attempted to use ids but encountered an issue where changing one value in the first pagination affected both tables. ...

When the height of the first div increases, make sure the second div gets pushed down

Currently, I am working on creating accordion buttons that are wrapped within a div container. The goal is to keep them in a grid layout with 2 columns like the image below: https://i.sstatic.net/SmfYx.png However, when one of the accordion buttons is op ...

Using jQuery to toggle CSS properties

I'm having trouble switching between a CSS column layout and a normal layout for a div element with the ID 'article'. The jQuery script I wrote doesn't seem to work properly, as the entire thing disappears. Can anyone advise me on how t ...

What is the process for sending a single post to two separate actions?

I am working with HTML and Javascript code to send a form using method=post to an action="www.randomaction.com/randomaction.php". My goal is to also send the form data to my email (the same post), but without opening the user's mail client. Instead, ...

Unexpected behavior in Internet Explorer with ChartJS version 2.6.0

When using ChartJS v2.6 to create a pieChart within a Bootstrap Grid, everything appears correctly in Chrome and Firefox, but not in Internet Explorer. In IE, the chart is unintentionally stretched on larger screens. Bootstrap should handle the sizing, but ...

Is it not possible to check the server control checkbox in ASP.NET?

When attempting to implement a toggle button in my project, I encountered an issue where a server control checkbox cannot be checked. However, when using an HTML control, the checkbox can be checked. What could I be overlooking? <div class="material- ...

Box-sizing:border-box causing CSS padding problem in Firefox

Something strange is happening in Firefox, it seems like the debug console is not telling the truth or there's something I'm not seeing. Here's the CSS for the body tag: html, body { width: 100%; } body { padding: 5% 10% 0 10%; ...

`The Best Times to Utilize Various Image Formats Within a Single Webpage`

If I have two identical images displayed on a page at different sizes, what is the best option for optimizing performance? On one hand, using an image already sized correctly can improve performance, especially on mobile devices. On the other hand, using t ...

What could be causing some elements to not be generated while utilizing HTTrack or Save All Resources on an HTML webpage?

I'm interested in understanding the code and logic behind an online game called Paper.io. In order to accomplish this, I decided to save the entire webpage along with its resources on my computer and observe how each element reacts individually. My ...