What is the best way to center align my horizontal subnav?

I am currently working on a horizontal navbar with horizontal submenus. One issue I am facing is getting the elements in mysubnav to align centrally instead of being pushed to the left all the time.

If you need a visual representation of what I mean, please check out this jsfiddle.

Thank you all for taking the time to help with this! I am open to any suggestions, whether it involves CSS or JavaScript.

http://jsfiddle.net/gbkdd7gg/2/

HTML

<div id="menu">
    <ul id="navbar">
        <li><a href="#">Occasions</a>

            <ul>
                <li><a href="#">Valentine's Day</a>
                </li>
                <li><a href="#">Easter</a>
                </li>
                <li><a href="#">Mother's Day</a>
                </li>
                <li><a href="#">Father's Day</a>
                </li>
                <li><a href="#">Wedding</a>
                </li>
                <li><a href="#">Birthday</a>
                </li>
                <li><a href="#">Baby</a>
                </li>
            </ul>
        </li>
        <li><a href="#">Cards &amp; Invitations</a>

            <ul>
                <li><a href="#">Invitations</a>
                </li>
                <li><a href="#">Baby</a>
                </li>
                <li><a href="#">Weddings</a>
                </li>
                <li><a href="#">Announcements</a>
                </li>
                <li><a href="#">Holiday</a>
                </li>
            </ul>
        </li>
        <li><a href="#">Canvas Prints</a>

            <ul>
                <li><a href="#">Full Wrap</a>
                </li>
                <li><a href="#">Colour Edge Wrap</a>
                </li>
                <li><a href="#">Frame</a>
                </li>
            </ul>
        </li>
        <li><a href="#">Mugs</a>

            <ul>
                <li><a href="#">11oz Single Image Mug</a>
                </li>
                <li><a href="#">11oz Double Image Mug</a>
                </li>
                <li><a href="#">11oz Scrapbook Mug</a>
                </li>
                <li><a href="#">11oz Fullwrap Mug</a>
                </li>
                <li><a href="#">14oz Travel Mug</a>
                </li>
                <li><a href="#">17oz Latte Mug</a>
                </li>
            </ul>
        </li>
        <li><a href="#">Device Cases</a>

            <ul>
                <li><a href="#">Smartphones</a>
                </li>
                <li><a href="#">Tablets</a>
                </li>
                <li><a href="#">iPods</a>
                </li>
            </ul>
        </li>
        <li><a href="#">Photo Gifts</a>

            <ul>
                <li><a href="#">Keychains</a>
                </li>
                <li><a href="#">Mousepads</a>
                </li>
                <li><a href="#">Playing Cards</a>
                </li>
                <li><a href="#">Puzzles</a>
                </li>
                <li><a href="#">T-Shirts</a>
                </li>
            </ul>
        </li>
    </ul>
</div>

CSS

#menu {
    position: relative;
}
#navbar {
    position: absolute;
    margin: 0;
    padding: 0;
    z-index: 999;
    width: 100%;
    background-color: #F6F6F6;
}
#navbar li {
    list-style: none;
    float: left;
    width: 16.6%;
    background-color: #F6F6F6;
    text-align: center;
    color:#9a9999;
    height:40px;
}
#navbar li a {
    display: block;
    padding: 3px 8px;
    text-transform: uppercase;
    text-decoration: none;
    color: #999;
    font-weight: bold;
}
#navbar li a:hover {
    color: #000;
}
#navbar li ul {
    opacity:0;
    top:0px;
    position:absolute;
    background-color:black;
    z-index: -100;
    left: 0;
    width: 100%;
    -webkit-transition: all 0.2s ease 3s;
    -moz-transition: all 0.2s ease-in-out;
    -ms-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease 1s;
}
#navbar li ul li {
    background-color:black;
    color:white;
    font-size:0.8em;
    width:14.1%;
}
#navbar li ul li a {
    color:white!important;
}
#navbar li:hover ul {
    opacity:1;
    background-color:black;
    position: absolute;
    display: inline;
    top:40px;
    left: 0;
    width: 100%;
    margin: 0;
    padding: 0;
    z-index:-100;
}
#navbar * {
}
@-webkit-keyframes fadein {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}
#navbar li:hover li, #navbar li.hover li {
    float: left;
    background-color:black;
}
#navbar li:hover li a, #navbar li.hover li a {
    color: #FFF;
}
#navbar li li a:hover {
    color: #357;
}

Answer №1

If you're interested in centering a horizontal menu and submenu using CSS, I recommend taking a look at the following discussion on Stack Overflow: "CSS, How do I center horizontal menu and submenu?"

I found Shaun's answer to be quite helpful. Make sure to check out his jsfiddle for a practical demonstration.

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

What is the priority of the asset pipeline's execution order?

What is the priority of asset pipeline? I am trying to replace a part of ace.css style with user.css.scss. The stylesheet file ace.css in the vendor folder should be overridden by user.css.scss. However, it did not work as expected even when I included ...

``There seems to be a problem regarding the alignment of elements in

