What is the best way to evenly distribute elements in a variable-width parent container across multiple lines?

I have a UL on my website with 30 list items, each containing an icon that is 130px wide, causing the list to span across multiple rows.

It's hard to explain the issue, so here's a JSFiddle example.

    ul { 
       display: table; margin: 10px auto; 
    }

    li { 
       float: left; list-style: none; margin-left: 5px; padding: 5px 0; 
    }
    li a { 
       background: #82B5DA; 
       border: 1px solid #599CCE; 
       border-radius: 3px; 
       padding: 5px; color: #333; 
       text-decoration: none; 
}

The goal is to make the spacing between list items dynamic and ensure that the rightmost item touches the edge of the screen instead of leaving a gap until there is enough space for a new item. When there is enough room for another item on the row, the width between the items should reset.

Essentially, transform this:

|[aaaaaaaaa][bbbbbbbbbb][cc]       |
|[dddddddddddddddddddbb][eeeee]    |

Into this:

| [aaaaaaaaa]  [bbbbbbbbbb]  [cc] |
| [dddddddddddddddddddbb]  [eeeee]|

I hope that clarifies things. I'm new to posting here, so apologies if there are any mistakes.

Answer №1

If you're looking to achieve a specific layout, consider using flexbox:

http://css-tricks.com/snippets/css/a-guide-to-flexbox/

https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes

Here's an example:

ul {
    display: flex;
    margin: 10px auto;
    padding: 0 10px;
    flex-wrap: wrap;
    justify-content: space-between;
}

Check out the demo: http://jsfiddle.net/5grwG/19/

Keep in mind, browser support for flexbox is not universal yet (http://caniuse.com/flexbox). It may be necessary to use a polyfill or limit support to modern browsers only.

Answer №2

From what I understand, the only way to accomplish this is by using javascript.

You might want to consider utilizing the Isotope plugin, which has a compressed version that's just around 15kb in size.

Check out the Isotope plugin here

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

Is there a way to trigger a fabric.js event externally?

Is there a way to trigger a fabric.js event externally? For example, if I have a 3D model that tracks the mouse's position and provides the UV coordinates on the canvas, I want to be able to emit click, mousemove, and mouseup events. Currently, everyt ...

I am having trouble getting my website to work offline using caching

My cache file is displayed below: CACHE MANIFEST # 2013-11-22 14:38:54735779 CACHE: ../../../../assets/img/background_01.jpg ../../../../assets/img/background_02.jpg ../../../../assets/img/background_03.jpg ../../../../assets/img/datepicker_icon.png .. ...

The list in my "Hamburger" menu isn't loading properly

I have been attempting to add a "Hamburger" style menu to my webpage specifically for the "md" size. Although the hamburger menu displays, clicking on it does not trigger any action. I experimented with removing some classes from certain tags, but most of ...

Use CSS to create boxes of a specific size within a table cell that spans the entire height

I'm having trouble using CSS to create two boxes (one red and one green) inside a table cell. I can't seem to make them fill the entire height of the cell. Here is what I have tried so far: td { background-color: lightblue; padding: 5px; ...

Guide on assigning a value to an input field using Vuejs

<div class="col-md-8"> <va-input label="Address 1" v-model="Address1" id="address" class="inp"> </va-input> </div> After calling the API to retrieve data, I need to populate the above input field wit ...

Hyperlinks are malfunctioning and the text cannot be highlighted

Here is my XML code: <!DOCTYPE HTML SYSTEM> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"></link> </head> <body> <div class="header"> <img id="circle" src="circle.png" ...

unassigned variables in a PHP email submission form

My PHP email form is not sending emails because my variables are coming up as null. I have an echo statement after my code to check if the variables have values, but they are not being printed. Only 'done' and 'email $to' are showing up ...

I find myself with just one button in my javascript function, but I actually need two

I'm currently working on a class assignment where I'm trying to create a javascript function that uses a button to display the value on a slider component. However, I'm running into an issue where only one button appears instead of two, even ...

CSS tricks: Make triangles stand out with unique hover effects

I find myself in a bit of a conundrum! I want to incorporate cursor: pointer into my CSS, but the issue is that it's for a triangle shape. If I were to use the following code: #triangleholder { width: 100px; height: 100px ...

Having trouble with making a responsive flip animation in jQuery

I've been attempting to implement a responsive flip card animation using the resources at , but I'm encountering an issue. The back side of the card is set to 100% width, and when I try to adjust it to 33.333%, the layout gets disrupted. If anyo ...

Error with ThreeJs OrbitControls.js functionality not operating as expected

Despite my efforts to utilize OrbitControls.js from ThreeJs by creating a folder and saving necessary files (such as GLTFLoader.js, OrbitControls.js), I encountered errors when trying to use functions within them. This led to frustrations like the one show ...

Tool to track character limits in multiple text fields

I'm facing a challenge with a form that includes 3 text areas, a copy button, and a reset button. My goal is to concatenate all the characters in the text areas to get a sum which will be displayed next to the copy/reset button. The character limit is ...

What is the best way to bind data to a textarea component and keep it updated?

I started using VueJS just a week ago for a new project. During this time, I have successfully created two components: * Account.vue (Parent) <!--This snippet is just a small part of the code--> <e-textarea title="Additional Information" ...

What can be done to resolve the issue of text not displaying correctly on the screen?

I am attempting to position text to the right of a card header in my project, but for some reason the working example on fiddle is not working. I am using Bootstrap 4. Check out the example here. Here is what I am seeing in my app: https://i.sstatic.net ...

Design for a mobile webpage layout featuring a carousel-style format

Recently, I've been experimenting with creating a mobile application using PhoneGap and I have some questions about defining the layout. I am trying to create a single-page layout with side scrolling, similar to a carousel image gallery, but instead ...

Issue with Bootstrap v3.3.6 Dropdown Functionality

previewCan someone help me figure out why my Bootstrap dropdown menu is not working correctly? I recently downloaded Bootstrap to create a custom design, and while the carousel is functioning properly, when I click on the dropdown button, the dropdown-menu ...

creating a table with only two td's for formatting

My ultimate goal is to keep my web form code as minimal as possible for easier maintenance. I am currently working on creating a questionnaire and have been using two td elements for this purpose. Here is an example of what the current text structure looks ...

Creating a nested structure using JQuery with dynamically generated divs and select tags

My current task involves creating forms and generating corresponding div elements, but my main concern lies elsewhere. What I am seeking guidance on is how to establish a hierarchy among these dynamically generated div elements using the <select> ta ...

The font size on my website fluctuates up and down every time I refresh the page or navigate to a different one

I am currently in the process of updating my research lab's website using a Jekyll framework with Bootstrap. However, I have encountered an issue where the font size grows slightly whenever I navigate to a new page or refresh the current one, before r ...

Unable to adjust the main-container height in Bootstrap to 100%

I'm struggling to set the height of my .main-container to 100%. When I try to do that, the height ends up being too high. Here is a screenshot for reference: enter image description here .main-container { height: 94vh; } Here is some of my code ...