Guide: How to Align Center Horizontal Menu and Its Submenu

Can someone help me with centering this navigation menu and submenu horizontally? I've tried adjusting the width of the navigation list and setting margin to 0 auto, but it's not working for me. I also want to maintain the background color of the navigation bar throughout the entire width of the page.

Here is my HTML:

<div class="slide-down-page">
    <ul id="nav">
    <li id= "bio"><a class="active btn" href="home.html">Bio</a></li>
    <li id= "portfolio"><a href="portfolio.html">Portfolio</a>
        <ul>
            <li><a href="#">Writing</a>
            </li>
            <li><a href="#">Illustrations</a></li>
            <li><a href="#">Design</a></li>
        </ul> 
    </li>
    <li id= "contact"><a href="contact.html">Contact</a></li>
    </ul>
</div>

This is my CSS:

#nav{
margin: 0 ; padding: 0;
position: fixed;
width: 100%;
background:#e7e7e7;
}

#nav li {
list-style: none;
display: inline-block;
float: left;
}

#nav li a{
padding: 1em 2em;
display: inline-block;
text-decoration: none;
color: #F2583E;
font-family: 'Helvetica Neue';
font-size:1.25em;
background: #e7e7e7;
text-align: center;
margin: 0 auto;
}

#nav li a:hover{
background-color: #C1DAD6;
}

#nav a.active, #nav li:hover a.active{
background-color: #F2583E;
color: #F7F3E8;
cursor: default;
}

/*SubMenu*/
#nav li ul{
display: none;
}

#nav li:hover ul{
display: block;
position: absolute;
top: 63px;
width: 100%
left:0px;
background: #C1DAD6;
color:white;
}

#nav li:hover ul li, #nav li:hover ul li a{
display: inline-block;
float: left;
padding: 5px;
color:white;
background: #C1DAD6;
}

#nav li ul li:hover a{
color: #77bed2;
}

#nav li ul li{ 
 _display: inline-block; /* for IE5*/
 }

#nav li ul li a{
width: 150px;
}

Answer №1

If you're looking to achieve this using CSS, here's the step-by-step method:

1. Remove "float: left;" from "#nav li" and "#nav li:hover ul li, #nav li:hover ul li a"

2. Add "text-align: center;" to "#nav"

3. Fix the issue with "width: 100%" of "#nav li:hover ul" by adding ";" after "width: 100%"

4. Include "padding: 0;" for "#nav li:hover ul"

Here is the updated CSS code:

#nav{
margin: 0 ; padding: 0;
position: fixed;
width: 100%;
background:#e7e7e7;
text-align:center;
}

#nav li {
list-style: none;
display: inline-block;
}
#nav li a{
padding: 1em 2em;
display: inline-block;
text-decoration: none;
color: #F2583E;
font-family: 'Helvetica Neue';
font-size:1.25em;
background: #e7e7e7;
text-align: center;
margin: 0 auto;
}

#nav li a:hover{
background-color: #C1DAD6;
}

#nav a.active, #nav li:hover a.active{
background-color: #F2583E;
color: #F7F3E8;
cursor: default;
}

/* SubMenu */
#nav li ul{
display: none;
}

#nav li:hover ul{
display: block;
position: absolute;
top: 63px;
width: 100%;
padding: 0;
left:0px;
background: #C1DAD6;
color:white;
}

#nav li:hover ul li, #nav li:hover ul li a{
display: inline-block;
padding: 5px;
color:white;
background: #C1DAD6;
}

#nav li ul li:hover a{
color: #77bed2;
}

#nav li ul li{ 
 _display: inline-block; /* for IE5*/
 }

#nav li ul li a{
width: 150px;
}

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

Having trouble toggling between the trending and search features on giphy website

I've been developing a chat application with NextJS and I'm currently working on integrating GIPHY images into it. Although I have the basic setup in place, I'm facing issues when switching between the giphy.search() and giphy.trending() fu ...

Ensuring a top margin is applied to a flexbox item of undetermined height

Having a flexbox container with two dynamic content columns, it becomes challenging to adjust the height of the right column without affecting the alignment. Setting a top margin does not yield the desired result as the bottom remains aligned with the left ...

Can you guide me on how to replicate this for an additional 16 times? Apologies for the confusion

I need help with a code that can apply to multiple names at once, rather than one by one each time. Here's the example HTML code: <div id="one" class="text-container"> <span> Campfire Cooking in Another World wi ...

The disappearance of the Request Body in the NuxtJS ServerMiddleware Express API

