Aligning images within a horizontal layout

I am facing an issue with my horizontal navigation list, where I am trying to place images/icons above the text in each list item.

Despite all the images being the same size, I am struggling to center them properly within the list items.

Here is the HTML code:

<div id="nav">
    <ul>
        <li><img src="images/kranznav.png" alt="kranz" /><a href="index.php">COMPETE</a></li>
        <li><img src="images/thumbnav.png" alt="thumb" /><a href="index.php">SCORE</a></li>
        <li><img src="images/bagnav.png" alt="bag" /><a href="index.php">SHOP</a></li>
        <li><img src="images/morenav.png" alt="more" /><a href="index.php">MORE</a></li>
    </ul>
</div>
<div class="clear"></div>

And here is the CSS code:

#nav {
    background: #ffffff;
    width: 100%;
    margin-top: -2em;
}

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

#nav li {
    display: inline;
    float: left;
    width: 2%;
    margin-left: 10%;
    margin-right: 10%;
    margin-top: 5%;
}

#nav a {
    display: block;
    width: 20%;
    margin-right: 0 auto;
    padding-left: 0 auto;
    color: #5E09CB;
    text-decoration: none;
}

Answer №1

Please include text-align:center; in the #nav li CSS rule

Answer №2

The issue lies in setting the width of the list items to 2%, which restricts the overall width

Check out this link for a demonstration

Consider this alternative approach:

#nav li {
display:inline;
float:left;
/*width:2%;*/
margin-left:10%;
margin-right:10%;
margin-top:5%;

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

The highlight_row function seems to be delayed, as it does not trigger on the initial onClick but works on subsequent clicks. How can I ensure it works properly right from the first click?

I developed an application using the Google JavaScript Maps API to calculate distances between addresses. I've encountered a bug where the row in the table is not highlighted on the first click, requiring a second click for the highlighting to take ef ...

Attempting to adjust the width of a text animation loaded with jQuery using Textillate, but encountering difficulties

I found a captivating animation on this website: http://codepen.io/jschr/pen/GaJCi Currently, I am integrating it into my project. #content { position: relative; margin-left: auto; margin-right: auto; width: 1000px; height: 700px; } ...

What is the best way to modify tabulator styles?

Is there a way to update the column header in Tabulator using inline style when trying to avoid ellipsis as the column size is reduced? <VueTabulator ref="tabulator" v-model="receipts" :options="options" style="white ...

How do I effortlessly resize an image to achieve a pulsating visual effect using JQuery?

Hey there, I am currently collaborating with a friend on a website project and we have encountered an issue with one of our animations that our client is not satisfied with. This particular animation enlarges and reduces the size of an image repeatedly to ...

Issue with ChartJs version 2.8.0: The custom tooltip remains visible even when clicking outside the chart canvas or moving the mouse pointer

I am utilizing ChartJs to display a line chart with a custom tooltip that appears upon the click event of the data points. The challenge I am facing is that the custom tooltip remains visible even when the mouse pointer is outside the chart canvas area. ...

Utilizing tag keys for inserting text and adjusting text sizes within a Web application

Creating an online editing interface for coursework where keyboard events are used. The goal is to have the tab key insert text, while also reducing the size of the text on that line. However, upon using getElementById, an error message pops up stating: ...

What is the best method to "deactivate" a radio button while ensuring that a screen reader can still read the text and notify the user that it is inactive?

My current situation involves needing to deactivate certain radio buttons, while still having the option to reactivate them later. When I use the disabled attribute, screen readers will overlook this field and miss key information. I am seeking an accessi ...

Why are my images not displaying in Next.js when using Laravel API, even though the paths and configurations are correct? What could be the reason behind

I am currently working on a project that combines Next.js with a Laravel API. One of the functionalities of the project allows users to upload the title, description, and image to the database. Once uploaded, the images are stored in the storage/app/produ ...

Is there a way to deliver information to a specific element on a client's HTML page?

My current project involves using Node.js to serve a webpage that collects user inputs and stores them in a mongodb server. The web page also displays these user inputs. I am trying to determine the best way to pass the user inputs from node.js to the < ...

What causes the change in background when I apply CSS to a div element?

Can anyone explain why the background changes when I style the div element? Here is the code I am using. When I remove the CSS related to the div, the background instantly reverts back to its original state. Any insights would be greatly appreciated. Than ...

Tips on executing a $.get request while submitting a form?

Instead of using AJAX to submit my form, I am trying to implement a progress bar by making GET requests to the server while the form is submitting. This is particularly important when dealing with multiple file uploads that may cause the submission to take ...

Remove dynamically inserted list item using a button

I have dynamically created an unordered list (<ul>) that adds list items (<li>) when a button is clicked, along with a remove button. Initially, the default list item should not contain a remove button so I added it in the script. SCRIPT $(d ...

Position an element to the right within a div using the float property

My product page features a range of attributes that can vary from 1 to 4, each sharing a common fieldset class name. I am attempting to position two of the fieldsets side by side and the remaining ones below them in a similar layout. However, achieving thi ...

What is the best way to align a <div> element below another without being on the same line?

I'm currently working on developing a snake game. My focus right now is figuring out how to make the body parts of the snake follow the head and be positioned after it. <!--Player--> <div class="snake"></div> So here we have the sn ...

How can we add a class to a radio button using jQuery when it is checked, and remove the class when it

I'm looking for help with using jQuery to toggle between two radio buttons and display different divs based on the selection. jQuery(document).ready(function($) { if ($('#user_personal').is(':checked')) { $('.user_co ...

Begin a fresh page within a separate window, proceed to print the contents, and subsequently close the window

I am facing some difficulties with this code. I am attempting to use the "onClick" function to change the image once it is clicked, open a new page in a new window, print the newly opened window, and then close it. The issue I am encountering is that while ...

Enhancing Bootstrap project with personalized CSS styling

Utilizing SCSS to create my CSS has been the approach I've taken. The content of my main.scss file remains consistent even after it is compiled into css. .my-custom-row { background-color: bisque; } Contained within my index.html file is everythi ...

Is it possible to have a page transition when clicking a form button

I've been experimenting with this on my website Everything is functioning well, except when I add data-ftrans="slide" to a form button. For example: <form action"http://google.com"> <button data-ftrans="slide" type="submit" style="h ...

translateZ function fails to function properly on Firefox browser

I've been experimenting with using translateZ to hide a child element called "list" behind the parent element "div2. It works perfectly in Chrome, but unfortunately, it's not working correctly on Firefox. Can anyone provide some guidance or help ...

The functionality of the onclick button input is not functioning properly in the Google

Here is some JavaScript code: function doClick() { alert("clicked"); } Now, take a look at this HTML code: <div > <table border="2" cellspacing="0" cellpadding="0" class="TextFG" style="font-family:Arial; font-size:10pt;"> <tr> <t ...