align menu at the center of the 960 grid

After delving into the world of 960 grid (http://960.gs/), I found myself facing an issue with an old-style menu that I had used years ago. Somehow, with the 960 grid system, the menu is floating left when I want it centered.

 ul#menu {
width:940px;
height:61px;
background: url(../images/menu_bg.png) no-repeat;
list-style:none;
padding-top:0;
padding-left:0;
margin: 0;
}

ul#menu li {
float:left;
}

ul#menu li a {
background: url(../images/menu_splice_color.png) no-repeat scroll top left;
display:block;
height:61px;
position:relative;
}

ul#menu li a.shel {
width:135px;
}

ul#menu li a.golf {
width:84px;
background-position:-135px 0px;
}
ul#menu li a.pro {
width:119px;
background-position:-219px 0px;
}
ul#menu li a.event {
width:94px;
background-position:-338px 0px;
}
ul#menu li a.member {
width:148px;
background-position:-432px 0px;
}
ul#menu li a.bistro {
width:91px;
background-position:-580px 0px;
}
ul#menu li a.contact {
width:115px;
background-position:-671px 0px;
}

ul#menu li a span {
    background: url(../images/menu_splice_color.png) no-repeat scroll bottom left;
    display:block;
    position:absolute;
    top:0;
    left:0px;
    height:100%;
    width:100%;
    z-index:100;
}

ul#menu li a.shel span {
background-position:0px -61px;
}

ul#menu li a.golf span {
background-position:-135px -61px;
}
ul#menu li a.pro span {
background-position:-219px -61px;
}
ul#menu li a.events span {
background-position:-338px -61px;
}
ul#menu li a.member span {
background-position:-432px -61px;
}
ul#menu li a.bistro span {
background-position:-580px -61px;
}
ul#menu li a.contact span {
background-position:-672px -61px;
}

My HTML markup is generic and looks like this:

<div class="container_16">



    <!-- Navigation Start -->
    <div class="grid_16">
        <ul id="menu">
            <li><a href="#" class="shel"><span></span></a></li>
            <li><a href="#" class="golf"><span></span></a></li>
            <li><a href="#" class="pro"><span></span></a></li>
            <li><a href="#" class="events"><span></span></a></li>
            <li><a href="#" class="member"><span></span></a></li>
            <li><a href="#" class="bistro"><span></span></a></li>
            <li><a href="#" class="contact"><span></span></a></li>
        </ul>
    </div>
    <div class="clear"></div>

</div>

To add some flair, I decided to incorporate jQuery animations for hovering over images.

    $(function() {

    $("ul#menu span").css("opacity","0");

    $("ul#menu span").hover(function () {

        $(this).stop().animate({
            opacity: 1
        }, "slow");
    },

    function () {

        $(this).stop().animate({
            opacity: 0
        }, "slow");
    });
});

Answer №1

Enclose the menu within a container and specify its positioning as position: relative. Next, use absolute positioning to place the menu at 50% from the left edge with margin-left: -(outerWidth/2).

You can achieve this like so:

<div id="wrapper">
    <ul id="menu">
    ...
    </ul>
</div>

CSS:

#wrapper { position: relative; }

JavaScript:

var $menu = $('#menu'),
    menuWidth = $menu.outerWidth();

$menu.css({
    position: 'absolute',
    left: '50%',
    margin-left: -(menuWidth/2)
});

Answer №2

If you want your menu items to be centered instead of floating them to the left, you can simply define them as display:inline-block. To achieve this, apply the text-align:center property to the container of your menu, like shown below:

CSS

.container_16 {
    text-align: center;
}

ul#menu li {
    display: inline-block;
    *display:inline; /* ie7 hack, since it doesn't support inline-block */
}

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

Tips for clear a single input tag in a form using only html5

After attempting the example below, my goal is to achieve the functionality where pressing the reset button will only reset the field in the current line. Is this achievable using only HTML5 without jQuery/JavaScript? Any suggestions or ideas are welcome ...

Having trouble with my ajax success function, could use some assistance please

