The li elements in a horizontal menu all have equal spacing between them, filling the width of the ul

I designed a horizontal menu navigation system using HTML elements ul for the main menu container and li for menu list items. The layout is structured with display table for ul and table-cell for li. However, I encountered an issue where there are inconsistencies in the gaps between words of each menu item due to padding set on the li elements. Removing the padding altogether eliminates the gaps, which is not desirable. My goal is to maintain consistent spacing between words even when there's a line break within an li element, while also ensuring responsive design.

https://i.stack.imgur.com/ZcDSR.png

See Demo

CSS Code:

body{
  background-color: #ff2;
  margin: 0px !important;
}
ul { 
    display:table;
    width:100%;
    box-sizing:border-box; 
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    background-color: #F25800;
    padding: 0 10px;
}
li { 
    display:table-cell; 
    text-align:center;
    padding: 10px 12px;
} 
li:hover{
  background-color: #64b448;
}
li a{
  text-decoration:none;
}
li a span{
  font-size: 13px; 
  color: #fff;
}

HTML Code:

<ul>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-rumah.html" class="level-top"><span>Kebutuhan Rumah</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-pribadi.html" class="level-top"><span>Kebutuhan Pribadi</span></a>
   </li>
   // Additional li items...
</ul>

Answer №1

body{
  background-color: #ff2;
  margin: 0px !important;
}
ul { 
    display:flexbox;
    width:100%;
    box-sizing:border-box; 
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    background-color: #F25800;
    padding: 0 10px;
}
li { 
    display:inline-flex; 
    flex-wrap: wrap;
    flex: 1;
    padding: 10px 12px;
    text-align:center;
    align-items: center;
    padding: 10px 12px;
} 
li:hover{
  background-color: #64b448;
}
li a{
  text-decoration:none;
}
li a span{
  font-size: 13px; 
  color: #fff;
}
 
<ul>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-rumah.html" class="level-top"><span>Kebutuhan Rumah</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-pribadi.html" class="level-top"><span>Kebutuhan Pribadi</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/makanan-minuman.html" class="level-top"><span>Makanan & Minuman</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-kantor.html" class="level-top"><span>Kebutuhan Kantor</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/otomotif.html" class="level-top"><span>Otomotif</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/listrik-teknik.html" class="level-top"><span>Listrik & Teknik</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/alat-bangunan.html" class="level-top"><span>Alat Bangunan</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/elektronik.html" class="level-top"><span>Elektronik</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/pakaian.html" class="level-top"><span>Pakaian</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/pecah-belah.html" class="level-top"><span>Pecah Belah</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kesehatan.html" class="level-top"><span>Kesehatan</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/mainan-hobi.html" class="level-top"><span>Mainan & Hobi</span></a>
   </li>
</ul>

Highlighted changes in the code.

  1. Modified ul's display to use `flexbox` property
  2. Adjusted li elements' display to be `inline-flex`, enabled wrapping, and specified sizing with `flex: 1`. Implemented vertical alignment using `align-items: 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

Bringing in a JavaScript file into a Svelte component

Today, I stumbled upon Svelte and I am really intrigued by the concept. However, I encountered an issue while attempting to import a small helper.js file. No matter what I try, whenever I reference the class, I receive the error: ReferenceError: Helper ...

How can I use jQuery to add styling to my menu options?

Looking to customize my menu items: <ul> <li class="static"> <a class="static menu-item" href="/mySites/AboutUs">About Us</a> </li> <li class="static"> <a class="static-menu-item" href="/m ...

Issue with Angular 2 NgFor Pattern Error Message Display Absence

I am attempting to incorporate inputs with a regex requirement within an ngFor loop, but I am not receiving the expected error message when entering something that does not match the required pattern. Even when I input an incorrect pattern, "Test" remains ...

Display the current position of the caret in a field that cannot be edited

Can a virtual caret be displayed between two letter boundaries in HTML/CSS/JavaScript, for example in a regular div without using contenteditable=true? Imagine having the following: <div>Hello world</div> If I were to click between the "w" a ...

Having trouble getting jQuery .append() to behave the way you want?

