Firefox exhibiting an additional pixel when hovering over dropdown menus

When using top: 100% to position a sub-menu dropdown below the main navigation bar on hover, there seems to be an issue in Firefox where the menu is pushed below the border of the menu causing a small gap. Interestingly, this works as expected in Webkit browsers without any gaps. The use of 100% is deliberate to avoid hard-coding specific values.

To better illustrate the problem, I have created a test scenario on CodePen, with the menu bordered in red for clarity. The second nav simply demonstrates the hover state.

Update:

The gap issue disappears if I remove display: table; from the .nav-main element. However, I need to retain it to be able to apply display: table-cell; to the navigation list items for proper spacing and alignment across the entire navigation. Any suggestions on how to resolve this dilemma?

HTML:

<nav role="navigation">
    <ul class="nav-main">
        <li><a href="#">Link One</a></li>
        <li>
            <a href="#">Link Two</a>
            <ul class="sub-menu">
                <li><a href="#">Dropdown Link One</a></li>
                <li><a href="#">Dropdown Link Two</a></li>
                <li><a href="#">Dropdown Link Three</a></li>
            </ul>
        </li>
        <li><a href="#">Link Three</a></li>
        <li><a href="#">Link Four</a></li>
    </ul>
</nav>

SCSS:

$pink: #ed2490;

a {
    text-decoration: none;
}

.nav-main {
    position: relative;
    display: table;
    margin: 0;
    padding: 0;
    width: 100%;
    border: 1px solid lighten(black, 22%);
    border-radius: 4px;
    background: lighten(black, 8%);
    @include background(linear-gradient(bottom, lighten(black, 8%), lighten(black, 36%)));
    font-weight: 500;
    line-height: 1;

    > li {
        display: table-cell;
        text-align: center;
        position: relative;

        &:hover {
            .sub-menu {
                top: 100%;
            }

            a {
                background: lighten(black, 18%);
                background: rgba(black, 0.25);
            }
        }
    }

    a {
        display: block;
        color: white;
        padding: 15px 10px;

        &:hover,
        &:focus {
            background: lighten(black, 18%);
            background: rgba(black, 0.25);
        }
    }

    .sub-menu { // dropdown
        position: absolute;
        top: -999px;
        z-index: 10;
        margin: 0;
        padding: 0;
        width: 200px;
        background: lighten(black, 4%);

        li {
            border-top: 1px solid lighten(black, 18%);

            &:first-child {
                border-top: 0;
            }
        }

        a:hover {
            background: $pink;
        }
    }
}

// For style demonstration only
.styleguide-dropdown {
    padding: 40px 20px 130px;
}

.nav-main {
    border-color: red;

    .psuedo-hover {
        a {
            background: lighten(black, 18%);
            background: rgba(black, 0.25);
        }

        .sub-menu {
            top: 100%;

            .psuedo-hover {
                background: $pink;
            }
        }
    }
}

Answer №1

When dealing with styles that involve display: table-*, it can be a challenge to maintain consistency across various browsers.

Fortunately, there are solutions that can help address this issue:

.submenu { display:none;}
:hover > .submenu { display: block;}

or

.submenu { height: 0; overflow:hidden }
:hover > .submenu { height: auto; }

By implementing either of these fixes, you can achieve a more stable layout without relying on drastic shifts in positioning. Additionally, these approaches are better suited for reflow and repaint processes, as well as offering more flexibility for potential reuse of the navigation lower down on a page.

Cheers!

Answer №2

By eliminating the 1px border around ul, you can then introduce top and bottom borders to each individual li. However, keep in mind that the border-radius will not be computed in the same manner.

I suspect that the discrepancy lies in the fact that the height of the ul surpasses the height of the li due to the presence of a 2px border, causing 100% of the li's height to fall short... though I cannot say for certain.

To see it in action, check out this functioning demo: http://codepen.io/anon/pen/jsBEt

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

Arrows on the button are unresponsive and there seems to be an incorrect number of

I've been working on a project by combining various examples I found online. I'm puzzled as to why it's displaying all the buttons at once instead of just one per page, and why the number of visible buttons decreases by one with each right c ...

Finding a specific element on a web page can be accomplished by targeting its href attribute

I'm facing a simple task. I have a String with an href value, and my goal is to locate the web element on the webpage that includes that specific href. Here's what I'm trying: String href = "http://www.bing.com/images?FORM=Z9LH"; driver.fin ...

How can I align an input to the right using Bootstrap?

I found an example of a navbar that I liked and decided to copy it. Here is the code snippet: <div class="container"> <h3>Nawigacja zamknieta w kontenerze</h3> <nav class="navbar navbar-toggleable-md navbar-light bg-faded"> < ...

