Issue with animated underline not appearing after link is selected

Utilizing Bootstrap and SCSS to tailor my CSS file, I've implemented a nav-link with the following style:

.sidebar .nav-pills .nav-link:hover,
.sidebar .nav-pills .show > .nav-link {
    @extend %underline-link;
    background: linear-gradient(120deg, $sidebar-navitem-background-color 65%, $sidebar-navitem-background-color 65%);
}

.sidebar .nav-pills .nav-link.active,
.sidebar .nav-pills .show > .nav-link {
    @extend %underline-link;
    background: linear-gradient(120deg, darken($sidebar-navitem-background-color, 2) 65%, darken($sidebar-navitem-background-color, 2) 65%);
}

%underline-link {
    color: $underline-link-color;
    position: relative;
    text-decoration: none;
    transition: all 0.4s ease;

    &:before {
        content: "";
        position: absolute;
        width: 100%;
        height: 3px;
        bottom: 0px;
        left: 0;
        background: $underline-link-color;
        visibility: hidden;
        transform: scaleX(0);
        transform-origin: center left;
        transition: all 0.3s ease 0s;
    }

    &:hover {
        transition: all 0.4s ease;

        &:before {
            visibility: visible;
            transform: scaleX(1);
        }
    }
}

While the line animates correctly on hover, it disappears when the item is active.

How can I ensure the line remains visible while the item is active?

Answer №1

If you want the active state of your link to be identical to the hover state, make sure to include the :active pseudo class along with the :hover pseudo class in your SCSS code. This ensures that when a user clicks on the link and it becomes active, the style remains consistent. For more information on the active state, check out this resource on MDN: More information about active state on MDN.

To demonstrate how different styles for hover and active states interact, I've created a codepen which you can view here: a codepen with different styles for hover and active. If both :hover and :active have the same styles applied, there will be no visual distinction between the two states.

&:hover,
&:active {
    transition: all 0.4s ease;
    
    &:before {
        visibility: visible;
        transform: scaleX(1);
    }
}

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

How does Facebook access the new hashtags when they are clicked on?

I've been developing a website recently and I'm really impressed with the way a popup page appears when you click on a hashtag. I've been wanting to incorporate a similar feature for a while now, but I'm not sure how to do it. Could thi ...

Trouble with achieving equal height columns in nested flexbox layout on Google Chrome

I am looking to evenly space several sidebar blocks throughout a lengthy post. To achieve this, I have utilized Flexbox within the sidebar for handling the even distribution of the blocks and it has been working flawlessly. Check out Code Pen: http://cod ...

Encountering an error when utilizing Angular Animation with Bootstrap -> Issue arises from attempting to read properties of undefined (specifically 'box-sizing')

To access the source code of Git, visit https://github.com/codezj/exampleAppAnimation. I recently developed a method to extract Bootstrap CSS and utilize it as CSS in Angular. However, I encountered an error while running the Angular project: Uncaught Typ ...

Scrolling to does not function properly with Material UI tabs

Looking for a solution to scroll to a specific div when clicking on a tab in Material UI Tabs using UseRef and ScrollTo. Check out the sandbox link here: https://codesandbox.io/s/exciting-sound-mrw2v Currently, when I click on Tab 2, I expect it to autom ...

Progress Bars Styled with CSS Percentages

I need help creating a basic CSS percentage bar. To get started, visit and paste the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://w ...

Center contents of Bootstrap row vertically

I have a simple property grid layout in Bootstrap 3 structured as follows: <div class="row"> <div class="col-md-3"> <div id="propGrid"> <div class="row pgTable"> <div class="col col-md-12"> <d ...

What is the best way to define my background image using markup language?

I'm facing a challenge with some code that I didn't write which includes a background-image. While I want to maintain the identical appearance, I prefer to set the image in my markup rather than CSS. However, I'm unsure of how to do this. T ...

Executing cssnano Using the Command Prompt

Although I'm not an expert in node JS, I have come across cssnano as a JavaScript tool for minifying CSS, which apparently does a more sophisticated job compared to the outdated YUI compressor. My only issue is that I am struggling to understand how t ...

When applying a vertical-align property to both an anchor tag and a button with the same class, why is the span also aligned to the top?

Why is the span tag aligning to the top when I have only applied properties to the .btn class elements, and not targeted it directly? .btns { text-align: center; } .btn { text-decoration: none; border: 1px solid black; display: inl ...

Modifying Div Size with Jquery

I am working on populating the div container with square boxes, but I'm having trouble adjusting the size of my #gridSquare div. Despite trying to change its height and width using jQuery, it doesn't seem to work as expected. $('#gridSquare ...

Tips for utilizing fontawesome effectively in a select menu

I'm having trouble displaying fontawesome icons in a select element, as they do not appear correctly in the drop-down list. .FontAwesomeSelect { font-family: Font Awesome\ 5 Free; font-size: 18px; } <link href="https://use.fontaw ...

Position the text in the center of a graph within a grid cell by utilizing dash and plotly

Is there a way to center text over a graph within a cell of a grid table using dash and plotly? I'm struggling with the alignment currently, as the graph is not centered while the text appears centered in the view but not in the cell itself. You can ...

issue with the margin-bottom property not functioning as expected

Question about CSS code: .dropdown-menu { position:absolute; top: 100%; left: 0; z-index: 1000; display: none; float: left; min-width: 160px; padding: 5px 0; margin: 2px 0 0; list-style: none; font-size: 14px; background-color: #ff ...

Row within table extending beyond boundaries of parent div table

Is there a way to make a table row wider than its parent table? I want the button at the bottom to extend beyond the borders of the table. https://i.sstatic.net/VH0HP.png Do I need to abandon the table styling to achieve this effect? Check out this JsF ...

What are the best strategies for optimizing my CSS drop down menu to be both responsive and mobile-friendly?

I am struggling to make my current CSS and HTML menu fully responsive and mobile-friendly. I have researched solutions from other sources but have been unable to implement them successfully. I am seeking help in modifying my menu so that it adjusts to smal ...

What is the process for adjusting the color of a mat-divider?

Has anyone been successful in changing the color of mat-divider? I attempted the following but had no luck: component.html <mat-divider class="material-devider"></mat-divider> component.scss .material-devider { color: red } ...

Incorporate a time selector into a Bootstrap 4 input form

Can anyone help me figure out how to incorporate a timepicker into an input field on a Bootstrap 4 admin template? Previously, I had integrated a jQuery timepicker successfully, but after switching to the new Bootstrap 4 admin template, it stopped working ...

Switch between different table rows

I have here a table that is used for displaying menu and submenu items. It's a mix of PHP (to fetch the menu items and their respective submenus) and HTML. What I am trying to figure out is how to toggle the visibility of only the submenu items under ...

Leveraging a vacant CSS class to locate specific HTML elements

While browsing through some HTML code, I noticed the use of classes without any CSS styling attached to them. Instead of using id's, they were simply used to group or identify elements for jQuery selectors: html: <div class=someClass> </div ...

What is the best way to enable this image lightbox to function seamlessly with both horizontal and vertical images?

Looking to create a responsive image lightbox? After starting from an example on W3Schools (https://www.w3schools.com/howto/howto_js_lightbox.asp), I ran into issues with portrait oriented images. Ideally, the solution should handle both landscape (e.g., 1 ...