Spread out elements equally into columns

I am facing a challenge with arranging items within a single div called .wrap. The goal is to place an unknown number of items (.thing) into four columns, stacked on top of each other.

<div class="wrap">
    <div class="thing"> thing1 </div>
    <div class="thing"> thing2 </div>
    ...
</div>

Creating evenly distributed columns without any empty ones would be straightforward using:

.wrap {
    column-count: 4;
    column-fill: balance;
}

The issue is that the column-fill property only works in Firefox. This leads me to wonder if there is another CSS technique that can achieve this layout across all modern browsers. Would flexbox be a viable option?

It's important to mention that I cannot add additional divs for columns and I prefer not to use a JavaScript solution.

Check out this fiddle showcasing the desired layout, lacking only even distribution → http://jsfiddle.net/bpdtkmmt/

Answer №1

Indeed, Flexbox can be utilized in this scenario and will likely fulfill the requirements at hand. It may involve some unconventional methods as CSS column rules need to be disregarded. While there is a degree of consistency, adjusting values may be necessary when adding more rows or columns. To maintain a set number of columns per row, setting a fixed or percentage height for the wrapper is essential. Subsequently, designate the wrapper container as a flex container using the display: flex rule and create multiple columns through either flex-direction: column and flex-wrap: wrap, or the shorthand flex-flow: column wrap.

In order to ensure each column contains a specific number of items, apply the flex-basis rule set at the desired percentage for the column to occupy. For instance, a column with 3 items would have flex-basis: 33.33%. Since you mentioned a layout with several rows of 3 items followed by a few rows of 2 items, utilizing the nth-child or nth-of-type selector to adjust the flex-basis to 50% from the 10th element onwards is recommended. Additionally, specify the width percentage for individual elements on the page (e.g., 4 rows = 25%) for uniform sizing.

However, centering text within a flexbox poses a challenge, requiring the establishment of elements as flex containers using display: flex or inline-flex, alongside setting flex-direction, text-align, and justify-content properties to center.

justify-content: center; 
flex-direction: column;
text-align: center

http://jsfiddle.net/hxcnoxx9/10/

This CSS approach should effectively address the specified requirements.

.wrap {
    height: 180px;
    background: linear-gradient(to right, tomato 0%, tomato 25%, slategrey 25%, slategrey 50%, tomato 50%, tomato 75%, slategrey 75%);
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
}

.thing {
    background: rgba(255, 255, 255, .2);
    color: #fff;
    width: 25%;
    flex-basis: 33.33%;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .5);
    display: inline-flex;
    justify-content: center;
    flex-direction: column;
    text-align: center;
}

.thing:nth-child(1n+10) {
   flex-basis: 50%;
}

Answer №2

Starting in the year 2022, it is now completely safe to utilize the column-count property since it is universally supported by all web browsers.

For example:

.wrap {
  column-count: 3;
  column-gap: 1rem;
}

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

<div><!-- including: unknown --></div>

Need some assistance with this issue. The ng-include path I am using is "http://localhost:8080/coffeeprojects/calc.html". I tested the path and it seems to be working. However, despite organizing the files and improving the path as suggested in some resp ...

Tips for utilizing divs and spans in HTML5

When it comes to web development, HTML markup is utilized for structuring content, while CSS is used for styling and presenting that content. In the past, HTML elements like <div> were often assigned semantic meaning through the use of IDs and class ...

Guide to configuring the overflow-y property for individual cells or rows in a Bootstrap-Vue table

Hello there, I am currently using Bootstrap-vue to display data that has been queried from a database. My goal is to have it displayed with an overflow-y style similar to this image. If you know how to achieve this effect, please share your solution. Here ...

What is the best way to insert text into a span element within a for loop using JavaScript?

I am currently working on form validation using an array and a loop. In each required field, I have created empty span elements. Here is the JavaScript code that I have set up: var name = document.querySelector('#Name').value, firstLastName = ...

Is it possible to adjust the width of the comment box on the Facebook "Like" button?

Is there a way to set the width of the comment box that appears after clicking the Facebook "Like" button? I know how to adjust the width of the button itself and related content, but can't find any options for the comment box: I've tried overri ...