I have created a code snippet to display a table like this: $("#results").append("<table><tr><th>Name</th><th>Phone Number</th></tr>"); $("#results").append("<tr><td>John</td><td>123123123 ...

Unable to run JavaScript file fetched from cache API

For a web application built using pure vanilla JavaScript without utilizing service workers, I am looking to cache a JavaScript file hosted on an AWS S3 file server explicitly. The script below will be embedded in the index.html file of the application (UR ...

Arrange two input fields side by side if the quantity of input fields is unspecified

I am currently in the process of developing a project using Angular. I have implemented an *ngFor loop to dynamically add input fields based on the data retrieved from the backend. Is there a way I can ensure that two input fields are displayed on the same ...

What is the purpose of using square brackets in a CSS style declaration?

I've recently taken over the management of a website and I came across some unfamiliar CSS syntax. Has anyone encountered this type of syntax before? Can someone explain what the square brackets inside a style represent? Thank you, Rob .trigger-tx ...

Transforming content dynamically

I am currently working on a single-page website that features a comprehensive product catalog. Here are the key elements I'm focusing on: A one-page website layout, complete with header, content, and footer (developed using HTML5/CSS3) Dynamic cont ...

Managing errors within AngularJS in your controller.js file

In my NodeJS + AngularJS single page application, there are fields for inputting text. I need to create a function in controller.js to check if any of the fields are empty so that an error alert can be displayed in the browser. This is my controller.js fi ...

Exploring smooth scrolling functionality using AngularJS and integrating it with IFrames

After implementing an angular controller, I included the following code: angular.element(document).ready(function () { ... } Within this setup, I added a function to enable smooth scrolling to the hash of window.location.hash using .animate({scrollTop... ...

Is it possible to encounter the issue of "Context Lost" and "Importing multiple instances" in Three.js?

I am in the process of developing a 3D editor that allows users to manipulate a 3D scene and then view the result by pressing a "play" button. In order to display the output, I am utilizing an iframe. Below is the snippet of my HTML code: <iframe id=&qu ...

Verify if spacebar is pressed and then use jQuery to add a hashtag with multi-language support

I am attempting to use jQuery to add a hashtag (#) after the user types and presses space. I have created a demonstration on CodePen. In this demo, when you type something like (how are you), the JavaScript code will change it to (#how #are #you). To ach ...

Is there a way to occupy the extra space while working with an inline unordered list?

Using this unique CSS code, I've managed to give my unordered lists a touch of elegance: ul, li { padding: 0px; margin-left: 0px; margin-bottom: 0px; display: inline; } li { border: solid 1px #333333; margin-right: 5px; padding: 2p ...

Decrease the distance between hyperlinks

I understand that this question has been asked numerous times before, but as a beginner in HTML, I still require some assistance. Given the code provided below, how can I decrease the spacing between the links in each column? Code: a { color: white; ...

Leveraging SignalR in conjunction with jQuery to dynamically alter the CSS style of a span

Utilizing SignalR to dynamically update a span's color based on real-time data source. The connection is established successfully, but encountering difficulties with updating the css classes on the span. The issue arises when attempting to update mul ...

Adjust the initial scroll position to - apply overflow-x: scroll - on the specified element

I have an image that can be scrolled to the right on screens that do not fit the full width, but I want the center of the image to be displayed first instead of the left side. Below is my React code: import React, { useEffect, useRef, useState } from &qu ...

Generating a dynamic triangle within a div while ensuring it fits perfectly with the maximum width

I need help with creating a responsive triangle on a web page that takes three inputs. The challenge is to adjust the size of the triangle so it fits inside a div element while maintaining the aspect ratio of the inputs. For instance, if the inputs are 500 ...

Employing HTML and CSS to restrict the number of characters in sentences

Extracting a few sentences from a JSON file and displaying it in a <p> tag. <p> Thank you all for another wonderful night spent together this past Sunday at The Hippodrome in Baltimore. Thank yo... <p> I want to f ...

Discrepancy in CSS rendering in Chrome and Firefox

When viewing in Chrome, the stats display properly within the gray box at the bottom left corner. However, when using Firefox, the stats appear positioned much lower below the box, with the bottom half of them hidden from view. Here is my CSS code being ...