Hover-triggered menu animations

I'm in the process of constructing a navigation menu using bootstrap 4, but I've encountered an issue that's proving to be quite challenging.

header > nav {
    background-color: #fff;
}

.navbar {
    padding-top: 1.6rem;
    padding-bottom: 1.6rem;
}

.navbar-nav > li > a {
    font-size: 1.4rem;
    color: #504843;
    text-transform: uppercase;
    letter-spacing: .1rem;
    font-weight: 700;
    margin-right: 2rem;
}

.navbar-nav > li > a:hover, 
.navbar-nav > li > a:focus {
    background: none;
    font-size: 1.4rem;
    color: #262423;
    border-bottom: .3rem solid #ff5f06;
    letter-spacing: .1rem;
}

.navbar-nav > li:last-child {
    margin-right: -2rem;
}
<nav class="navbar navbar-expand-lg navbar-light bg-light sticky-top">
       <div class="container">
        <a class="navbar-brand" href="#">
            <img src="assets/images/logo.png" alt="">
        </a>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Features</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Pricing</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link disabled" href="#">Disabled</a>
                </li>
            </ul>
        </div>
        </div>
    </nav>

I have successfully implemented an orange underlined effect (border-bottom) on hover for each menu item. However, when hovering over a menu item, the entire menu shifts upwards slightly.

Any suggestions on how to prevent this upward shift? Thank you

Answer №1

To see a live demonstration, check out this link: jsfiddle.net/n5f2b4qk/

If you're experiencing "Jumps", it's likely due to the border-bottom: .3rem solid #ff5f06; styling.

There are two ways to address this issue:

  1. One option is to set the border in the :after element (shown in the example below)

    .navbar-nav > li > a { font-size: 1.4rem; color: #504843; text-transform: uppercase; letter-spacing: .1rem; font-weight: 700; margin-right: 2rem; position: relative; }

    .navbar-nav > li > a:hover, .navbar-nav > li > a:focus { background: none; font-size: 1.4rem; color: #262423; letter-spacing: .1rem; }

    .navbar-nav > li > a:hover:after, .navbar-nav > li > a:focus:after { content: ""; position: absolute; display: block; left: 0; right: 0; bottom: 0; height: .3rem; background: #ff5f06; }

  2. Alternatively, you can opt to set a transparent border for the a element and change the color on hover.

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

Leveraging class names to dynamically apply CSS styles to components based on their props value

In the process of developing a collection of reusable components, I have utilized material-ui and styled them with CSS. One specific requirement is to dynamically set the width of a component through a prop passed into a custom button. My goal is to lever ...

Is there a way to retrieve php session in a javascript file?

Here's the code snippet for reference: Contents of index.php file Javascript code from index.php function Result() { var marks = 55; document.getElementById("hdnmarks").innerHTML= marks; window.location = "results.php"; } HTML code from ind ...

List of dropdown options retrieved from SQLite database

I am attempting to retrieve data from an SQLite database table in order to populate a dropdown menu list. My thought process is outlined below. The main issue I am facing is how to integrate the JS function with the HTML section. HTML.html <label ...

Is Jade used for making subsequent lines children of an included partial?

Recently, I've encountered an issue with a basic jade layout. Here is an example: include test.jade #bar hi In the test.jade file: #foo hello No matter what I try, the #bar element always ends up as a child of #foo. <div id="foo">hello &l ...

Retrieving byte data from an HTML file results in varying byte arrays across different devices

During the process of creating unit tests for a project that utilized the Razor engine to generate HTML, I encountered a peculiar issue. To ensure the accuracy of the unit test, I manually set the model, executed the function, and saved the resulting HTML ...

The JavaScript class failed to work properly within the asp.net framework

I have successfully implemented a JavaScript function on my aspx page. Here is the code snippet: <script type="text/javascript"> $(document).ready(function () { console.log("ready!"); var options = []; var ...

Troubles with Fixed Navigation Bar in JavaScript-Enabled Scrolling

I've implemented a JavaScript-enabled scrolling navigation bar that starts below a hero graphic and then sticks to the top when it reaches the top of the page. The functionality works as intended, but there is one issue - when the navigation bar reach ...

In order to effectively utilize and display images on a webpage, I require a JavaScript array to store image links for use as image sources

I need assistance with changing these images. I am trying to store their links in an array, but when I click the button to change the image, it does not recognize that the array variable points to an image. Can someone offer guidance? Here is my current ...

How can I transform an HTML div into a video file like MP4 using Python and Django?

I'm looking to take a HTML page and target a specific <div> within it in order to convert it into video format. Purpose: I understand that HTML is typically static, but I have a requirement to transform it into a video. I'm seeking method ...

Having issues with Angular Material, specifically with mat-list-item and routerLinkActive not functioning as expected

Currently, I am working with a navigation view that utilizes the MatSidenavModule. The issue I am encountering is on mobile screens. When I click a mat-list-item, the mat-sidenav closes as expected. However, upon opening the mat-sidenav again, Material alw ...

What is the best way to combine all footer cells in a DataGrid that is responsive and built with Bootstrap?

How can I merge the columns of the footer in a multi-column DataGrid within an ASP.NET user control? I am using C# for the code-behind, Bootstrap 4 beta classes to hide columns based on viewport width, Font-Awesome for symbols/icons, and the provided Dat ...

The image hover feature is not functioning as expected in Angular 4

Currently, I am involved in a project using Angular 4. One particular section involves changing images on hover. Although I have implemented the following code, it does not seem to be functioning correctly for me. Interestingly, the same code works perfect ...

Using JQuery to toggle image visibility, starting with the first image and scrolling through until only one image remains visible

I am facing a challenge where I am unable to modify the HTML code. The current structure is as follows: <div class="slider"> <div class="slider-control"> <button id="prev">Previous</button> ...

What is the reason behind getElementsByClassName not functioning while getElementById is working perfectly?

The initial code snippet is not functioning correctly DN.onkeyup = DN.onkeypress = function(){ var div = document.getElementById("DN").value document.document.getElementsByClassName("options-parameters-input").style.fontSize = div; } #one{ heigh ...

The blur function in jQuery is not functioning as expected

I'm facing an issue where only the first of my 3 .blur's is working. Here is the jQuery code snippet: <script type='text/javascript'> $(document).ready(function() { $("#user_name").blur(function() { if ($( ...

Checking if an Element is Visible in Selenium

Is there a way to determine if an element is within the visible viewport of a browser using Selenium? I attempted to do this by comparing the dimensions and locations of the element and the browser window. However, I encountered an issue where the Y value ...

Modify the color of the header on Material-UI Date Picker

I recently integrated a Material-UI Date Picker into my React Single Page Application and now I'm facing an issue with changing the header color. I attempted to modify it using the muiTheme palette property, but unfortunately, the header color remains ...

Styling a div box

I've been experimenting with CSS on a div for a while now, but I'm still struggling to replicate the image below. If anyone can offer some guidance, it would be greatly appreciated. I'm looking for a solution involving a div or another elem ...

Is there a way for me to display an alert notification when a website requests access to additional storage on my computer?

Is there a way to set up an alert notification in Internet Explorer when a website requests additional storage on your computer? The notification would ask: Do you want to grant permission for this website to use additional storage on your computer? ...

Tips for stopping tables from stretching past card body width in Bootstrap 4

Here is a sample of the code I used: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> & ...