Having some trouble with centering the grid on my simple website. As a beginner in Bootstrap, I'm seeking guidance. Will be grateful for any help you can offer! Please bear with me as I learn. <html> <head> <meta charset="utf8"/& ...

Implement a timeout feature for npm clickhouse queries

Is there a way to set the timeout for query execution in the following code snippet? I attempted to use the timeout property, but it does not seem to be functioning as expected. const results = await clickhouse.query(query, { timeout: 50, }).toPromise(); ...

Loading of iframes occurs only after receiving the content sent through the postMessage function

I have a scenario where an iframe is used to receive information through the contentWindow.postMessage function in order to log in to a page that I am creating. However, I am facing an issue where the page loads before the contentWindow.postMessage message ...

How is it possible for TypeScript to enable the importing of dependencies that it ultimately cannot utilize during runtime?

Take a look at my sample project by following this link: https://github.com/DanKaplanSES/typescript-stub-examples/tree/JavaScript-import-invalid I have developed a file named main.ts: import uuid from "uuid"; console.log(uuid.v4()); While type ...

Leverage a JSON document to deactivate certain dates within the JQuery UI-Datepicker

As a novice apprentice delving into the world of programming, my knowledge of J-Query and JavaScript is limited. Nonetheless, I am embarking on a new project where users will be booking days using a datepicker. My aim is to ensure that all UK Bank Holidays ...

Combine and emphasize several gridview rows into a single highlighted unit

Imagine you have a gridview that looks like this: FAMILY GROUP COLOR =============================================== | | Poodle | Blue ROW1 | DOG | German Shepherd | Red | | Pitbul ...

Incorporating an external JSX file into an HTML page in a React project

I have a React code in my js/script.js file. My HTML page's head tag is structured like this: <head> <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06746367657246373328302834" ...

Encountered a SyntaxError on JSON Web Tokens Node JS Server: Unexpected token } found in JSON at position 24

Me, along with others, have encountered this issue: SyntaxError: Unexpected token } in JSON at position 24 at JSON.parse (<anonymous>) while following a tutorial on JSON Web Tokens (TUTORIAL LINK: https://www.youtube.com/watch?v=mbsmsi7l3r4&t=34s ...

Issue with Jquery's .change event not being activated

I'm having trouble with this script. I want to retrieve the selected value(s) from a multiple selection dropdown, but it doesn't seem to be working. Can anyone point out what's missing? Thank you! <script type="text/javascript" src="js/ ...

Difficulty integrating Bootstrap with JavaScript file in Visual Studio Code

I am attempting to incorporate a dynamic progress bar by utilizing Bootstrap's progressbar and creating a custom JavaScript file. The goal is to continuously adjust the width of the progress bar at regular intervals to give the appearance of a moving ...

Create a new canvas for each iteration of a Vue.JS template loop

I'm having trouble figuring out how to initialize a local "signaturePad" canvas for each loop or required signature. Additionally, I am interested in binding "dataURL" to signaturePad.toDataURL("image/jpg"). This means displaying the dataURI for ever ...

The program encountered an error while trying to access the undefined property '_header'

An issue arises when the app.use(express.static("web")) line is executed. var express = require('express')(); var app = express(); var http = require('http').Server(app); var io = require('socket.io')(http); //app.get(&apo ...

MongoDB and Node.js encounter unexpected outcomes due to undefined variables

I am trying to retrieve data from my collection called students within the pool database in MongoDB. Despite having a successful connection to the database, when I use console.log(result.lastname), it returns undefined. Below is an excerpt from my server ...

Accessing CSS content from a bundle within the code of an ASP.NET MVC website

I have a website that utilizes bootstrap along with a customized bootstrap theme. The website is designed to send notification emails to its users upon certain actions being triggered. For these notification emails, I want the content to be in well-format ...

Utilizing a GLTF asset as the main Scene element in a Three.js project

I'm struggling with incorporating a gltf model as the main scene in Three.js. Specifically, I have a gltf model of an apartment that I want to load from inside and not outside the apartment. I also need the controls to work seamlessly within the apart ...

Resetting text in an input field using CSS

Here is the fiddle link for reference: http://jsfiddle.net/a765q/1/. I noticed that when I click the reset button, the text color changes to grey. Any ideas on how to fix this? ...

Try implementing jQuery to switch out an href link

Is there a way to replace a specific link using jQuery without impacting other links in the navigation menu? <ul class="navigation" <li class="level0 nav-1 level-top first parent"> <a href="mylink.com/whats-new.html" class="level- ...

Using html as a template parameter in django

When passing HTML to my template, I am using the following code: passDict["problemText"] = <p> Some html</p> return render(response,'main/base.html',passDict). However, when trying to display {{problemText}} in my HTML file, I only se ...

Hiding an HTML image element by setting its display to none will prevent it from being visible if later changed to block display

I am currently developing a web-based game that can be played on both desktop computers and mobile devices. However, for the optimal experience on mobile devices, players need to rotate their device to landscape mode. To prompt users to rotate their devic ...