I've been working on my ExpressJS and NuxtJS skills lately. I've been trying to receive data from an axios POST request, but for some reason, the req.body and req.params always turn up empty. Below are the configurations and code snippets that I& ...

Enable scrolling for divs on mobile devices

My filter layout is a customized HTML and CSS template based on Bootstrap. Here is an example of the code: a:hover { color: #444; text-decoration: underline; } /* Rest of the CSS styling */ I ...

Retrieving data from external domains using jQuery AJAX

I'm having some trouble with this code. For some reason, the AJAX call is failing and I'm not getting any response back. Any ideas on why this might be happening? $.ajax({ type: "GET", dataType:"html", url: "http://www.example.com ...

jQuery UI - Add character right before the final element

My slider is set to range from 10 to 1000 with increments of 20 (10-30-50, etc). I'm looking to add a character before the value on the last step (1000). Currently, it shows values like "560 kr/h" or "400 kr/h", but I want it to display ">1000 kr ...

The default margin of an HTML element is set to fill the width of its containing div and cannot be overridden

As a budding web developer, I'm encountering a basic issue that has me scratching my head. Any assistance would be highly appreciated: Regardless of the width I specify for elements within a particular div container, both Safari and Chrome seem to au ...

Ways to retrieve textStatus beyond ajax boundaries

Do you know how to access the textStatus variable after an ajax call? I need to use this variable in an if condition. Can anyone provide assistance? $this->registerJs("colorArray = ['#ff4c4c','#32CD32']; $('.grooveTable&apo ...

Automatically insert a page break after every set of n items to make printing easier

When printing a web page with a list of items, sometimes the items end up appearing garbled across multiple pages. Is there a method to add a CSS class definition after a certain number of items have been added on a page? The CSS in question would be asso ...

The importance of version numbers in Bower usage

While working on bower.json, it's common to specify version numbers in your dependencies. A scenario often encountered is: { ... "devDependencies" : { "grunt" : "~0.3.13", } } So, what does the ~ signify in this context? Why not simp ...

Performing a function when the ondrop event of a <li> element is triggered

Is there a way to trigger a code-behind click function on the ondrop event of an <li> element? Additionally, I would like to know if it's possible to force a postback on the onclick event. Any ideas on how to achieve this? Just to clarify, thi ...

What is the best way to call an Angular component function from a global function, ensuring compatibility with IE11?

Currently, I am facing a challenge while integrating the Mastercard payment gateway api into an Angular-based application. The api requires a callback for success and error handling, which is passed through the data-error and data-success attributes of the ...

Error in sending data to the server via the specified URL: "Http failure response for http://localhost/post.php: 0 Unknown Error" and POST request to http://localhost/post.php failed with error code

Feeling a bit stuck trying to add data to my database. As a junior with PHP and Angular, I am using PHP via XAMPP and Angular 8. Is it possible to create separate files for the post and get methods in the PHP file? app.component.ts import { Component, O ...

Displaying a background image in Laravel using the storage directory

Can anyone offer some guidance on how to load a background image from storage? I have tried using the following code with normal img src, but it doesn't seem to work with background images. background-image: url({{ Storage::get("{$post->image}") } ...

Having trouble utilizing Reactjs Pagination to navigate through the data

I'm currently working on implementing pagination for a list of 50 records, but I'm encountering an issue. Even though I have the code below, it only displays 10 records and I'm unaware of how to show the next set of 10 records until all 50 a ...

Use jQuery's fadeIn() method for all browsers except IE

Upon the page loading, I am administering a fade-in effect to some text using jQuery. Unfortunately, the opacity transition doesn't appear very smooth for text in IE 8 and earlier versions, so I am looking to eliminate it. Is there a method by which ...

Issue encountered when attempting to populate input values in a form after retrieving data from the backend

Currently, I am developing a form that will submit user data to the backend using AJAX upon hitting the submit button. After fetching additional values from the backend through AJAX, I need to redirect back to the frontend. My goal is to retrieve these val ...

Blender Mesh Not Visible in Three.js

After creating a mesh in Blender, I attempted to use it in three.js. Although the file is being loaded according to the event log, all I see is a black screen. How can I ensure that the mesh actually appears on the screen? import * as THREE from 'thre ...

Tips for delaying form submission until the xhmhttprequest finishes

I am working on a Flask Web App that utilizes Boostrap and plain JavaScript. The challenge I am facing is related to making an XMLHttpRequest to upload a file to s3 when a form is submitted. However, the issue arises because the page reloads before the xhr ...