The flex item does not appear as intended when styled with justify-content: space-between

I'm trying to create a menu bar with sub-menus in CSS using Flexbox. I want to have an "Account" or "Profile" link on the right side of the menu bar on the same line, but for some reason, it's displaying under the main menu. I believe there's an issue with my CSS code, specifically the justify-content: space-between property for the nav element. How can I fix this?

Here's the code I'm currently using:

nav {
    font-size: 20px ;
    background-color: white;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
}

nav ul {
    display: flex;
    flex-direction: row;
    margin: 0;
    padding: 0 ;
    position: absolute;
    background-color: white;
    list-style-type: none;
}

nav ul li {
    padding: 0;
    margin: 0;
}

nav ul li ul {
    display: none;
    margin: 0;
    padding: 0;
    box-shadow: 3px 3px 3px 0px #DDDDDD;
    background-color: white;
}

nav ul li:hover ul{
    display: block;
}

nav a {
    color: #333;
    display: block;
    margin: 0;
    padding: 10px 20px;
    text-decoration: none;
}

nav a:hover {
    background-color: #DDD;
}
<nav>
    <ul>
        <li><a href="#">Menu1</a>
            <ul>
                <li><a href="#">Submenu</a></li>
                <li><a href="#">Another longer submenu here</a></li>
                <li><a href="#">Yet another submenu</a></li>
                <li><a href="#">Etc. etc.</a></li>
            </ul>
        </li>
        <li><a href="#">Menu2</a></li>
        <li><a href="#">Menu3</a>
            <ul>
                <li><a href="#">My submenu 1</a></li>
                <li><a href="#">My submenu 2</a></li>
                <li><a href="#">My submenu 3</a></li>
                <li><a href="#">My submenu 4</a></li>
                <li><a href="#">My submenu 5</a></li>
                <li><a href="#">My submenu 6</a></li>
                <li><a href="#">My submenu 7</a></li>
                <li><a href="#">My submenu 8</a></li>
            </ul>
        </li>
        <li><a href="#">Menu4</a>
        </li>
    </ul>
    <a href="#">Account</a>
</nav>

Answer №1

Oh, I just discovered my mistake.

nav ul { position: absolute; }

shouldn't be placed here, but here:

nav ul li ul { position: absolute; }

So, the issue is resolved (hopefully it can assist someone else)...

nav {
    font-size: 20px ;
    background-color: white;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
}

nav ul {
    display: flex;
    flex-direction: row;
    margin: 0;
    padding: 0 ;
    background-color: white;
    list-style-type: none;
}

nav ul li {
    padding: 0;
    margin: 0;
}

nav ul li ul {
    display: none;
    margin: 0;
    padding: 0;
    box-shadow: 3px 3px 3px 0px #DDDDDD;
    background-color: white;
    position: absolute;        
}
nav ul li:hover ul{
    display: block;
}

nav a {
    color: #333;
    display: block;
    margin: 0;
    padding: 10px 20px;
    text-decoration: none;
}

nav a:hover {
    background-color: #DDD;
}
<nav>
    <ul>
        <li><a href="#">Menu1</a>
            <ul>
                <li><a href="#">Submenu</a></li>
                <li><a href="#">Another longer submenu here</a></li>
                <li><a href="#">Yet another submenu</a></li>
                <li><a href="#">Etc. etc.</a></li>
            </ul>
        </li>
        <li><a href="#">Menu2</a></li>
        <li><a href="#">Menu3</a>
            <ul>
                <li><a href="#">My submenu 1</a></li>
                <li><a href="#">My submenu 2</a></li>
                <li><a href="#">My submenu 3</a></li>
                <li><a href="#">My submenu 4</a></li>
                <li><a href="#">My submenu 5</a></li>
                <li><a href="#">My submenu 6</a></li>
                <li><a href="#">My submenu 7</a></li>
                <li><a href="#">My submenu 8</a></li>
            </ul>
        </li>
        <li><a href="#">Menu4</a>
        </li>
    </ul>
    <a href="#">Account</a>
</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

"Revolutionary AJAX-enabled PHP social commenting system with multi-form support

Why is it that when I submit forms using these ajax functions in PHP, they only send to the first form on the page? I have multiple forms under each article and I want them to be submitted separately. What am I doing wrong? ...

Changing the MIME type from "application/octet-stream" to "video/mp4" in HTML5: A Step-by-Step Guide

<!DOCTYPE html> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> </head> <body> <video id="video" controls src="${maps.url }" height="598" width="782"> ...

