Inline-block list with consistent spacing between each item

I have a list of five items that are displayed inline. The challenge is to align the text in the first item with the left side and the text in the last item with the right side, while maintaining equal spacing between all items relative to both edges.

After searching, I came across a solution provided at this link: http://jsfiddle.net/Cerebrl/Wt4hP/, which was mentioned in this thread: A Space between Inline-Block List Items

However, the provided solution didn't work for me as it only aligned everything to the left. Below is an example code snippet available on jsfiddle here: http://jsfiddle.net/phacer/uV9s9/

Do you have any ideas? I am open to using JavaScript or jQuery solutions if there are any available.

​<ul>
    <li>first item</li>
    <li>second item</li>
    <li>3 item</li>
    <li>fourth item</li>
    <li class='last'>5 item</li>
​</ul>​​​​​​​​​​​​​​​​​​​​​​​​​​​​

CSS:

ul { width: 650px; }
ul li { width: 130px; position: relative; display: inline-block; float: left; }
ul li.last { float: right; text-align: right; }​

Answer №1

To adjust the layout, consider setting the width using percentages rather than pixels.

ul { width: 650px; }
ul li { width: 20%; position: relative; display: inline-block; float: left; }
ul li.last { float: right; text-align: right; }​

Answer №2

If you constantly have the same quantity of elements and container width, you may want to consider this approach:

<ul>
  <li class='first'>first item</li>
  <li>second item</li>
  <li>3 item</li>
  <li>fourth item</li>
  <li class='last'>5 item</li>
</ul>

ul { width: 650px; }
ul li { width: 130px; position: relative; display: inline-block; float: left; text-align: center; }
ul li.last { float: right; text-align: right; }
ul li.first {text-align: left; }

http://jsfiddle.net/x96eN/1/

This setup fulfills your requirements. There might be a slight unevenness in the middle due to different text alignments. To address this issue, you could:

  • Align all text to the left in each li element and make the container box visible for visual balance.
  • Adjust the widths of the first, last, and central lis to minimize the effect.
  • Utilize padding to manually adjust the text position in various li elements.
  • Develop a generic case in JQuery for aligning any number of li elements. If you anticipate needing more or fewer than 5 items or modifying the total width, this solution will come in handy - although using JS solely for layout is not ideal.

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

Javascript problem: Trouble with event.clientX position

I found a great resource on learning javascript at this website, I'm having trouble understanding the code snippets in function XY(e, v), specifically these two lines: event.clientX + document.documentElement.scrollLeft and event.clientY + document ...

Failure to validate two dates, even when they are both in date format within AngularJS

Although it may seem silly to ask, I am confused as to why this is not working. Even though all the values appear fine in debug mode. My goal is to display an error if the productionStartFrom date is before the current date. Controller scope.currentDate ...

Tips for arranging three images in a single row

I have 3 payment logos in my footer that I want to center and align in one row. Can someone help me with this? sliki { text-align: justify; } sliki img { display: inline-block; width: 75px; height: 100px; padding: 7px; margin-top: -5px; ...

Can the <br> tag affect the layout in terms of horizontal space?

I am perplexed by the fact that it displays two lines vertically separated with a br tag differently than two lines vertically separated with the first line acting as a block level tag. For example, see this code snippet: https://jsfiddle.net/qzgeassf/ ...

Using Angular to click and scroll to a component on a web page

Hello, I am attempting to create a basic navigation bar with a few link buttons. I would like it so that when I click on a button, it scrolls to the correct component. I am aware that this can be achieved using jQuery and ScrollTo(), but since my HTML is n ...

Having trouble accessing news feed with jQuery due to an unexpected token error when attempting to cross domains

I am attempting to access the Yahoo News feed from a SharePoint site, but it is causing a cross-domain access issue. Despite trying various solutions found on numerous websites and blogs, I still cannot resolve the problem. (I am executing this code in the ...

Retrieving the length of a Pipe in the parent component using Angular 2

In Angular2, I have created a custom pipe to filter an array based on type ID and year arrays. Here is how it is defined: @Pipe({name: 'highlightedWorksFilter', pure: false}) export class HighlightedWorksFilterPipe implements PipeTransform { tra ...

Connecting multiple knockout ViewModels with a single View

I have created multiple knockout ViewModels and now I am attempting to bind them to a single ViewModel, but it is not functioning correctly! The desired behavior is for it to function like a navigation. Once the LoginModel is completed, it should transiti ...

Conflicting JavaScript between Rails 2 "defaults" and jQuery

Whenever I include both jQuery and Rails' :defaults using the javascript_include_tag, it seems like the latter one is taking precedence over the former. For example, if I include them in this order: <%= javascript_include_tag "jquery-1.4.min.js" ...

Using Javascript to display different div elements based on the type of mobile device

My goal is to use JavaScript to accomplish the following: 1- Check the mobile device type, if it is an ANDROID device, show the ANDROID_USERS div. If it is an IPHONE device, show the IOS_USERS div. 2- Display the above div only once during the first visit ...

Strategies for preventing XML caching using pure JavaScript

Looking for a way to prevent XML caching, I came across a potential solution: How to force browser to reload updated XML file?. However, as a beginner, I found that no concrete example was provided. I suspect that the issue lies in this section of my code ...

Is there a way to specifically style my jquery datepicker widget without affecting other elements on the page?

I have customized my main page using a tab-style widget and a specific stylesheet. Recently, I realized the need for a datepicker in my application. To accommodate this, I included the jquery-ui-style.cs stylesheet to my project. The challenge now is that ...

The validation function in Angular for checking a field against a database is encountering issues and not functioning correctly

Greetings, I have been working on validating whether a user has already participated in an event using AngularJS. Here is the relevant code snippet: HTML <form> <span style="display:none"> <input ng-model="userInfo.user_id"> ...

Issue with horizontal scrolling functionality in DataTables

I have a table with over 10 columns, and I am using datatables to display the data. I have enabled horizontal scrolling due to the large number of columns, but the scroll is not appearing. Can someone please assist me with this issue? Here is a screenshot ...

Adjust the width of content within a wrapper using HTML and CSS to automatically resize

I am facing an issue with a content div that has variable length content arranged horizontally. I want the width of this content div to adjust automatically based on the length of its content. Removing the arbitrary "20%" value from the width property is c ...

Issue encountered during Expo managed app iOS build: "Encountered error: Unable to read property 'transformFile' of undefined."

I'm currently developing a managed expo app and utilizing eas services. When attempting to create an internal distribution build, the process is successful on Android, but on iOS, the build fails with the following error: Metro encountered an error: C ...

Creating interactive text using Three.js and dat.gui

Exploring the possibilities of dynamic rendering user inputted text with three.js and dat.gui, I have developed a code snippet to display the text: var displayGui = function(){ var gui = new dat.GUI(); var parameters = { message:"sample", spin ...

Tips for displaying a div element alongside a button within a reactJS table

How can I position a div element next to a button in a ReactJS table? I have a table component where I want to display some options in a small window that closes when clicked outside. Options : https://i.sstatic.net/sT8Oi.png Action button in the table ...

Using keys with a hyphen (-) in Jquery templates

My JSON array has a key 'BASIS-TYPE'. When I try to use this value in the markup: <html> <head> <script src="/js/jquery-1.7.min.js"></script> <script src="/js/jquery-plugins/jquery.tmpl.min.js"></script> < ...

Filled with challenges in React

Having trouble with loading loaders.css file due to an error. The module build failed because it couldn't find the 'daisyui' module, leading to a stack of require calls: F:\VS code\three js\material tailwind\Clone\ ...