Place li elements in the center of ul

Looking at the title, it seems that I am facing some challenges in centering my li elements within a ul element. Despite experimenting with various solutions like fixed width + inline-block, table and table-cell, I still can't seem to make it work.

In my scenario, there is a button and a ul placed next to each other as both are set to

display: inline;

Any suggestions?

<html>
    <head>
        <title>2i3</title>
        <style>
        * {
            font-family: Arial, Calibri, sans-serif;
        }
        #button {
            background-color: red;
            border: soild red 2px;
            border-radius: 200px;
            color: white;
            font-size: 20px;
            margin: 20px;
            display: inline-block;
            padding: 0px;
            width: 50px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            cursor: pointer;
        }
        #button:hover {
            background-color: #830d1e;
            box-shadow: 5px 5px 3px #888888;
        }
        #menu {
            list-style-type: none;
            text-align: center;
            padding: 0px;
            margin: 0px;
            display: inline;
        }
        #menu li {
            border: solid red 2px;
            border-radius: 100px;
            padding: 15px;
            width: 150px;
            display: inline-block;          
            color: blue;
            text-align: center;
            font-weight: bold;
            cursor: pointer;
        }
        #menu li:hover {
            background-color: yellow;
            text-decoration: underline;
        }
        </style>
    </head>
    <body>
        <div id="button">O</div>
        <ul id="menu">
            <li>Guo Yulong</li>
            <li>B</li>
            <li>C</li>
            <li>D</li>
        </ul>
    </body>
</html>

EDIT: Apologies for any confusion, what I actually want is to have equal margins set using auto between the li elements.

Answer №1

Take out the display:inline property from #menu and instead include text-align:center;

See Demo Fiddle Here

This adjustment instructs the ul element to center any text content, since the li elements are set to display:inline-block, they will be treated as textual content and centered accordingly. If you want your button to appear beside the menu, there are several options available, with the 'easiest' being to apply a float:left to it.

There are essentially two primary methods for centering content:

  1. Text Alignment (inline elements): Configure the child elements to have either display:inline or display:inline-block, then set the parent container to text-align:center. The children will be centered within the parent depending on its width.

  2. Margin Alignment (block elements): For block-level child content (display:block), set text:align:center; on the parent, and then provide the child with margin:0 auto and a fixed width that is less than that of the parent.

Answer №2

Learning HTML and CSS

<div class="menu-container">
    <ul id="menu" class="menu">
         <li>John Doe</li>
         <li>E</li>
         <li>F</li>
         <li>G</li>
    </ul>
</div>

Styling with CSS