Discovering how to adjust the "border-spacing" in a React Material-UI Table to ensure each row is nicely separated

I am looking to create Material-UI Collapsi Table Rows with separate styling. Check out the link for more information: https://material-ui.com/components/tables/#CollapsibleTable.tsx const theme = createMuiTheme({ overrides: { MuiTable: { root ...

What is the best way to arrange 4 divs with 2 on each line?

Consider the divs provided below: <body> <div id="f1">{{ f1|safe }}</div> <div id="f2">{{ f2|safe }}</div> <div id="f3">{{ f3|safe }}</div> <div id="f4"> ...

What are some creative ways to design drop-down menus within an email?

The snippet of code I am currently using lacks the ability to format text, and it does not allow content below dropdown items to shift down properly in an email. <select> <option value="first">First-time users of the Vanguard Events app:&l ...

Extracting Tik Tok videos from their URLs using PHP web scraping

I'm attempting to save videos from the following URL: Original: https://api.tiktokv.com/aweme/v1/playwm/?video_id=v09044340000bq0vmd39if221ld9o5n0 The link then changes to this: http://v16m.tiktokcdn.com/e50056249a925719f71ce8618053e173/5eee4e39/vid ...

Creating a scrolling list group in a single column that moves in sync with another longer column

When working with two columns in Bootstrap, such as col-4 and col-8, how can you ensure that a list-group stays fixed within its column and vertically aligned even when the content in the other column is scrolled down? Dealing with this responsively can b ...

Creating a horizontal scrolling section for mobile devices using CSS

Looking to create a horizontal scroll area specifically for mobile devices, similar to the design seen here: https://i.sstatic.net/oSNqL.png Additionally, I want to hide the scrollbar altogether. However, when attempting to implement this, there seem to ...

Overlay text on top of image with fading transparency when hovering over it

Is there a way to make the transparent layer that appears when hovering over an image on a card only cover the image itself and not extend onto the footer of the card? Currently, the image on the card covers most of it, and I want the hover overlay to beh ...

What is the effect of SEO on the use of image width and height attributes for a responsive website

Is it true that setting inline width and height for all images is beneficial for SEO and can improve site loading speed? Here's an example: <img src="http://www.example.com/images/free-size.jpg" width="200" height="400" alt="random image" /> E ...

Move the button to the following line and center it. Ensure that the background image remains at full size

.home{ background-image:url("https://cdn.pixabay.com/photo/2016/12/27/21/03/lone-tree-1934897_960_720.jpg"); background-repeat: no-repeat; height: 100vh; display: flex; align-items: center; justify-content: center; } .hero-text{ fon ...

Angular JS: Grabbing Text from a Specific div and Copying it to Clipboard

I recently developed a Random Password Generator using Angular JS. Here is how the output appears in the application: <div data-ng-model="password" id="password"> <input class="form-control" data-ng-model="password" id="password" placeholder=" ...

Is there a tool available for monitoring server status with HTML/CSS

I am interested in a way to create an HTML or CSS code that can ping an address or IP every 10 minutes to determine if it is sending back a signal. If a signal is received, I want the status on my website to display as 'online'. In case there is ...

How can a block be made to stay fixed and float in a flexbox layout?

There are three blocks within the body of this page. Title bar Content block Bottom block I have employed a flex display for the body layout. My goal is to make the title bar float, meaning it should always remain at the top when scrolling. I attempted ...

Is there a way to invert the order of elements in a scrollable container in Google Chrome?

The code snippet provided creates the desired effect, but unfortunately, it only seems to function properly in Firefox and Edge. In Chrome, while the elements are stacked correctly, there is no horizontal scroll bar, making the rightmost items hidden and i ...

Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX: <select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select> $.ajax({ type: "GET", url: "GetCraftCodes", data: ...

Tips for adjusting the up and down controls and spins of a date input after modifying its height

After adjusting the height of my inputs (date type) to 40px, I noticed that the up and down controls are no longer aligned with the text inside the field. I am looking for a solution to either adjust the height of the spins or remove them if necessary in ...