What is causing the elements to not be perfectly centered?

I need to display a list of URLs in a single row within a ul element. I attempted to center them on the page using this method, but they ended up slightly off-center.

Here's the HTML code:

<ul id="links">
    <li class="inner-li"><a href="about">about</a></li>
    <li class="inner-li"><a href="http://ahmadalli.net/projects">projects</a></li>
    <li class="inner-li"><a href="http://photo.ahmadalli.net">photoblog</a></li>
    <li class="inner-li"><a href="music">music</a></li>
</ul>

This is the CSS style that I applied:

.inner-li {
    font-size: 1.25em;
    float: left;
    margin-left: 1em;
    list-style-type: none;
}
@media screen and (min-width: 21em) {
    #links {
        width: 21em;
        margin: 0 auto;
    }
}

You can view this layout on JsFiddle.

Answer ā„–1

To enhance the appearance of #links, include the properties display:table and padding:0 as shown below:

@media screen and (min-width: 21em) {
    #links {
        display:table;
        margin: 0 auto;
        padding: 0;
    }
}

In addition, apply the following CSS:

#links > li:first-child{
    margin:0;
}

This adjustment removes unnecessary margin-left from the first li element.

View the code on fiddle

Answer ā„–2

The reason for the lack of centering is due to the utilization of margin-left on the <li> elements.

.inner-li {
 font-size: 1.25em;
 float: left;
 margin-left: 1em; /* this */
 list-style-type: none;
}

This adjustment moves the initial <li> element 1em away from the middle.

To achieve a centered layout, simply switch to using padding: 0 .5em instead.

See a demonstration here

Answer ā„–3

To make your menu responsive, remember to include the following CSS code in your stylesheet:

Ensure that you define the width of your menu and add any additional styles as needed.

#links {
    min-width: 100%;
}

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

Unable to move up and down in my iframe

I am currently working on an Instagram page that is visible through an iframe. Everything seems to be functioning fine on my iMac, but I am facing an issue with scrolling on my iPhone and iPad? After reviewing my code, I couldn't find any hidden over ...

Is there a way to activate the dashed line around buttons when pressing the tab key?

Can someone help me figure out how to display the "dashed line" that shows up when you tab through buttons? I'm not sure which CSS code is responsible for hiding it. Is it something like a:hover or a:active? (I'm assuming this is related to CSS.) ...

Issue with highlighting when one string overlaps with another

I am facing a challenge with handling a string that contains Lorem Ipsum text. I have JSON data that specifies the start and end offsets of certain sections within the text that I need to highlight. The approach I am currently using involves sorting the JS ...

What is causing my Bootstrap navigation bar to not show up correctly?

I followed the Bootstrap 4 tutorials to create a navbar, but for some reason, my code isn't working. I made only a few changes to the nav-brand, so I'm not sure what went wrong. You can see what it looks like here. <nav class="navbar stic ...

Is it achievable to have a background image cover size with a responsive rollover effect?

Iā€™m currently facing a unique challenge where I want to have an image as the background of my website, with the size set to cover the entire screen. On this background image, there are elements like buildings that I want to interact with when hovered ove ...

Having trouble with the tool tip not showing up correctly in Internet Explorer?

Check out the code snippet below: <table> <tr> <td class="bgimg" valign="middle" width="10%" title="some title" nowrap> <img src="images/SomeLogo.gif"/> </td> </tr> </table ...

Obtain a Compilation of Video Sources in an HTML5 Format

Hey there, I am using an HTML5 video. This is just an example <video width="320" id="video" height="240" controls> <source src="video.mp4" type="video/mp4"> <source src="video1.mp4" type="video/mp4"> <source src="movie.ogg" typ ...

PHP-powered webcasting platform

I have a client who is looking for a website that can live stream uploaded videos through search functionality. One of their main requests is to cover and record certain weekly events on camera, with the video being visible live on the website. While I am ...

Exploring the power of positive lookahead in CSS styling

When working with Perl regex, I understand how positive lookahead works - such as q(?=u) matching a q that is followed by a u without including the u in the match. Now, I am interested in finding something similar in CSS: specifically, I am looking to sele ...

Angular Material List Component with Material Table

In this code snippet, everything seems to be functioning perfectly. When you click on the magnifying glass icon, some additional information is displayed: <mat-card *ngIf="(bills && bills.length > 0) && all" style="overflow-x: auto;" ...

FAQs about utilizing percentage width and margins in CSS with Bootstrap

Currently, I am working on a mobile responsive web design using Twitter Bootstrap 3, and I am facing challenges with margins and resizing. I previously asked about creating a mobile-first responsive web design along with responsive images, which you can f ...

Conceal image description while hovering and reveal overlay text

Navigating image captions: I'm looking to hide the title caption on hover and display overlay text instead. However, when hovering over the image, both the title and overlay text disappear. Where am I going wrong with this? If anyone could provide in ...

Learning the ins and outs of HTML5 and CSS3

Does anyone have recommendations for high-quality HTML5 and CSS3 tutorials? ...

Clicking on the icon reveals the current date

I need some help with the input field that displays the calendar date. Currently, I can only click on the input field to show the calendar date. What I actually want is for users to be able to click on a calendar icon to display the calendar date, which sh ...

Update the content of a text area with AJAX requests

Just starting out with AJAX, I'm looking to requery or perform a select *from on my database when a button is clicked in order to change the content of a textarea. How can I achieve this using AJAX? Below is the code snippet I have so far: <text ...

Tips for incorporating three background images within one CSS file

Here's a snippet of my navigation code: <? if($page == ""){ ?> <li> <a href="<? echo $site;?>" id="on" style="background-image:url(images/logonav_onright.png) right no-repeat;">Home</a> </li> ...

Ways to deselect checkboxes and eliminate validation requirements

Below is the HTML code snippet that I am working with: <div class="form-group"> <div class="input-group"> <label for="daterange-vacancy" class="input-group-addon">Van</label> ...

Achieve Full Height with Bootstrap 4!

I have gone through numerous threads on this issue and attempted all the suggestions, but nothing seemed to work for me. Here is the code I am currently using: <div class="container-fluid d-flex flex-column" style="height: 1 ...

Tailored design - Personalize interlocking elements

I am currently working on a custom theme and I am trying to adjust the font size of Menu items. In order to achieve this, I have identified the following elements in the tree: ul (MuiMenu-list) MuiListItem-root MuiListItemText-root If I want to modify th ...

CSS - uniform sizing for border images

INQUIRY: I am looking for a way to maintain a consistent border image size when resizing a div. I have considered using pseudo elements, but I'm wondering if there is a simpler solution? HTML: <div id="resizable" class="ui-widget-content"> & ...