organize items based on the highest height of the div

Looking to organize li based on the maximum height of the div, with additional items automatically moving to the right side.

For example,

-------------
  1   4  7
  2   5  8
  3   6
-------------

Please suggest a more effective method using CSS or JavaScript.

I've tried using the following code snippet but it's not working as expected:


        ul {
          column-count: 2;
          column-gap: 0;
          width: 200px;
        }
        li {
          display: inline-block;
          width: 100px;
          height: 100px;
          background:
            linear-gradient(
              rgba(255,0,0,0.4),
              rgba(255,0,0,0.4)
            ),
            url(http://placekitten.com/g/100/100);
          color: white;
          text-align: center;
          font-size: 50px;
          line-height: 100px;
          font-weight: bold;
          border: 1px solid white;
        }
      
<ul>
         <li>1</li>
         <li>2</li>
         <li>3</li>
         <li>4</li>
         <li>5</li>
         <li>6</li>
       </ul>

Answer №1

According to a resource from CSS tricks on creating Responsive-Friendly CSS Columns:

The recommended approach is to use both column-count and column-width properties simultaneously for better control over CSS columns. You can either define each property separately or utilize the shorthand columns.

When using both properties, it is important to note that column-count determines the maximum number of columns while column-width sets the minimum width for those columns.

However, if you do not have a specific maximum number of columns but rather a maximum height, this traditional method might not yield satisfactory results.


An alternative solution involves leveraging flexbox with its ability to wrap content across multiple lines (or columns in this case), offering more predictable outcomes:

ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  /* additional styling code here such as:
  - flex-flow
  - align-items (vertical alignment)
  - align-content (horizontal alignment)
  based on design preferences
  => https://css-tricks.com/snippets/css/a-guide-to-flexbox/
  */
  max-height: 306px;
  background-color: lightyellow;
}
li {
  width: 100px;
  height: 100px;
  background:
    linear-gradient(
      rgba(255,0,0,0.4),
      rgba(255,0,0,0.4)
    ),
    url(http://placekitten.com/g/100/100);
  color: white;
  text-align: center;
  font-size: 50px;
  line-height: 100px;
  font-weight: bold;
  border: 1px solid white;
  list-style-type: none;
}
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
</ul>

Compatibility: IE10+
For compatibility with older browsers like IE10, consider using Autoprefixer and adapting the code accordingly due to the evolving specifications.

A valuable resource: CSS Tricks guide on flexbox
View the Codepen demonstration here: http://codepen.io/anon/pen/MwJYjP

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 retrieve a HTML element within a control using the store?

Currently, I have built a login page using ReactJS + Flux pattern. However, I am wondering if there is a more efficient way to implement it. This is how my current implementation looks: LoginForm.jsx: //imports..... var LoginForm = React.createClass({ ...

Internet Explorer 9 encountered a JavaScript error: SCRIPT5007. It was unable to access the value of the property 'ui' because the object is null or undefined

My website is functioning properly on Chrome, Firefox, and Internet Explorer 8. However, when it comes to Internet Explorer 9, some strange errors occur just by hovering over elements. SCRIPT5007: Unable to retrieve the value of the property 'ui&ap ...

Could it be that the function is returning undefined because console.log is executing before the result is actually returned? Perhaps a promise

There is a function located in another file that I need to extract the response from and perform an action based on that response before completing my controller function. This is the snippet of code from the external file: exports.counter = function(com ...

Error in Ionic 3: Function Undefined - ReferenceError: The function is not defined on HTMLButtonElement's onclick event

Whenever I click on a button that is supposed to trigger the doLogout function, an error message pops up. The error reads: Runtime Error function not defined - ReferenceError: function is not defined at HTMLButtonElement.onclick This issue is puzzling be ...

Scrolling horizontally with full width CSS styling

Recently, I finished developing a new website, but upon loading it, I noticed there is an unnecessary scroll. I would appreciate any feedback on why this is happening and how I can fix it. I am happy to share the code here, but from past experience, most ...

I'm having trouble launching Clinic.js, any suggestions on what steps I should take next?

When starting up your application, use the following command: clinic doctor --on-port 'autocannon -m POST localhost:8000/users/register' -- node dist/main.js If you need help, check out the Clinic.js Doctor - v9.2.0 log. The clinic doctor tool ...

"Clicking on one item at a time from the list will trigger a reaction in React

Hello, I'm fairly new to programming and currently tackling a challenging exercise that has me a bit stuck. Display 10 posts from the API in the browser - Working Show 3 related comments for each post - Working The issue I'm facing is that whe ...

Managing a fixed footer in an Android webview to prevent it from obstructing an input field when the soft keyboard appears

I am working with a webview that contains a React app, not React Native. The webview features a login page with two input fields and a fixed footer. However, when one of the input fields is focused, the footer moves up and covers part of the input field, ...

Is Three.js application compatible with mobile browsers?

I'm working on an application that relies on the Three.js library. Unfortunately, I've run into an issue where it doesn't seem to work on mobile browsers when using WebGLRender. I've looked at some applications created by Mr.doob () for ...

Guide to populating a select-option dropdown using CSS

I'm working on creating a dropdown menu in HTML that pulls the options from a CSS file. To accomplish this, I have assigned custom classes to each option and specified the option label in the CSS using the CUSTOM_CLASS::after{content: "";} r ...

The server side is experiencing issues with processing the GET request

I am currently working on a basic web server using nodejs and express module. It is supposed to handle both POST and GET requests. The POST request function is functioning correctly, but the GET request is not returning any data. In the console, I am see ...

Retrieve a variable with jQuery and incorporate it into the view in Laravel 5.3

I am utilizing jquery to fetch data from a controller. The controller method returns a $user, yet I am unsure how to utilize it in the view. Below is the snippet of my code: <li> <a href="#suppression" data-toggle="modal" onclick="getForm(&a ...

Mastering jQuery Selectors

<ul id="comment1"> <li>response1</li> <li>response2</li> <li>response3</li> <li>response4</li> <li>response5</li> <li>response6</li> <li>response7</li> <li ...

Sending query parameters from one URL to another using jQuery

Greetings! I have implemented a load more feature using a script that loads additional content on the same page when the 'Load More' button is clicked. Below is the script: <script type="text/javascript"> var track_page = 1; load_c ...

Opening a Partial View (.ascx) in jQuery UI Dialog

I'm a bit confused about how to display a partial view in a jQuery UI Dialog window. I was initially using a simple Dialog but decided to explore other options for various reasons. Most online tutorials only demonstrate opening <div></div> ...

Customizing the primary CSS style of an element without the need to individually reset every property

Is there a way to reset just one property of a selector that has multiple properties stored in main.css, without listing all the properties and setting them to default values? I always struggle with CSS interference and constantly need to reset properties ...

What could be causing certain icons in my jQuery to appear distorted?

My app utilizes jQuery mobile, and everything was functioning properly until I recently noticed that two components do not appear as they should. The first issue is with a selection field that used to display a small arrow but now shows an empty gray circ ...

Utilizing the Bing Translation API to translate an entire webpage

I am currently attempting to use the Bing API to translate an entire webpage instead of using the Bing widget. This is because I want to create a custom design for the translation panel, However, I have been unable to find any resources on how to do this ...

Encountering an error in Next.js with the message 'NextRouter was not mounted'

When attempting to retrieve the input value and redirect to '/search/${searchvalue}', where 'searchvalue' represents the input value, I encountered an error stating 'Error: NextRouter was not mounted'. More information can be ...

trigger content loading upon page load instead of upon click

If I wanted the code to automatically load content.php when the page loads, what modifications would I need to make? $(document).ready(function() { $("#content").load("content.php"); }); ...