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

Adding a stylesheet dynamically to the <head> tag using $routeProvider in AngularJS

Is there a way to load a specific CSS file only when a user visits the contact.html view on my AngularJS application or site? I came across this helpful answer that almost made sense to me How to include view/partial specific styling in AngularJS. The acce ...

The <input> element is not functioning as expected when attempting to use it as a button. I am struggling to find the solution to

My HTML file contains: <!Doctype HTML> <html> <head> <title>Orange’s Game</title> <meta charset="utf-8"> <script src="game.js”></script> </head> <body> <input type="button ...

Arranging and overlaying images with HTML and CSS

Currently, I am in the process of developing a simplistic game through the use of HTML and CSS. The game consists of a background image portraying an alleyway, and my objective is to incorporate additional images on top of this background as clues within t ...

The height of the TinyMCE editor refuses to adjust

Hello, I'm new to utilizing Tinymce within an AngularJS environment. Specifically, I am working with the Tinymce library version 4.3.13. I have constructed some HTML and angularJS code to implement the Tinymce editor, but I've encountered a probl ...

Angular list with a repeating group of radio buttons

I have a set of 'options', which consists of the following: {Id: 1, Label: "option 1"}, {Id: 2, Label: "option 2"} Additionally, I have a list of 'products' structured as follows: {Id: 1, Name: "Name 1", Recommend: options[0]}, {Id: ...

Extract the color of an individual character

There is a code snippet in JavaScript using p5.js that functions as a video filter: const density = ' .:░▒▓█' //const density = ' .tiITesgESG' //let geist; let video let asciiDiv let playing = false let ...

Guide to utilizing custom fonts in a VUE application

I'm currently working on my first Vue project and despite following various examples and the official documentation, I am struggling to resolve an issue regarding importing fonts locally in my project. Within my `` tag, I am importing the font that I ...

Alter the class of the div element every three seconds

Greetings, I trust everyone is doing well. I am in need of some assistance from your brilliant minds. I have a circular div along with three CSS classes, and my objective is to switch the div's class and update the label text every 3 seconds. Any insi ...

Drop-down and autocomplete feature in Material UI design framework

Encountering an issue with aligning a label with a dropdown in material UI, and for some reason, this behavior is observed: Struggling to get them aligned on the same row. This is the code snippet of my component: const useStyles = makeStyles(theme => ...

I would like to automatically log out when closing the tab in Mozilla, Internet Explorer 8.0, or Chrome

Hello there, I was wondering if it's feasible to end the session and log out when I close my tab. I'd appreciate it if you could confirm whether this feature has been implemented on your end. Thank you, Manish ...

Clarification: Javascript to Toggle Visibility of Divs

There was a similar question that partially solved my issue, but I'm wondering if using class or id NAMES instead of ul li - such as .menu, #menu, etc. would work in this scenario. CSS: div { display:none; background:red; width:200px; height:200px; } ...

Bring the element to the top of the page by clicking on the anchor within the element or anywhere within the specified div ID

I am looking to implement a functionality where the page scrolls to the top of the navigation div ID when a link inside the navigation div is clicked, or ideally even when clicking anywhere within the div itself that contains the navigation links. After r ...

Customizing the size of dateBox icons in JQuery Mobile

Is there a way to increase the button size within the input box of a dateBox? Currently, it appears small and difficult to click accurately. Additionally, it would be beneficial if clicking anywhere in the input field could open the calendar, instead of ju ...

When the button is clicked, the JavaScript function is not being executed

I'm having a strange issue with my second button not working as expected. Despite appearing to be straightforward, the "Reset" button does not seem to be triggering the clear() function. Within the HTML code, I have two buttons set up to interact wit ...

Adjusting the color of text based on the fulfillment of a condition

Is there a way to change the text color of a variable based on its value in PHP? For example, if the $status is set to admin, can we display it in red when echoed out using <h3>Status: <?php echo $status; ?></h3>? I'm not quite sure ...

Tips for defining a relative path in the base URL with AngularJS

Encountering an issue with AngularJS routes related to file paths. The folder structure is as follows: js -> Angular -> css -> Bootstrap -> index.html Routes work fine when hosted on a server like IIS. However, copying and pasting the direct ...

Does jQuery's click event not successfully hide an element on Internet Explorer?

In my dropdown menu, I have the following options: <div class="my_div"> <select name="selection" id="select_id"> <option value="0">A</option> <option value="5">B</option> ...

Switching between various quantities of inputs in each row using Bootstrap V3

My Bootstrap V3 form has a mix of rows with two inputs and rows with one input, as per the designer's request. Check out my fiddle here: https://jsfiddle.net/06qk4wh4/ <div class="form-group col-sm-12"> <label class="control-label col- ...

What's the best way to align divs vertically in a compact layout?

Update The reason I require this specific behavior is to ensure that all my content remains in chronological order, both on the web page and in the HTML structure. This way, even if the layout collapses into a single column on smaller screens, the content ...

Table for maximizing space within a cell (Internet Explorer 8)

I am attempting to make the "innerTable" fill up all available space within another table. The issue is with IE8 (no compatibility mode, IE8 browser mode, Doc mode IE8 Standards). The table does not expand to fit the containing TD. I tried enclosing the ta ...