What is the significance of keyframes in CSS animations at different percentages

I'm facing a challenge with the code below that is intended to make objects float from left to right in the background. However, once they go off-screen, a vertical scroll bar appears despite the overflow-y: hidden attribute added to the class. I atte ...

Mastering the Art of Displaying Every Side of a Spinning Cube Using HTML and CSS Animations

As I navigate the online world, my goal is to create a dynamic animation of a rotating cube with an image on each face. Despite my efforts, I am struggling to display all faces simultaneously, especially the front and back faces. I have explored various so ...

*ngIf doesn't seem to be functioning as expected compared to other *ngIf directives

Encountering a peculiar issue with my *ngIf related to the isAdmin variable which determines whether the list of users in userList should be displayed or not. Strangely, it seems to behave differently compared to other *ngIf statements within the same comp ...

Middle align an absolutely positioned block within a table cell vertically

Here is the code I am working with: td { border: 1px solid #cecece; position: relative; padding: 80px 20px; } .hint { background: yellow; position: absolute; top: 0; bottom: 0; left: 100px; } <table style="width:800px"> <tr> ...

Steps to Safely Transmit the Password through Ajax() Function in jQuery

I have implemented a Password textbox that starts with an empty value. When the user clicks on it and enters a password, the password will be updated in the database upon leaving the textbox (onblur event). I have successfully achieved this using ajax(). H ...

Conceal the icon for the Dropdown Menu arrow

Is there a way to hide the old arrow icon in the Select (drop down) after changing it with a new one? HTML: <div class="row"> <div class="col-sm-12"> <select name="country" class="form-control SearchBar"> <option ...

What is the purpose of making a "deep copy" when adding this HTML content?

My canvas-like element is constantly changing its contents. I am attempting to execute a function on each change that captures the current content and adds it to another element, creating a history of all changes. window.updateHistory = func ...

Display or conceal a div element using a dropdown menu selection

I have implemented the following code, which is currently functional and was sourced from a different thread on Stack Overflow: <Select id="colorselector"> <option value="red">Red</option> <option value="yellow">Y ...

Identify and forward to the mobile-friendly version of the site

I am currently working on a website that requires separate files for mobile view as the html layout poses challenges in making it responsive. My goal is to find code that can detect if the user is accessing the site from a mobile device, and automatically ...

The margin of a single flexbox item is not aligned properly

The flexbox I have contains several items. All of them are displaying correctly except for the last one, which is expanding to the edge of the div. View the current appearance of the flexbox As shown in the image, the last element is extending to the con ...

The Bootstrap image fails to adhere to the size of its parent flex container

I am having trouble positioning an image, heading, and a button on top of another image. The issue arises when viewing the content on small screens as the smaller image fails to resize properly and extends beyond the background of the larger holding imag ...

Steps for creating a resizable and automatically hiding side menu

I am working on a side menu that can be resized, and I want it to collapse (set to zero width) when it is empty. I have considered implementing one of these features individually, but I'm not sure how to make both work together. Is there a way to ad ...

Creating an HTML hyperlink to a file

I've been attempting to connect a blog post I saved previously to my new website, but so far I have not been successful in getting them to display. It keeps showing up as file not found. Is there something I am missing? My past blog posts are stored i ...

Create two grid components for Material-UI and use Flexbox to ensure that both items have equal height

I'm currently working on ensuring that two grid items have the same height using flex in Material UI, but I've hit a roadblock. It seems like my approach with the Grid element might be incorrect. I've experimented with adjusting the height a ...

What is the reason behind the functionality of invalid HTML?

As someone diving into the world of web programming, I have a burning question: Why does invalid HTML seem to work just fine sometimes? And why is it that HTML validation isn't always necessary? <html> <body> </html> It's intr ...

Having trouble getting the hover effect to change the colors of CSS borders

I am experiencing difficulty in changing the background color of the 'About' button to yellow and the border color to blue when it is hovered over. Currently, only a small rectangle around the text changes to yellow on hover and I cannot figure o ...

Altering the DOM directly within the componentDidMount() lifecycle method without needing to use

In ReactJS, I am facing an issue while trying to manipulate the DOM in the componentDidMount() method. The problem lies in the fact that the DOM is not fully rendered at this point, requiring me to use a setTimeout function, which I find undesirable. Upon ...

react-basic-photo-carousel: adjust size and dimensions

As a newcomer to React JS, I decided to use react-simple-image-slider for my image slider component. However, I am facing an issue with making the images responsive on mobile devices. I tried setting the width and height using vw, vh or percentage units, ...