Is it possible to have my menu underlined when it is active instead of just upon hover? I am not looking for the regular underline code

I'm currently implementing a CSS code snippet I found on CodePen to create a unique underline effect for the menu on my website. The goal is to move away from the traditional link underline style.

At the moment, the underline appears when hovering with a subtle animation effect. However, I would like it to be activated when the link is active instead of just on hover. Despite my attempts to modify the code, I find myself stuck due to not being the original creator of the underline code.

Is there anyone who can assist me with this issue? I apologize if my explanation seems messy, and I am willing to provide additional snippets of code if necessary. Thank you!

Below is the code I am using:

HTML:

<nav>
    <ul>
        <li class="item"><a href="index.html">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="services.html">Services</a></li>
        <li><a href="faq.html">FAQ</a></li>
        <li><a href="contact.html">Contact</a></li>
    </ul>
</nav>

CSS (Code from CodePen):

a:link {
    text-decoration: none!important;
}

nav li a {
    color: #fff;
    font-size: 14px;
    position: relative;
    letter-spacing: 2px;
    text-transform: uppercase;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
    padding: 15px;
}

a:after {
    content: "";
    position: absolute;
    height: 2px;
    background-color: white;
    width: 0;
    left: 50%;
    bottom: 0;
    -webkit-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    transform: translateX(-50%);
    -webkit-transition: 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) all;
    transition: 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) all;
}

a:hover {
    color: tomato;
}

a:hover:after {
    width: 100%;
}

Answer №1

To achieve the same styles as the hover effect, you can create a new class.

Change this:

a:hover:after {
    width: 100%;
}

To this:

a.active:after {
    width: 100%;
}

Then apply the class to the a:

<li class="item"><a class="active" href="index.html">Home</a></li>

ul {
    padding: 0;
    margin: 0;
    list-style:none;
}

ul li {
    float: left;
}

a:link {
    text-decoration: none !important;
}

nav li a {
   
    font-size: 14px;
    position: relative;
    letter-spacing: 2px;
    text-transform: uppercase;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
    padding: 15px;
}

a:after {
    content: "";
    position: absolute;
    height: 2px;
    background-color: red;
    width: 0;
    left: 50%;
    bottom: 0;
    -webkit-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    transform: translateX(-50%);
    -webkit-transition: 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) all;
    transition: 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) all;
}

a:hover {
    color: tomato;
}

/*a:hover:after {
    width: 100%;
}*/

a.active:after {
    width: 100%;
}
<nav>
    <ul>
        <li class="item"><a class="active" href="index.html">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="services.html">Services</a></li>
        <li><a href="faq.html">FAQ</a></li>
        <li><a href="contact.html">Contact</a></li>
    </ul>
</nav>

Answer №2

It seems there is confusion about the 15px padding in the 'a' element. Maybe it would be better to incorporate that into the 'li'. Another developer suggested using Firefox Developer, as its style editor can help identify potential issues with your styling.

https://i.sstatic.net/D0nhP.jpg

This approach allows you to visually understand how the color rules affect the size of your element based on its padding.

a:link {
text-decoration: none!important;
}

nav li a {
color: #fff;
font-size: 14px;
position: relative;
letter-spacing: 2px;
text-transform: uppercase;
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
margin: 15px;

}


a:after {
content: "";
position: absolute;
height: 2px;
background-color: white;
width: 0;
left: 50%;
bottom: 0;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
-webkit-transition: 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) all;
transition: 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) all;
}

a:hover {
color: tomato;
}

a:active:after {
width: 100%;
}
body{background-color:black}
<nav>
                <ul>
                    <li class="item"><a href="index.html">Home</a></li>
                    <li><a href="about.html">About</a></li>
                    <li><a href="services.html">Services</a></li>
                    <li><a href="faq.html">FAQ</a></li>
                    <li><a href="contact.html">Contact</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

I am curious as to why the "default + label" pseudo-class is not functioning properly on the "input" element. Specifically, I am having trouble with the "+ label" portion

":default" works fine on its own, but when combined with the "label" pseudo-class, it fails to apply. .pseudo-test input:default { box-shadow: 0 0 20px 20px red; } .pseudo-test input:default+label { color: coral; } <div style ...

Getting the value of an element from a form using jQuery

Struggling to retrieve the values for email and password from a simple HTML form using jQuery. However, all I get is an empty string. Can anyone point out what needs to be changed? Below is the HTML form: <form> <label for="email">Ema ...