.menu-container {text-align: center;}
.menu-container ul {margin: 0px auto; display: 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

Looking at an HTML string in its natural HTML form

In my AngularJS app, I have autogenerated HTML code that highlights certain texts. The specific line of code responsible for generating this HTML looks like this: var replaced = original.replace(regEx, '<span style="background-color: red;">&apo ...

Iterating over JSON data in Jquery based on key-value pairs

Hey everyone, I received a response from the server below. I attempted to iterate through it based on keys but unfortunately, I couldn't succeed. {"metricsLevelList": [{ "levelName": "Account1Name", "levelId": 1, "metrics": [ { "value ...

What could be the reason for the Javascript function failing to run?

I need assistance in executing a function called "send()" which includes an AJAX request. This function is located in ajax.js (included in the code snippet) The Ajax success updates the src attribute of my image. The function seems to be working correctly ...

The Organization of Tabs in the AngularJS ui-select Component

As a newcomer to angularjs, I am currently working on a web application using the ui-select library for select2-style dropdowns. While developing my forms with appropriate tabindex values, I noticed a user experience issue with tab ordering. When tabbing ...

Does my SQL function correctly return the desired outcome, or does it simply return the initial entry in the column?

I am currently developing a website and facing an issue with checking and displaying a user's level. I have a function that retrieves the user_lvl from a database as an integer value. Upon login, this function is called and the session variable for th ...

PHP form enables users to send messages using their own email address

Lately, I've encountered some issues with my PHP contact form. It has been functioning perfectly for the past two years without any changes made to it; therefore, I'm puzzled as to what could be causing the problem. Here is the code snippet: < ...

A vertical divider within an ion-item collection

Currently, I am using Ionic and HTML to implement a vertical line within an ion-item in an ion-list. The desired outcome is to have something similar to this (the colored line after 'all-day'): I attempted the following approach: <ion-list&g ...

What is the best way to handle the changing relative image URL in CSS minification?

The Issue Managing a large website with a deep CSS folder structure can present challenges when it comes to relative URLs for background images. Minifying CSS files before deployment can cause issues with these relative URLs due to the flattening of the f ...

The jCapSLide Jquery Plugin is experiencing compatibility issues in Chrome browser

While this JQuery plugin works perfectly in Internet Explorer and Firefox, it seems to be malfunctioning in Chrome. The plugin is not being recognized at all by Chrome, and the captions are appearing below the image instead of on top with a sliding effect. ...

ASP.NET MVC is unable to locate the resource inside the Content folder

Trying to incorporate a trackCues.vtt file stored in my solution into my view has been a challenge. I attempted to load this resource following this code: <video style="margin-top:30px;margin-bottom:30px" width="800" height="600" controls> <s ...

Use javascript to automatically remove an element upon page load

Here is a snippet of code: <a id="sdffg" href="/allgemeinen/me-libe-love-y/"><p class="erfg" class="quotes"> bla bla bla </p></a> I need help deleting the a element using JavaScript: <script> window.onload=function( ...

What is the method in JavaScript to retrieve the current date with millisecond precision?

Is there a way in JavaScript to obtain the current date and time down to milliseconds? When using the new Date() method, it returns the time up to seconds like this: console.log(new Date()) LOGS - Thu Sep 07 2017 14:47:37 GMT+0530 (India Standard Time) W ...

How to align several DIVs within a container with an unspecified size

I have several blue rectangles with known dimensions (180 x 180 px) within a container of unknown size. <html> <head> <style> @import url('http://getbootstrap.com/dist/css/bootstrap.css&a ...

What steps can I take to stop the datepicker from altering the value of the previous row?

I have implemented a datepicker function using React Bootstrap for each table row. The issue I am facing is that when I change the date in the first row, it correctly updates. However, when I try to change the date in the second, third, and subsequent rows ...

Is it possible to highlight only a specific color on the div background?

I'm trying to create a layout similar to this: https://i.sstatic.net/dZ1ka.png I've managed to get most of it working, but I'm struggling with the yellow rectangle that spans the background of the screen. Currently, it looks like this: https ...

Achieving class toggling between elements using event.target

I'm currently working on a JavaScript app project and I'm looking to add classes to my navigation links using the event.target property. I have two different functions that should toggle the class names between the two navigation links. For examp ...

Submitting a POST request to a Node.js Server is causing a prolonged loading time, leading to an error message of

Struggling with a simple form that sends user input to the server and encountering ERR_EMPTY_RESPONSE after submission? Don't worry, I can successfully log the input from the form using console.log, confirming that it is reaching the server. Here is ...

Timing issue with the animation callback

I have been experimenting with creating a bounce effect on my custom scroller by utilizing the translate3d method for scrolling. I managed to successfully implement it, but now I am facing an issue where if you continuously scroll out of bounds (try double ...

How can I create a custom elevation for a Vuetify v-menu?

I'm currently working with vuetify and v-menu as outlined in the official documentation here https://vuetifyjs.com/en/components/menus/ I'm struggling to figure out how to apply elevation only on the bottom left and right corners. When I add a ...

Is it possible to selectively process assets based on their type using Gulp and Useref?

Is it possible to selectively process css and js assets using gulp-useref based on the build type specified in the html tags? <!-- build:<type>(alternate search path) <path> --> ... HTML Markup, list of script / link tags. <!-- endbui ...