Having issues passing values from an AJAX request to a PHP file. The error function seems to be working, but I can't figure out what's wrong with my code. Here is what I have: <script> function test(){ //alert("hello"); var myD ...

"Displaying HTML content in an EditText on Android: A Step-by-Step

Hey everyone, I am facing an issue with trying to convert a string into HTML and display it in the EditText Control. Unfortunately, it is not working correctly. The problem seems to be that the span tags are not being recognized as HTML elements. <sp ...

obtain the present date using JavaScript

I am currently utilizing the Datetimepicker developed by XDAN. My goal is to have the current date set as the default when the page loads. To achieve this, I attempted using the new Date() along with the getUTCFullYear functions. However, there's a ...

Is it possible for jQuery to retrieve numerical values in a similar way to PHP's (int) function

Is there a method in jQuery that can filter out only numbers, similar to how PHP's (int) function works for getting numbers? For example, if I have text that includes both strings and numbers: <div class='selector'>(4)</div> $ ...

JavaScript is having trouble locating the HTML select element

Having trouble selecting the HTML select element with JavaScript? You're not alone. Despite trying different solutions found online, like waiting for the window to fully load: window.onload = function(){ var opt = document.getElementsByName("prod ...

What is the best method for inserting the HTML content from a specific div into a textarea?

Everything seems to be working fine, but once I insert the HTML into the textarea, an issue arises where the div gets wrapped within another div, causing the layout to break. var urls = []; $('body').on('click', '.btn_video&apos ...

Issue with Displaying Local Server Image in Angular 2 HTML

I am facing an issue with my Angular 2 Application. It retrieves items from a local database where the server stores the image of the item and the database stores the path to that image stored on the server. While I can retrieve all the items without any p ...

The significance of the "$=" or "?=" symbols in lit-element illustrations

I'm struggling to comprehend the purpose of ?= or $= in these two instances: First Example: Lit-Element README <div id="box" class$="${this.uppercase ? 'uppercase' : ''}"> <slot>Hello World</slot> </div> ...

Simple ways to increase the scalability of the jQuery .click function

I'm struggling to make this work properly. I have multiple anchor tags on a page that use scrollTop for their animation. Here's an example of the HTML: link <a id="TOS" href="#Svc">What are your terms of service?</a> and the target ...

What causes the variation in default margin between React and traditional HTML?

There seems to be a difference in margin between <button> elements when rendered by React compared to plain HTML. Plain HTML adds a small margin between the buttons, while React removes it completely. Does anyone have an explanation for this discrep ...

jquery error encountered when attempting to submit a form

I want to create a form that includes error messages like the following: <div class="box-content"> <?php $properties = array('class' => 'form-horizontal', 'id' => 'form1'); echo form_open("contro ...

Tips for injecting animation into a DIV

I am trying to make a <div> element on my webpage expand to the full width and height of the screen. While I have managed to achieve this, I also want to implement an animation that will be displayed when the <div> enlarges to fit the screen si ...

Tips for managing callbacks from a RESTful service API on IOS

Utilizing an API to enable users to register an account on my application. Currently, I have managed to craft the necessary URL in objective-C to submit the data, with the API documentation detailing the return codes that will indicate the outcome. My qu ...

I've been attempting to adjust the currentTime of an HTML5 video element within a React JS environment, but for some reason it keeps reverting

Need help updating the currentTime of an HTML5 video element with a slider in React JS. My issue is that whenever I adjust the slider, the value resets to 0. The problem seems to be related to the handleChange function which updates the videoRef.current.cu ...

Looking to Access Client-Side XML Files with HTML5 in Google Chrome?

I am currently facing an issue while trying to load a local XML file and parse its content. I have made some attempts at writing the necessary code, but I seem to be stuck at this point. Could someone kindly offer their assistance in guiding me further w ...

Still having trouble getting @font-face to work with Firefox and htaccess

I've been struggling to load my custom font in Firefox despite numerous attempts. Here is the @font-face property code I have placed in the head section: @font-face { font-family: 'MeanTimeMedium'; src: url('http://sweetbacklove.com ...

Pan motion gesture above HTML components

Is it possible to create a hovering effect over elements in a container using a pan gesture? https://i.sstatic.net/E6G56.gif Here's the HTML code: <div class="container"> <div class="item"></div> <div class="item"></div ...

Retrieving information from a PHP server using AJAX

Searching for a way to retrieve the posts created by users and load more posts upon user's request. Encountering an Unexpected end of JSON input error when initiating an ajax request in the console. Javascript $("#ajax_load_more").click(function ...

Why is React App showing up twice on the webpage?

After successfully creating a React app based on Free Code Camp's Drum Machine project that passed all tests on Code Pen, I encountered an issue when transferring the code to Visual Studio. Surprisingly, the app now fails one test (#6) even though it ...