Randomly swap images in HTML when a button is clicked

In order to change images randomly on mouse click, I came across this solution: <SCRIPT LANGUAGE="Javascript"> function banner() { } ; b = new banner() ; n = 0 b[n++]= "<A HREF='index.html'><IMG SRC='images/pic1.jpg'> ...

Trigger the fire event on .click() except when text is being selected

I am currently working on a chat box project and I want the input field within the message area to automatically get focused when the user clicks anywhere in the chat box. However, I don't want this functionality to interfere with text selection for c ...

Issues arise when trying to alter the attributes of the :hover pseudo-class

I'm having trouble changing the background-color of list items to black when hovering over them. However, changing the color itself is working fine. .nav li { display: inline-block; height: 100%; padding: 0px 35px; border-width: 0 1px 1px 0 ...

Position the text to line up perfectly alongside the floating image

How can I use the float property to align text and images? The text marked in red should be positioned next to the floating image, but for some reason I am having trouble getting it in the right place. https://i.sstatic.net/gfuu6.png This is my CSS for t ...

Is Polymer more of a comprehensive framework compared to just a library? What is the best way to implement web components in a modular fashion

Web components aim to modularize the web, independent of any specific framework being used. The Polymer project offers the ability to create web components without being tied to a particular framework, meaning they should be compatible with any framework. ...

Adding a dropdown feature in a React.js component

I am working on a React project that requires me to create a dropdown menu using only pure React. The dropdown should also be responsive for different devices. The expected interactions are as follows: For larger devices like desktop screens: On hover Fo ...

Looking for assistance with resizing and repositioning an image within a viewport?

The JSFiddle code can be found at this link - https://jsfiddle.net/pmi2018/smewua0k/211/ Javascript $('#rotate').click(function(e) { updateImage(90, 0) console.log("rotation"); }); $('#zoom-in').click(function() { updateImage(0 ...

Is there a way to dynamically adjust the height of a DIV block?

I have a situation where I need the height of one div to automatically adjust based on changes in the height of another div. var height = jQuery('#leftcol').height(); height += 20; jQuery('.rightcol-botbg').height(height); Unfortun ...

Ensure that the checkbox remains constant and visible at all times when using Bootstrap

Introducing the latest checkbox design in Bootstrap4 : Check out the Bootstrap4 checkbox: <label class="c-input c-checkbox"> <input type="checkbox"> <span class="c-indicator"></span> Click here </label> The stan ...

Having trouble aligning two divs horizontally on the same line in Bootstrap 4

I am facing an issue with my HTML code where I have two div elements that are supposed to be horizontally aligned, but the second div is appearing underneath the first one. To fix this, I tried using bootstrap classes for alignment. <div class="c ...

Generating a text input field with multiple lines

<!DOCTYPE html> <html> <body> <p>Press the button to generate a File Upload Button.</p> <button onclick="myFunction()">Click here</button> <script> function myFunction() { var x = document.createElemen ...

The preventDefault() function is not functioning properly on the <a> tag

As a JavaScript beginner, I decided to create an accordion menu using JavaScript. Although I was successful in implementing it, I encountered a bug in my program. In this scenario, uppercase letters represent first-level menus while lowercase letters repr ...

Round corners, images in the background, gradients in the background, and compatibility with Internet Explorer versions 8 and

I've encountered an issue where my div with border radius, background gradient, and background image are working fine in FireFox but not displaying correctly in IE8 or IE10. In IE8, the gradient and background image work properly, but as soon as I app ...

Bootstrap 4 collapsible navbar with a stacked design

I am struggling to create a collapsible navbar. Even though everything seems to be working, the nav items are not stacking vertically as they should be. Instead, they are appearing horizontally next to each other. Here is the code snippet: <nav class= ...

Stylish Navigation Menu Logo/Site Name

Currently in the process of upgrading my website and I have my website name included as part of my menu, as opposed to a logo. This has been an easy solution that has worked well for me. For mobile screens, the template I purchased includes a slicknav men ...

Looking for assistance in creating an overlapping effect between two divs using an SVG?

I am attempting to create an effect where the bottom div overlaps the top one with a wavy SVG, giving the illusion of a wavy line on the bottom div that extends into the top div However, my current result is different than what I expected. It looks like t ...

How to prevent click events and still enable scrolling within a DIV using CSS/JS

Basically, I am looking to enable scrolling within a DIV while also disabling click events. Any suggestions on how this can be achieved? Appreciate any help! ...

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...