Position the floated navigation bar in the center and have the dropdown menu appear from left to right

In order to center align the content in the nav bar and keep the float left property without causing any issues, consider using display: inline-block. Additionally, you may want the dropdown bar for the left thing to start at the image's left side and expand towards the right.

Check out the demo fiddle for a visual representation.

HTML Code:

<div id="nav">
    <div id="container">
        <ul>
            <li>
                <a href="#"> Left thing </a>
                <ul>
                    <li><a href="#"><----- want it to go this way</a></li>
                    <li><a href="#">i want this to start under left thing</a></li>
                </ul>
            </li>
            <li>
                <img src="http://www.jonathanjeter.com/images/Square_200x200.png" style="height:70px" />
            </li>
            <li>
                <a href="#"> Right thing </a>
                <ul>
                    <li><a href="#">This starts right</a></li>
                    <li><a href="#">And this is right</a></li>
                </ul>
            </li>
        </ul>
    </div>
</div>

CSS Styling:

* {
    padding: 0;
    margin: 0;
}
#body {
    padding: 0;
    margin: 0;
    font-family: Arial;
    font-size: 17px;
}
#nav {
    background-color: #72776A;
    width: 100%;
    position: fixed;
    height: 50px;
}
#nav ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    position: relative;
    display: block;
}
#nav ul li {
    float: left;
}
#container {
    text-align:;
}
#nav ul li:hover {
    background-color: #333;
}
#nav ul li a, visited {
    color: #ACD661;
    display: block;
    padding: 15px;
    text-decoration: none;
}
#nav ul li:hover ul {
    display: block;
}
#nav ul ul {
    display: none;
    position: absolute;
    color: red;
    border: 1px solid black;
}
#nav ul ul li {
    display: block;
}
#nav ul ul li a:hover {
    color: #6699;
}

Answer №1

update

#wrapper {
  width: 320px;
  margin: 10px auto;
}

View this demo

Answer №2

Before we delve into the solution, let's address your CSS issues. I have removed redundant declarations such as margin: 0, padding: 0, highlighted any missed selectors or typos. In order to achieve the desired outcome, you need to specify a width for your container div and set margin-left and margin-right to auto:

/* {
    padding: 0;
    margin: 0;
} This is not the most efficient solution as it may not be necessary for everyone */

/* Instead, here is a group of selectors */

body,
#nav ul {
    padding: 0;
    margin: 0;
}
/*#*/body { /* Just referring to the body, right? */
    font-family: Arial;
    font-size: 17px;
}

#nav {
    background-color: #72776A; /* You forgot the # here */
    width: 100%;
    position: fixed;
    height:50px; /* Smaller than your image */
}
#nav ul {
    list-style-type: none;
    position: relative;
    /*display:block; Not required */
}

#nav li {
    float: left;
}
#container {
    /*text-align:; Seems like something is missing here? */
    margin: 0 auto;
    width: 300px;
}

#nav ul li:hover {
    background-color: #333;
}

#nav ul li a,
#nav ul li a:hover,
#nav ul li a:visited {/ { /* Did you mean a:visited? */
    color: #ACD661; /* Another missing # here */
    display: block;
    padding: 15px;
    text-decoration: none;
}
#nav li:hover ul { /*ul unnecessary (assuming #nav ul li...) */
    display: block;
}
#nav ul ul {
    display: none;
    position: absolute;
    color: red;
    border: 1px solid black;
}
#nav li li { /* Shorter version of ul ul li */
    display: block;
}
#nav a li a:hover {
    color: #699;
}

You can view the code here

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

Error in zone: 140 - Property 'remove' is not readable

I'm brand new to kendo ui. I successfully set up a prototype in my fiddle with a working delete confirmation window. However, when I try to integrate it into my existing codebase, I encounter an error: Cannot read property 'remove' at the li ...

Progress Bar Modules

I am currently working on creating a customizable animated progress bar that can be utilized as follows: <bar [type]="'health'" [percentage]="'80'"></bar> It is functional up to the point where I need to adjust different p ...

Is there a way to recycle an image and ensure that the web browser only needs to download it once?

Is there a way to effectively reuse the same image multiple times on my website without inefficiently downloading it each time? ...

What is the best way to change the background color of a table cell in HTML and CSS?