Adjusting the width of a Material UI drawer: Tips and tricks

Currently, I am working on implementing the Material UI drawer and anchoring it to the top of my page. It is functioning correctly, however, I am encountering an issue where I am unable to adjust the width of the drawer. Despite trying to update the width ...

What methods can be used to dynamically pan the entire picture in a programmatic way?

I have a massive div measuring 11500x11500 pixels that contains 400 images, causing it to overflow the viewport. My goal is to programmatically pan across the entire div. I'm aiming to create an animation that pans the entire div from top to bottom, ...

JQuery hover functionality

What modifications are needed in the code below to display a div on the right side of the hyperlink when the onmouseover event occurs? <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> ...

Differences between HTML viewports and media queries

For simple webpages, my general rule is to utilize viewport units as they provide a lot of flexibility to the website. However, when dealing with complex webpages, I usually opt for media queries. In some cases, especially intermediate ones, I find it ben ...

Python BeautifulSoup Web Scraping may be successful initially, but it may not work for subsequent attempts or any subsequent instances

As a beginner in Python, I am attempting to create a web scraper to collect price data. The website I am targeting is: This is the code snippet I'm using: from bs4 import BeautifulSoup import requests URL = "https://www.medizinfuchs.de/?par ...

Emails sent through an HTML form submission should always include a valid "from"

I am currently in the process of creating a feedback form that allows individuals to provide anonymous feedback. I have explored nodemailer and node-sendmail, but I have encountered an issue with both requiring a from address. While I am aware that this ca ...

Enhance Your Joomla 2.5 Website with Custom CSS Specifically for iPad Viewing

Is there a way to load a specific CSS file when accessing my Joomla website through an iPad? I am running Joomla version 2.5. Although I have set up style-sheets for browsers like Internet Explorer and Firefox, I am unsure how to target the website to lo ...

In order to eliminate the underline in Bootstrap links, I am seeking a solution

I have encountered a similar issue before, and the solution proposed involved making changes via CSS. Despite my attempts, I have been unsuccessful in replicating the solution with my own code. Currently, the link appears as follows: I have also referred t ...

In JavaScript, you can verify if a specific <input> element is an input-field

With the introduction of HTML5, a variety of new input types have been added: <input /> Is there a way to detect whether an input field is a text field (such as date, time, email, text, etc.) without explicitly specifying each type? I would like t ...

Additional section for positioning beyond designated spot

If you want to understand better, take a look at this link: Here's the scenario: I have one article with 2 CSS columns. In the first column, there is text and images for the article, while in the second column, I want to place aside elements like tex ...

The creation script in create-react-app automatically includes an external import in the CSS file

I am facing an issue with my create-react-app. Everything works perfectly fine when I run it using npm run start. However, after building it with npm run build, some CSS appears to be missing from the app. Upon investigation, I discovered that the file bu ...

Using a variable as a URL parameter in a jQuery ajax request: tips and tricks

$.ajax({ type:"POST", url:"hostname/projfolder/webservice.php?callback=statusReturn&content="+str_table, contentType: "application/json; charset=utf-8", crossDomain:true, dataType:'jsonp', succe ...

Designing a menu header against a specific background color resulting in misalignment

I have been working on creating a menu header for my website. If you would like to take a look, here is the link to my jsfiddle page. Unfortunately, I am facing an issue where all my images and text should remain in that grey color scheme but somehow it& ...

Can someone show me how to toggle <td> elements, also known as 'table data cells', in Javascript?

My goal is to create a functionality where I can click to show/hide table data cells. Despite trying various methods to toggle their display, I have hit a roadblock in my progress. No matter how many functions I attempt, I keep encountering the same error ...

What could be causing my fixed element to disappear when the webpage is not long enough?

OBJECTIVE: a div is { position:fixed; bottom:0% } it has a genuine height and width; the z-index is the highest in the document = so, it should always be visible! ISSUE: mobile browsers 'cut' the bottom end of the page when zooming out. ...

Is there a way to split the text into distinct pages within a contenteditable area, similar to the functionality in Google Docs

I've been working on developing a specialized browser-based text editor and I've encountered a puzzling question. How can I detect and split long texts into separate pages, similar to how Google Docs handles pagination? I'm aware that Google ...

Tips for ensuring consistent display of UTF-8 arrow characters across different web browsers

Is there a way to ensure consistent display of UTF-8 arrow characters across all browsers? I have experimented with various font sizes like px and pt, as well as using HTML Entity codes (e.g. &#9664;) but unfortunately the arrows still show difference ...