Ways to ensure that the ul navigation bar spans the entire bottom of the page evenly (without resorting to using table

This question has been previously raised on Stack Overflow with an insightful answer provided. However, I am encountering a specific problem in implementing it.

My objective is to have a navigation bar (nav consisting of ul and its li items) occupy the entire width, or 80% of the width, evenly divided between items.

The primary goal is to center the navigation bar and ensure that each item occupies equal space. A solution proposed on Stack Overflow involves using the content: "" hack after the ul. Although this works well for single-word li items, it causes alignment issues with two-word items as shown in the screenshot.

If there isn't a straightforward way to eliminate this issue, are there any hacks available to reduce the extra spacing? I've created a jsFiddle here, although it may not fully depict the extent of the problem.

Thank you for your help.

HTML:

<article class="main home">
    <img class="heroLogo" src="media/TM-Logo-4.png">
        <nav>
            <div class="menuIcon"></div>
            <ul>
<li><a href="http://www.google.com">One</a></li>
<li><a href="http://www.google.com">One</a></li>
<li><a href="http://www.google.com">One</a></li>
<li><a href="http://www.google.com">Two longWords</a></li>
<li><a href="http://www.google.com">One</a></li>
<li><a href="http://www.google.com">One</a></li>
<li><a href="http://www.google.com">One</a></li>
            </ul>
        </nav>
</article>

CSS:

.menuIcon {
display: none;
}

nav, nav:hover {
width: 80%;
position: fixed;
left: 0;
right: 0;
bottom: 0;
margin: 0 auto;
font-size: 1.2em;
text-align: center;
}

nav ul, nav:hover ul, nav ul.clicked, nav ul.none {
width: 100%;
display: block;
text-align: center;
text-align: justify;
}

nav ul:after {
content: '';
display: inline-block;
width: 100%;
}

nav li {
width: 100%;
display: inline;
padding: 0;
margin: 0;
}

Answer №1

If you want to streamline your CSS, consider utilizing display: inline-block. This property can be quite handy, especially when combined with setting a percentage width for the <li> elements. Here's an example that may work for you (assuming you know the number of list items you will have).

CSS

.menuIcon {display: none}
nav {width: 100%; position: fixed; left: 0; bottom: 0; font-size: 1.2em}
nav ul {text-align: center; padding: 0; width: 80%; margin: 0 auto}
nav li {display: inline-block; width: 14%; margin: 0}

HTML

<article class="main home">
  <img class="heroLogo" src="media/TM-Logo-4.png">
  <nav>
    <div class="menuIcon"></div>
    <ul>
      <li><a href="http://www.google.com">One</a></li>
      <li><a href="http://www.google.com">One</a></li>
      <li><a href="http://www.google.com">One</a></li>
      <li><a href="http://www.google.com">Two longWords</a></li>
      <li><a href="http://www.google.com">One</a></li>
      <li><a href="http://www.google.com">One</a></li>
      <li><a href="http://www.google.com">One</a></li>
    </ul>
  </nav>
</article>

Answer №2

Upon reviewing the code, it appears that the solution is quite straightforward: Removing "text-align: justify;" should likely resolve the issue.

A more detailed explanation would involve discussing how setting width: 100% will cause the LI elements to expand and allow the text to fully justify within the designated space...but alas, I lack the motivation to elaborate further at this time.

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

An element generated using a JavaScript loop is covering another element in the layout

I am facing an issue with positioning images within a div to a span. The problem arises as the images are overlapping each other and I am uncertain about how to properly place each image when it is added. Below is the code snippet: The CSS: <style ty ...

UI binder is having difficulty resolving CSS

After creating a search panel for my application using UI binder, I noticed that the desired behavior is not being achieved. Ui.xml <g:HTMLPanel> <c:SimpleContainer> <c:InfoContainerHeader text="{labels.searchFilter}" /> ...

Error Message "Alexa.create is not a valid function" encountered while using the Alexa HTML Web API on the Echo Show 10

Here is the HTML code for my custom Alexa Skill. <head> <script src="https://cdn.myalexaskills.com/latest/alexa-html.js"> </script> </head> <body> var alexaClient; Alexa.create({version: '1.0'}) .t ...

CSS gallery slideshow with images that smoothly fade-in and fade-out at a specified location

In the image below, at position 4 (marked by yellow circle) from the left where picture 6 is displayed, I want a cross-fade (fade-in/fade-out) gallery of images to take place. Specifically at position 4, I am looking for a cross-fade (fade-in/fade-out) ef ...

How can I align the kendoMenu in the center when the menu width is unpredictable?

Presenting a modified menu structure: <html class="k-webkit k-webkit40"> <head> <title>Site Title</title> <!-- js and css files referenced here --> </head> <body> <link h ...

The font size calculation is larger than the CSS definition on the Asus Nexus 7 device

Running the website on an Asus Nexus 7 (developed in jQueryMobile), I set the font size in CSS to be 14px. However, while debugging using Chrome on my PC, I noticed that the computed size is actually 22px. Here's a snippet of the HTML code: <div ...

Struggling to implement a sidebar in HTML using jQuery and running into issues?

I am struggling to create a template that includes a navbar, sidebar, and other elements that can be used across multiple HTML files. Despite trying different approaches, including changing the jQuery version and downloading jQuery, I am unable to make it ...

Having issues getting the single div to refresh with `.#div.load(scriptpage.php)` command

I've been attempting to update the content of a specific div (in-game chat) every X seconds or minutes using this simple code. The issue I'm facing is that the content never refreshes or loads the new page. I've searched through various cod ...

Parsing all HTML elements using Nokogiri

I've been searching everywhere and all I could find was how to use CSS selection with Nokogiri. What I really need is to completely remove all HTML tags. For instance, this: <html> <head><title>My webpage</title></head& ...

What is the best way to position an image behind textboxes?

Need help with moving an image behind textboxes and a sign-in button? Check out my issue: https://i.stack.imgur.com/cGoEU.png The image is currently covering the login form. How can I position it properly? Here's the code snippet causing the issue: ...

When using HTML5's checkValidity method, it may return a valid result even if

Consider the following scenario: <input pattern="[a-z]"/> If you run the command below without entering any input: document.querySelector('input').checkValidity() You will notice that it actually returns true. This seems counterintuiti ...

Using an Image to Directly Link to Your Own Video Hosting Site

I'm encountering an issue where clicking on an image link to a hosted video results in the browser prompting to download the video instead of playing it directly. I've tried adjusting between absolute and relative paths in my code, but it hasn&ap ...

Python script for extracting content from web pages that are loaded dynamically

I am facing an issue with extracting content from a webpage on my website. Despite trying to use selenium and clicking buttons, I have not been successful. #!/usr/bin/env python from contextlib import closing from selenium.webdriver import Firefox import ...

Activate the mouseenter event as soon as an animated element makes contact with the cursor

As I venture into the world of web development, I am currently working on creating a game where the main objective is to avoid touching moving circles with the cursor. My approach involved using a mouseenter event as shown in the JSFiddle example below: $ ...

Replacing CSS conditional statements with jQuery

My CSS code is set to hide the #global-widget-base tag and display a tab #aside-expander when the browser width is less than 1200px. @media screen and (max-width: 1200px) { aside#global-widget-base { display: none !important; } #aside- ...

Synchronize two slideshows in a cycle with timed delays between each cycle

Is there a way to add a sequential delay between slideshows in the synchronized slide show example from cycle2 API? For example, having each div.cycle-slideshow start at 5s intervals (5s, 10s, 15s, 20s...). The cycle would then repeat with the first div st ...

What are some techniques for styling a field when the div id is not specified?

I need to customize a data field within a table, but I am unable to locate or identify its div ID. Here is the page source: <tbody> <tr> <td style="font-size:12px; text-align:center;" name=""> <div sty ...

Having trouble importing the name 'RadioChoiceInput' from the 'django.forms.widgets' module during the migration from Django 1.19 to 3.2

Currently, I am facing an issue while upgrading a Django widget.py file from version 1.19 to version 3.2. The error message I encountered is: from django.forms.widgets import RadioSelect, RadioChoiceInput ImportError: cannot import name 'RadioChoiceI ...

Use $.ajax to display the menu by capturing an array of elements {0}

Whenever I click on one of the DIV elements, a menu pops out on the side displaying more information about the clicked element. I can determine which element I'm clicking on, but I'm struggling to pass that information from jQuery to the PHP pag ...

Nested Flexbox Elements

Looking to nest elements using flexbox in order to achieve the following layout: https://i.sstatic.net/bEKps.png Does anyone know how to make this happen? ...