What is causing the <a> elements not to align in the center of my <li> navigation items?

I'm in the process of building a website with a mobile-first approach. Right now, I'm focusing on styling the navigation bar, which consists of a ul containing five li elements and an a element within each li. In the mobile layout, my goal is to have the navigation perfectly centered. While the nav element and the li elements seem to be centered, the a elements are not – they appear skewed to the right. How can I fix this issue?

Below is the HTML code:

<nav>
    <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="programs.html">Programs</a></li>
        <li><a href="about.html">About Us</a></li>
        <li><a href="why.html">Why</a></li>
        <li><a href="contact.html">Contact Us</a></li>
    </ul>
</nav>

And here is the CSS code:

nav {
    width: 15%;
    margin: 0 auto;
    padding-top: 0.5em;
}

nav ul {
     list-style-type: none;
}

nav li {
    max-width: 100%;
    margin: 1em;
    text-align: center;
    border-radius: 10px;
}

nav a {
    display: inline-block;
    width: 100%;
    margin: 0 auto;
    padding: 0.5em;
    color: inherit;
    text-decoration: none;
}

Here's an image showing how the nav looks in the Chrome browser:

Answer №1

Adjust the list items' margin and padding to 0;

Answer №2

You can include the code snippet below either inline or in an external style sheet for the nav a element:

margin: 0px;
text-align: center;

Answer №3

Give this a shot:

header nav {
    display: flex;
    justify-content: center;
    padding: 0px;
}

nav ul {
    display: block;
    overflow: hidden;
    padding: 0px;
    list-style-type: none;
}

nav a {
    display: block;
    width: 100%;
    margin: 0 auto;
    color: inherit;
    text-decoration: none;
    overflow: hidden;
}

Also, consider using max-width instead of simple width for the <a> tag.

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

How can I display only four decimal places by default within textboxes in HTML?

Looking for assistance with configuring textboxes in a cshtml file. The textboxes are bound to decimal fields in a view model and currently display nine zeros after the decimal point. I would like to limit the display to only four digits by default witho ...

What is the best way to modify the color of a button within an AngularJS function by utilizing the same button?

Currently, I have a function assigned to the ng-click event on a button in order to filter a list. My goal is to change the color of the button once it has been clicked and the filtered list is displayed. I am looking for a way to achieve this within the ...

Is it possible to have a PHP variable embedded within an HTML link tag?

<?php $message = ' <a href="' . $link . '">here</a> '; ?> My goal is to include a clickable link in the $message variable for use in the mail() function. However, despite setting $link with the desired link string, it ...

On mobile devices, the alignment of text in Bootstrap doesn't appear as intended

I'm creating a portfolio website for showcasing my design projects from university. The text in the "my work" section is centered, but it seems misaligned with other elements like buttons when the window width is reduced. What could be the issue? Cod ...

Is there a way to verify if all the div elements are set to display as none?

If all div elements are set to display:none, I want to display a message saying "No result found" element.each(function(i, el) { if (all elements are set to display:none) { no result found } } <div class="a" data-key="a ...

What is the best way to adjust a component to display varying content based on the scenario?

I am looking for a way to reuse a component on my website that displays messages. The challenge is that in one section, the content consists of only a title and paragraph, while in another section, there are multiple titles and content pieces. I have imple ...

Guide to dynamically updating a text file on a live PHP website

Is there a way to display real-time modifications to a text file on my website without the need for a page refresh timer? The current code for the site can be accessed here. if (isset($_GET['enSubmit']) && isset($_GET['uname']) ...

What is the best way to create rounded corners on a WordPress login page?

I recently implemented the Custom Login Page Customizer plugin on my WordPress website and it has created a beautiful login page. While I was able to round the corners of my logo using CSS, I'm struggling to achieve the same effect on the login form. ...

HTML: attempting to align a quote image next to some text within the page

I want to add a quote to my website, but I'm having trouble positioning it correctly. https://example.com/quote-image.png Every time I try to adjust the position of the quote image, it moves when I zoom out. https://example.com/background.png I attem ...

Expand a div in navigation using JQuery

Check out this code snippet: .container { width: 150px; height: 100px; } .test { color: white; background-color: red; width: 100%; height: 25%; transition: height ease 1s; } .test:hover { height: 100%; } <div class="container"> ...

Establish the minimum and maximum values for the text field depending on the selection made in the previous dropdown menu

For my project, I need to create a dropdown menu and a text box positioned next to it. I want the text box to have specific character limits based on the option selected from the dropdown. For example, if "Option 1" is chosen, then the textbox should only ...

Tips for creating a form that adapts to different devices, ensuring it looks great no matter the screen size

<div class="modal" id="myModal" style="width:50%; top:15%;" role="dialog" > <div class=""> <!-- Modal content--> <div class="modal-content"> ...

CSS is broken when transitioning from the development to the live site

After configuring the CSS for this site using CodeIgnitor, I placed it in the specified location: {link rel="stylesheet" type="text/css" href="<? echo base_url();?>application/assets/css/public.css" /> Although Firebug confirms that the link is ...

"Error: The function $(...).autocomplete is not recognized" in conjunction with Oracle Apex

Currently experiencing some frustration trying to solve the issue at hand. The Uncaught TypeError: $(...).autocomplete is not a function error keeps popping up and I have exhausted all resources on Stack Overflow in an attempt to fix it. This problem is oc ...

Instructions for making text stand out on a background image and allowing it to move across the image

I'm looking to create a unique effect where a background image only shows through knockout text and then have that text animate from the top of the page to the bottom, revealing different parts of the background image as it moves. Although I know how ...

Is there a way to make FullPage and Lightbox compatible with each other?

Currently utilizing Fullpage.js for my website, I am looking to incorporate a lightbox in one of the sections. However, upon attempting to load the script and CSS, my fullpage layout breaks and the sections collapse. I have experimented with placing the ...

The importance of white space within email design utilizing table tags in HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html style="opacity: 1;" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv ...

Creating a corner ribbon for a Material UI Card: A step-by-step guide

Can anyone provide pointers on incorporating a corner ribbon to a Material-UI card? Currently, I am utilizing makeStyles for styling from Material UI. ...

Is it possible to update the text within a button when hovering over it in Angular?

https://i.sstatic.net/ZhNeM.png https://i.sstatic.net/kb670.png I'm looking to update the text displayed inside a button when hovering over it, similar to the examples in these images. I have already implemented the active state, but now I just need ...

Transforming a GIMP design into a fully functional webpage

How can I transform my GIMP design into an actual website? After reviewing some information, it appears that using GIMP might not be ideal for creating CSS. It is recommended to utilize a CSS/HTML editor instead. Exporting HTML/CSS with Inkscape or GIMP N ...