What are some creative ways to animate a highlight effect on a CSS

I'm struggling to implement a sliding underline effect for my top navigation bar on hover. I've tried using transitions and keyframes, but so far it's not working as expected.

My current attempt only triggers the effect on the parent element, and the underline translates in an undesired manner (500px to the right).

Any guidance or assistance would be greatly appreciated!

#header {
position: fixed;
background-color: rgba(255, 255, 255, 0.7);
width: 100%;
top: 0px;
left: 0px;
z-index: 1;
text-align: center;
padding: 15px;
}

.nav ul li {
font-family: 'Exo', sans-serif;
text-transform: uppercase;
list-style-type: none;
display: inline;
padding: 11px;
}

.slider {
position: absolute;
bottom: 0px;
left: -50px;
width: 50px;
height: 5px;
background-color: rgba(52, 152, 219, 0.3);
transition-property: transform;
transition-duration: 300ms;
transition-timing-function: ease-in-out;
transition-delay: 0s;
}

.nav:hover .slider {
transform: translate(500px);
}

.nav ul li a {
text-decoration: none;
margin: 0px 30px 0px 30px;
color: rgba(0, 0, 0, 0.5);
<header id="header">
    <nav class="nav">
    <ul class="list">
              <div class="slider"></div>
    <li class="btn"><a href="#home">Home</a></li>
    <li class="btn"><a href="#about">About</a></li>
    <li class="btn"><a href="#portfolio">Portfolio</a></li>
    <li class="btn"><a href="#contact">Contact</a></li>
    </ul>
    </nav>
      </header>

Answer №1

Perhaps you could consider implementing something along these lines:

$('li').on('click', function() {
  $('.current').removeClass('current');
  $(this).addClass('current');
}).has("a[href*='" + location.pathname + "']").addClass("current");
  .nav {
    width: 50%;
    margin: 0 auto;
    font-family: 'Exo', sans-serif;
    text-transform: uppercase;
    list-style-type: none;
    display: inline;
    padding: 11px;
  }

ul li {
    display: inline;
    text-align: center;
  }

  a {
  display: inline-block;
  width: 25%;
  margin: 0;
  text-decoration: none;
  color: black;
 }

 .home.current ~ hr,
   ul li.home:hover ~ hr {
   margin-left: 0%;
 }
   
  .about.current ~ hr,
   li.about:hover ~ hr {
      margin-left: 25%;
   }
  
  .portfolio.current ~ hr,
   li.portfolio:hover ~ hr {
    margin-left: 50%;
   }

  .contact.current ~ hr,
   li.contact:hover ~ hr {
   margin-left: 75%;
   }

hr {
 height: 3px;
 width: 25%;
 margin: 0;
 background-color: rgba(52, 152, 219, 0.3);
 transition-property: transform;
 transition: .3s ease-in-out;
   }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<header id="header">
  <div class="nav">
    <ul>
      <li class="home"><a href="#home">Home</a></li><!--
   --><li class="about"><a href="#about">About</a></li><!--
   --><li class="portfolio"><a href="#portfolio">Portfolio</a></li><!--
   --><li class="contact"><a href="#contact">Contact</a></li>
     <hr />
    </ul>
 </div>
</header>

Alternatively, are you interested in having the underline extend from the side of the screen?

UPDATE: Included js/jQuery code to ensure menu items remain highlighted upon clicking.

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

Tips on customizing default Polymer CSS

Currently, the core-toolbar features an indent class that shifts the title to the right by 60px. However, material design guidelines suggest that the margin should actually be 80px (oddly enough, the number 60 is not mentioned in the documentation). I cou ...

Reacting to each change event, Angular dynamically loads new autocomplete options

I am facing an issue with my form where users need to select a company using mat-select-search. Upon selection, an API call is made with the selected company ID to fetch users from that company for the autocomplete feature in recipient fields. The process ...

Double tap on the YouTube video

Recently, I added a YouTube video to my project. However, I am encountering an issue where when I double click on the video, it opens in a new tab. What I actually want is for the video to open in full screen when clicked. Appreciate any help! ...

Display a Popup at 5PM without the need for a page refresh, updating in real-time

After searching extensively online, I was unable to find a suitable solution for my issue. What I am aiming for is to have a popup appear on my page every day at 5:00 PM without the need to refresh the page. If I happen to access the page before 5:00 PM an ...

Designing HTML columns to adapt to different screen resolutions

Looking for a clever jQuery or CSS trick to dynamically adjust the number of columns in a website layout based on the size of the viewport. For instance, an iPhone display would have 1 column, while an 800x600 screen would have 2, and a 1024x768 screen wou ...

Show the comment section only if the initial radio button is selected

I am struggling to implement a comment section that is displayed when the first radio button is checked. Here's the code I have tried: I attempted the following to show the section and test the associated script: <div id="commentaryDiv" ...

Property registered in CSS dynamically

Is it possible to pass dynamic values when using the css registered property? For instance, in the code snippet below, how can I pass on the value ---numValue=35 to to{--num:}? @property --num { syntax: "<integer>"; initial-value: 0; ...

Issue with input width percentage not functioning as expected

Is there a special method to specify the width of an input field in CSS without it extending beyond the container? I want my input field to be 98% of the container's width, but when I set it to that, it goes off the screen. This is the HTML code I am ...

Retrieving the `top` value using `$this.css("top") can either return an object or an element value

Something odd is happening with my HTML object: <div data-x="1" data-y="1" class="tile empty" style="top: 32px; left: 434px;"> <div class="inner">1:1</div> </div> When attempting to access its top property in jQuery using the ...

What is the best way to align my SVG to display inline with text?

I am trying to add an SVG image next to some text in a navbar, but I'm running into some issues. The SVG is overflowing from the navbar instead of aligning inline with the text. Here's a snippet of my HTML code: ...

Restrict Bootstrap Column Width to Container Boundary

I am struggling to create a two-row hierarchy display for a set of photos using bootstrap 5. Despite my efforts, the images overflow out of the container instead of staying within it. My goal is to make the container fill the screen and act as a cover disp ...

No change in the element's text content when clicking

I'm working on creating a timer that counts down from either 15 or 30 seconds. However, I'm having trouble changing the text value when the 15 button is clicked. Can someone help me figure out what's wrong? Thank you in advance HTML <h ...

Is it just me, or does `position: fixed;` not actually fix the element to the top?

I'm experiencing an issue where the element isn't fixed to the top of the page, despite using the position: fixed; property. I've tried several solutions including: Setting a z-index of 9999 Wrapping all elements in a single container (.fi ...

Tips for separating the 'title' and 'icon' elements within Bootstrap panels?

I am currently working on integrating a FAQ section into my website (not yet live, so I cannot provide a link). However, I am facing some minor CSS issues that I am struggling to resolve. The problem lies in a panel that is meant to display as shown below: ...

A guide on incorporating an event to multiple elements in vue.js

I am trying to implement a double-click event on multiple elements in Vue3. My goal is to create a table of contents data and add a double-click event to the checkboxes and favorite icons without using @dblclick="". I find it more convenient to assign a c ...

If I dared to eliminate the emphasized line, this code would completely fall apart

<!DOCTYPE html> <html> <head> </head> <body> <h1 id="message-el">Ready to play?</h1> <p id="cards-el"></p> <p id="sum-el"></p> <butto ...

What is the best way to modify CSS and HTML with a post request?

I'm currently in the process of working on a webpage with multiple sections. One section includes a form that sends a POST request. Once this form is successfully submitted, I want to update the CSS style of another element to make it visible. Given ...

The jQuery menu is malfunctioning in Internet Explorer due to the Document Mode being set to Quirks

I am encountering an issue where the below code is not functioning properly in Internet Explorer's document mode quirks. Each time I hover over the submenu, its length doubles unexpectedly. Can someone please provide assistance with this matter? < ...

Is it possible to dynamically change HTML content by utilizing a JSON file?

Looking to implement a JavaScript loop (using jQuery) that can dynamically populate the HTML file with content from a JSON file based on matching <div> ids to the JSON "id" values. The solution should be scalable and able to handle any number of < ...

JavaScript, XML, and PHP all support the use of HTML entities within their code

Having some trouble as a newbie again))) Can you please help me out, guys? I'm having an XML file with the following data: <Page> <Content>&lt;p&gt;Article content&lt;/p&gt;&#13; &#13; &lt;h1 style="font-style ...