How to use CSS float to align list items with different heights seamlessly

I am attempting to create two columns with a floated list. Each li element has a different height, and I want to achieve this layout without large spacing gaps.

Here is the closest solution I have reached:

http://jsfiddle.net/kR94q/1/

HTML

Please note that I do not have br tags in my actual code. They are only used here to demonstrate varying heights of content.

<ul>
    <li><br /><br /><br /></li>
    <li><br /><br /></li>
    <li><br /><br /><br /><br /></li>
    <li><br /></li>
    <li><br /><br /><br /></li>
    <li><br /><br /></li>
</ul>

CSS

ul { width: 300px; margin: 0 auto; border: 1px solid black; list-style: none; padding: 0; }
ul li { width: 138px; margin: 1px 5px; border: 1px solid #d8d8d8; background-color: blue; float: left;}
ul li:nth-child(2n+1) { float: right; }

Answer №1

If you're unable to use two <ul> elements due to constraints in the current html rendering, you might want to consider using the column-count property in CSS3 as a quick solution!

ul { 
    /* your existing css */
    -moz-column-count:2;
    -webkit-column-count:2;
    column-count:2;
}

In addition, I eliminated the float property and :nth-child(2n+1) selector to maintain alignment of the columns with this approach.

You can view the updated version of your fiddle here: Fiddle

Answer №2

Have you considered using the flex display?

ul { width: 300px; 
    height: 250px;
    margin: 0 auto; border: 1px solid black; list-style: none; padding: 0; 
    overflow: hidden;
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
}

Check out this fiddle example

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 best way to expand the Bootstrap container's width to the maximum size?

I'm having an issue with implementing a DataTable within a Bootstrap container. When viewing the website at maximum width, the table container adds a scrollbar and cuts off the last column in the table. Attempted solutions such as using a `container- ...

Validation with Javascript can trigger the display of a hidden element

Hello there, I could really use some assistance with the JavaScript part of this code. I have two JavaScript functions: one for validating if a radio checkbox is selected, and another for revealing the "answer". Currently, an alert pops up if no selections ...

What is the best way to populate data with Angular framework in my specific situation?

I'm currently working with Angular and attempting to implement a pagination feature. Here is what I have so far: Angular controller appModule.controller('testCtrl', ['$scope', function ($scope) { $scope.numbers = 7; }]) html ...

Creating a stylish border for a div element

Having trouble with styling a border around a div box. I'm struggling to find a way to prevent the borders from looking like this: Let me show you an example of what I'm dealing with: .num.num_1 { border-left-color: #0D2431; } .num { wid ...

CSS - Implementing Below Element Validation Message in MVC 3

Hello everyone, Currently, I am looking for a way to show input validation messages below the element instead of beside it. The default CSS file adds a margin-bottom of 19px to the <input /> element, so I need to adjust this positioning in order to ...

Importing a substantial number of records from an XML file into an HTML table

Currently, I am working on a project where I am reading records from an XML file and dynamically displaying them in an HTML table using JavaScript's push method. Everything works smoothly when the number of records is below 1000. However, when the num ...

What is the process of uploading a video and showcasing it on an HTML page?

I have successfully uploaded images and displayed them on an HTML page. Now, I am looking to do the same for videos. Here is my current code: $('#addPhotosBtn').click(function() { $(this).parents().find('#addPhotosInput').click(); }) ...

"Using jQuery to append a new div after the last div that matches a specified class on

I am looking to dynamically add a new div element at the end of the last match on click. <form id="form"> <div class="border item-block"> <p><b>Color :</b> silver </p> <input type= ...

Use jQuery to display the user input value in real-time

I am in the process of developing a dynamic calculation program to sharpen my jQuery skills, but unfortunately, I'm facing some challenges. I have managed to store values in variables as shown in the code snippet below. <form> Table of: ...

Organizing posts in a sub directory within the _posts folder and ordering them by time using Jekyll

Currently, I am utilizing the mmistakes/minimal-mistakes theme built with jekyll to create my github pages. The goal is to display all of my posts located in the _posts/android directory within the _pages/android.html file. Specifically, I want to include ...

Using Angular 4 for HTML 5 Drag and Drop functionality

I have been working on integrating native HTML 5 drag & drop functionality into my angular application. While I have successfully implemented the drag and dragOver events, I am facing an issue with the drop event not firing as expected. Here is the snipp ...

How can I prevent my website layout from adjusting when the window is resized?

I want my website to remain the same regardless of window resizing, ensuring that no elements on the page shift or change size. Currently, my image and buttons are moving and resizing with the window, which I don't want. * { box-sizing: border-bo ...

Challenges with Adjusting Background Opacity in CSS

My text has a white background with an opacity of .3, but it's affecting the foreground due to CSS repositioning. Even though the HTML is in a different division, I struggle with certain aspects of CSS and would appreciate guidance from those more exp ...

Why doesn't the Kellum method for CSS image replacement work with button elements?

Recently, I discovered that the kellum method doesn't work as effectively with HTML button elements. Specifically, it does work but requires additional text indent. To sum it up, here is the technique: text-indent: 100%; white-space: nowrap; overflow ...

Store information during each iteration

Below is a snippet of my JavaScript code: for (var i in data){ var trans_no = data[i]; var transno = trans_no.transno; var transdate = trans_no.transdate; var dropno = trans_no.drop; var cusname ...

What is the best method for centering text input within an Angular Material toolbar?

Can anyone help me with aligning input elements with buttons on an Angular Material toolbar? Check out the code pen: http://codepen.io/curtwagner1984/pen/amgdXj Here is the HTML code: <md-toolbar class="md-hue-1" layout-align="start center" style="mi ...

Tips for preventing text from changing its padding within a div when reducing the width and height dimensions

I am facing an issue with a div inside another div. The child div contains text, and when I apply animation to decrease the width and height, the text inside gets reset according to the new size. While I understand why this happens, I want to find a way to ...

When removing a specific option from a dropdown menu, the default selection is chosen instead of the

So I have a select element that initially had multiple options selected. After I manipulated it with my code, only one option remains selected, but because I removed the previous selections, the default option is now being shown instead of the next selecte ...

What is the best way to determine the value of margin-left in inline CSS using jQuery?

Here is the code snippet I am working with: <div class="ongoing" style="width: +728px; margin-left: +12480px"> I need to extract the value of the margin-left property for scrolling purposes. The technique I used to retrieve the width value does no ...

Angular: Display an element above and in relation to a button

Before I dive in, let me just mention that I have searched extensively for a solution to my problem without much luck. The issue is describing exactly what I'm trying to accomplish in a way that yields relevant search results. If you're interest ...