Trying to adjust the background image of each data cell to a selected image has been challenging. Using the method displayed below, only the top left corner of the photo appears. The code snippet demonstrates setting the background of each data cell to th ...

Display a unique button and apply a strike-through effect specifically for that individual list item

I've encountered an issue with my code that is causing problems specifically with nested LI elements under the targeted li element: $('#comment-section .comment-box a#un-do').hide(); $('#comment-section ul li[data-is-archived="true"]& ...

What is causing the data element to remain unchanged within a Vue.js function and what steps can be taken to ensure its value can be updated?

Using the axios API in the created() function, I am able to access webURLs. When a mouseover event triggers a v-for handler in the HTML file, the index is obtained and assigned to the selectedState data element. Although the value of selectedState changes ...

Displaying PDF files on the internet without allowing them to be downloaded

How can I display PDF files on my website without allowing them to be saved or downloaded? Is there a way to prevent browsers from saving or downloading the PDF files? ...

Converting a CSS file into a string by importing it into a JavaScript file

Is it possible to manipulate a style tag loaded from an external CSS file using plain JavaScript? I'm attempting to apply custom styles to a style tag fetched from a CSS file, The issue lies in my struggle to convert the file content into a string ...

Using Handlebars: Rendering HTML within a passed parameter to a template engine

My current setup involves using expressjs with hbs (Handlebars) as the template engine. The issue I'm facing is that when a page loads, one of the parameters passed to the template contains HTML code. However, instead of processing the parameter and d ...

Deselect one checkbox while selecting another

<?php session_start(); if (count($_POST) > 0) { $_SESSION['link'] = $_POST['link']; } ?> <form method="post"> Gmail: <input type="checkbox" name="link" value="gmail" id="gmail" <?php if ($_SESSION[&apos ...

Tips on showcasing a kendo template within a different one

I have a kendo popup template for add/edit functionality. I am looking to display a list of products with checkboxes inside a list-view in that template. I came across this helpful link on Stack Overflow that explains how to create a list-view of checkboxe ...

Ensuring consistent width for both tables

How can I ensure that two tables have the same width at all times, even when the Confirmer name is long and causes one table to expand while the other remains the same? Is there a way to keep both tables of equal width in any situation? You can view an exa ...

Disable checkboxes upon page initialization

I am working with a form that includes checkboxes. Whenever the page loads, clicking on the checkboxes automatically checks them. However, I am looking for a solution where the checkboxes are disabled or not clickable during the page load process. Once th ...

Ways to access real HTML components within a Chrome extension instead of the initial source code

I am currently developing a Chrome extension, When using Chrome's Inspect Elements panel, all HTML page elements are displayed at the bottom. Is there a way to retrieve all those elements using JavaScript? The original source code of the page does ...

What is the best way to vertically center text within a div?

After successfully centering my text horizontally in my div using text-align: center, I'm now faced with the challenge of centering it vertically. Despite my attempts with vertical-align: middle;, I haven't been able to achieve the desired result ...

Creating a dynamic routerLink value in Angular 8 for items within an ngFor loop

I am currently attempting to route a dynamic value from the NgFor using [routerLink]. <li class="list-group-item d-flex justify-content-between align-items-center" *ngFor="let factors of factorsList"> <span>{{factors | ...

Encoding multiple arrays with JSON and PHP is a powerful feature that allows

I'm working on encoding my multiple news array into JSON using PHP. Here's what my news array looks like: $news[1]["header"] = "First header"; $news[1]["content"] = "First news content"; $news[2]["header"] = "Second header"; $news[2]["content"] ...

Can you please explain how to reverse the deletion on both ends just like I demonstrated in my video using VS Code?

Watch a brief video detailing my issue Despite uninstalling all extensions and resetting settings to default, the problem persists. Could I be missing something? ...

The iteration count and stop code in CSS are failing to function correctly

Is there a way to make this loop once and then stay as is unless the page is refreshed? I've tried multiple methods I came across. Can you modify the code so I can see where the changes are being made? The goal is to have it display once and then per ...

Updates to the Tailwind file are not appearing on the page after reopening the file

Every time I reopen the project, the file tailwind properties do not get rebuilt and if I add new properties like text-color, they are also not reflected. Any suggestions on how to solve this issue? I need the project to update and take in new properties ...