How to Align HTML Menu in the Middle of the Page

I've been struggling to center my menu on the page. I tried setting the display to block and using margin auto for left and right, but it's not working as expected. You can see the issue in this JSFiddle. Any help would be appreciated.

<ul id="menuList">
    <li><a href="index.html">Home</a></li>
    <li><a href="About.html">About</a></li>
    <li><a href="Contact.html">Contact</a></li>
</ul>


#menuList{
    display:block;
    width:100%;
    margin-left:auto;
    margin-right:auto;
}

#menuList ul{
    margin: 0px;
    padding: 0px;
    list-style-type: none;
}

#menuList li
{
    list-style: none;
    float: left;
    margin-right: 0.5em;
}

#menuList a
{
    display: block;
    width: 8em;
    color: black;
    text-decoration: none;
    text-align: center;
}

Answer №1

Change the menu display style to inline-block and center its parent using text-align:center

JS Fiddle Example

Substitute body with the id or class of your desired container element


body {
    text-align:center;
}

#menuContainer {
    display:inline-block;
}

Answer №2

If you want to properly align an element at the center, one effective method is by utilizing the margin: 0 auto; property. It's important to note that the element should have a specific width specified, rather than using a fluid width (such as 100%).

To achieve this, simply include the following CSS:

#sidebar {
    width:320px;
    margin:0 auto;
}

Answer №3

It may not be the best idea to use text-align: center on the body, as Vitorino pointed out.

Instead, consider applying this CSS to the ul element and displaying the menu items inline or inline-block. You can see an example here.

#menuList ul {
    margin: 0px;
    padding: 0px;
    list-style-type: none;
    text-align: center;
}
#menuList li {
    list-style: none;
    display: inline-block;
    margin-right: 0.5em;
}

Answer №4

To adjust the size, you need to modify the width, like so:

#navMenu{
    display:flex;
    width:80%;
    margin-left:auto;
    margin-right:auto;
}

Answer №5

It is important to understand the difference between using display block and floats in CSS. Display block will make elements start on a new line, while floats can keep them on the same line. However, it is not recommended to mix these two properties together as it can cause complications.
If you want your items to stay in line, consider using 'display:inline;'
If you need items to align to the left or right side of their parent element, floats are the way to go. Just be cautious with floats as they can disrupt the layout. There are alternative methods for floating elements without affecting the document flow.
Below is an example of revised CSS:


#menuList{
    display:block;
    width:100%;
    margin: 0 auto;
    text-align:center;
}

#menuList ul{
    margin: 0px;
    padding: 0px;
    list-style-type: none;
}

#menuList li
{
    list-style: none;
    display: inline;
    margin-right: 0.5em;
}

#menuList a
{
    display: inline-block;
    width: 8em;
    color: black;
    text-decoration: none;
    text-align: center;
}

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

Incorporating a new row in JQuery Datatable using an mdata array

