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

What is the method for altering the color of the webkit-slider-thumb using JavaScript?

I am looking to adjust the color of an input range using JavaScript instead of CSS. Can someone help me with this request? Current CSS Code: input[type='range']::-webkit-slider-thumb { -webkit-appearance: none; background: goldenrod !importa ...

Is there a method to measure the time it takes to download a script apart from its actual execution time

I am looking to track timing variables for my primary JavaScript load. One approach could be: <script> performance.mark('script-start') </script> <script src="something.js"></script> Later in something.js, inc ...

Is there a WebKit equivalent to the MDC (Mozilla Documentation Center)?

Experimenting with the newest HTML5 features is rewarding, yet challenging as standards and browser-specific implementations constantly evolve. While Mozilla provides a valuable resource with their MDN Doc Center documenting Gecko changes, I'm curious ...

Invoke a JavaScript function once the div has finished loading

After clicking on a radio button, I am dynamically loading a div element. I want to hide a specific part of the div once it is loaded. $(function($) { $('.div_element').on('load', function() { $('.textbox').hide(); } ...

Find your favorite artist on Spotify through the search function

Recently, I stumbled upon this intriguing demo showcasing how to search for an artist using the Spotify API. However, despite several attempts, I have been unable to make it function properly. Can anyone provide any tips or guidance on making this work suc ...

Apply the "selected" class to the menu located in the common header

I have implemented a menu in the header.php file that will be displayed on all pages such as index.php or contact-us.php. However, I am facing an issue with adding the selected class to the active main category in the menu. The code provided works fine wh ...

What are the steps to resolve issues with my dropdown menu in IE9?

I have a cool CSS built hover-over drop-down menu that I want to add to my website. It works perfectly on all browsers except for IE9. Here is the code and stylesheet I am using: Check out the code and sheet here If anyone has any insights on what might ...

Using Selenium 2 to dynamically insert CSS styles into the currently visible webpage

I experimented with using jQuery on a webpage that has already been modified by jQuery in order to add custom CSS code: jQuery('head').append('<style type=\"text/css\">body { background: #000; }</style>'); When I ...

The problem with clearing floats in IE7 (as well as 6)

Currently, I am facing an issue where I have a list that I want to split into three columns by using the float left property and then clearing the first column. Everything seems to be working fine in IE8 and FF, however, IE7 is causing the second column t ...

The function you are trying to use is not a valid jQuery function

Although I've come across this issue in previous posts, none of the solutions worked for me and it's really frustrating. I'm a newbie to javascript and I'm sure the answer is probably simple. I'm attempting to incorporate the rapt ...

Is it advisable to include cursor:pointer in the pseudo class :hover?

Which option is better to use? .myclass { cursor: pointer; } or .myclass:hover { cursor: pointer; } Is there a notable difference between the two? ...

What is the best way to display the letter X (Clear) in the Chrome INPUT field with the type

A custom application was developed that utilizes an input field of the number type: <input type="number" min="-1000000" max="1000000" step="0.01"> Within Internet Explorer, there is a small "clear" or [X] button on the right side of the input field ...

Tips for enabling the background div to scroll while the modal is being displayed

When the shadow view modal pops up, I still want my background div to remain scrollable. Is there a way to achieve this? .main-wrapper { display: flex; flex-direction: column; flex-wrap: nowrap; width: 100%; height: 100%; overflow-x: hidden; ...

What is the best method for selecting or isolating the code for a single animation created using JavaScript?

Currently, I am attempting to extract the CSS and JS code of an image reveal animation that is part of a static HTML page: https://i.stack.imgur.com/bnmaO.gif The challenge lies in the fact that the desired effect is one among many showcased image animat ...

How do I make a div's height equal to 100% of the height of its parent

I am trying to make a button stick to the bottom of a card no matter how much content is on it. I used the Bootstrap class mt-auto, which worked well. However, when I set the height of all divs to 100%, the row in the card body overflows from the lg breakp ...

Uncovering Twig's Power: Navigating JSON Key Values

In my current project, I am utilizing OctoberCMS and Twig to fetch dynamic data and translations from JSON files for the front end. Here is an example snippet of code: <div class="wrapper items"> <div class="items"> {% for suggestion in ...

Identifying Oversized Files on Safari Mobile: A Guide to Detecting Ignored Large Files in

Mobile browsers such as Safari have a tendency to ignore large files when passed in through the <input type="file">. For example, testing with a 10mb image file results in no trigger of any events - no change, no error, nothing. The user action is si ...

Tips for aligning an element vertically when it has a float using CSS or JavaScript

I am struggling with centering the image within the article list loop I created. The code snippet looks like this: <article class="article <?php if($i%2==0) { echo 'even'; } else { echo 'odd'; } ?>"> <section class ...

I'm having trouble with my fullscreen menu. Is there something I missed when setting it up?

Hi there! I'm currently working on my website and I seem to have run into an issue with the code. I'm still new to coding, so I would appreciate some assistance in identifying where I went wrong. I've been going through the code diligently t ...

FusionCharts Gauges may share similar code, but each one produces a unique output

Currently, I am developing a project that involves creating a dashboard with 3 gauges. These gauges are enclosed in bootstrap cards with a column value set to 4. In the layout of the dashboard, these 3 cards are positioned next to each other. I have succe ...