I am currently using a datatable that retrieves its data through mData. var processURL="path" $.ajax( { type : "GET", url : processURL, cache : false, dataType : "json", success ...

Unusual occurrences observed with the table

While working with tables, I've noticed an unusual effect. Take a look at the two fiddles below. http://jsfiddle.net/v5co5uy7/ (why are the cells in the left column pushed down?) http://jsfiddle.net/v5co5uy7/1/ (the cells in the left column are posi ...

Ways to align items in the middle of a list

I'm having trouble figuring this out. How can I properly center list items within the ul? body {margin: 0} ul { width: 1000px; margin: 0 auto; padding: 0; list-style-type: none; margin-top: 30px; overflow: hidden; background-color: # ...

What is the best way to ensure the second navbar stays at the top when scrolling, after the first one?

Looking for a solution to create a navbar with two distinct sections for contact info and the menu. The goal is to have the contact info disappear when scrolling down, while the menu remains fixed at the top of the page. When scrolling back up, the menu sh ...

Padding-left in InfoBox

Currently, I am in the process of developing a map using Google Maps API3 along with the InfoBox extension available at this link I have successfully implemented an overall padding to the Infobox using the following code snippet: var infoOptions = { disa ...

Quick CSS tweak

Below is my CSS code: .wrapper { display: inline-block; margin-top: 60px; padding: 15px; width: 100%; } I am looking to create a new class that inherits all the properties from wrapper and only changes the width property: .wrapper .exten ...

Closing the space between vertical edges of table data cells

I'm currently in the process of translating my resume from MS Word to HTML and CSS for easier maintenance and sharing purposes. I really like the layout and style of my resume and want to maintain it. The design features a left column with bold titles ...

Utilizing pop-up info boxes

After thoroughly reviewing the documentation on Imagemapster's website and browsing several posts, I am still struggling to get the tooltips working correctly. I attempted to share my HTML and JS code on jsfiddle.com without success, so I have provide ...

Verify the existence of the linked page's href url

Is there an elegant way to verify if a hyperlink on a website directs to a page that returns an HTML 200 code? I'd like to give users the ability to input their own links in a model, but I also want to block any links leading to a 500 or 404 error pag ...

What is the best way to implement a side navigation bar with 100 tabs using Angular 4?

I am new to learning AngularJS and could use some guidance. Currently, I am working on an Angular 4 SPA project that consists of a sidebar with 100 tabs, each containing different content. Here is an image of the sidebar. I have tried a simple approach by ...

Tips for concealing divs when hovering over them

I have designed a header for my website with the following styles: .header-cont { width:100%; position:fixed; top:0px; } .header { height:50px; background:#F0F0F0; border:1px solid #CCC; width:950px; margin:0px auto; } .hea ...

Choose an element by its specific data attribute

I have come across this html code and I am attempting to assign a new class to it using the data attribute: <p class="form-row form-row-wide" data-child-field="child_has_jacket"> </p> Even after trying with jQuery : jQuery( ...

Ways to Achieve the Following with JavaScript, Cascading Style Sheets, and Hypertext

Can someone help me convert this image into HTML/CSS code? I'm completely lost on how to do this and don't even know where to start searching for answers. Any assistance would be greatly appreciated. Thank you in advance. ...

What is the best method for creating a loop script within a widget using script?

I am trying to implement a loop within a widget, however, the widget contains an internal script: <!DOCTYPE html> <html><head></head><body> <script> list=["AMAR3","BBDC4", "BEEF3", "BPAN4", "BRFS3", "B3SA3", "CVCB3" ...

Is it considered fashionable to utilize HTML5 data attributes in conjunction with JavaScript?

Currently, I am utilizing HTML5 data attributes to save information such as the targeted DOM element and to initialize events using jQuery's delegation method. An example of this would be: <a href="#" data-target="#target" data-action="/update"> ...

Mastering the art of calculating the width for a responsive scroll menu

I found a useful tutorial on building a scrolling menu for smaller screen sizes. You can check it out here. However, I encountered an issue when trying to add a border width element. Whenever I define the width, it overrides the scrolling: auto; element. ...

Discovering the previously dropped value from a droppable div

Currently, I am developing a dynamic formula generator using jQuery's drag and drop functionality. Here is what I have accomplished so far: I have two lists: <ul id="head"> <li class="horizontal">Salary</li> <li class="h ...

monitor the location of a div within a textarea

My question revolves around a textarea that is linked to a draggable div through the following code: $('#content').children().draggable({ drag : function () { $('#textarea').text("left:" +($(this).position( ...

Utilize HTML5 localStorage functionality

I have a good understanding of utilizing HTML5 localStorage with methods like localStorage.getItem/setItem. However, I am currently trying to figure out the implementation for a dynamic page. Let me explain the scenario: On my dynamic page (myPage.jsp), ...

Identify all anchor elements that contain image elements

I am on the hunt. I am looking for a CSS-Selector, but if that's not an option, a jQuery selector would work too. ...