The maximum number of columns allowed in a flex layout in the direction of the

Can a maximum number of columns be set for a column-direction flexbox? I want to prevent the creation of a third column when the second one is filled. Instead, I prefer that the overflow extends both first columns while maintaining the element order.

https://i.sstatic.net/O2jVa.png

Take a look at this fiddle, and here's my code:

div.columns {
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  height: 200px;
  border: 1px solid red;
}

div.columns>div {
  width: 60px;
  padding: 0 8px;
  border: 1px solid black;
}
<div class="columns">
   <div>foo 1</div>
   <div>foo 2</div>
   <div>foo 3</div>
   <div>foo 4</div>
   <div>foo 5</div>
   <div>foo 6</div>
   <div>foo 7</div>
   <div>foo 8</div>
   <div>foo 9</div>
   <div>foo 10</div>
   <div>foo 11</div>
   <div>foo 12</div>
   <div>foo 13</div>
   <div>foo 14</div>
   <div>foo 15</div>
   <div>foo 16</div>
   <div>foo 17</div>
   <div>foo 18</div>
   <div>foo 19</div>
   <div>foo 20</div>
   <div>foo 21</div>
   <div>foo 22</div>
   <div>foo 23</div>
   <div>foo 24</div>
</div>

If flexbox isn't the right solution for this, what would work better? No need to worry about supporting old browsers.

Answer №1

When working with flex columns, it's important to note that the container usually needs to have a fixed height. If this is causing issues, you may want to consider using CSS columns as an alternative solution.

div.columns {
  columns: 2;
  border: 1px solid red;
}

div.columns>div {
  width: 60px;
  padding: 0 8px;
  border: 1px solid black;
}
<div class="columns">
  <div>foo 1</div>
  <div>foo 2</div>
  <div>foo 3</div>
  <div>foo 4</div>
  <div>foo 5</div>
  <div>foo 6</div>
  <div>foo 7</div>
  <div>foo 8</div>
  <div>foo 9</div>
  <div>foo 10</div>
  <div>foo 11</div>
  <div>foo 12</div>
  <div>foo 13</div>
  <div>foo 14</div>
  <div>foo 15</div>
  <div>foo 16</div>
  <div>foo 17</div>
  <div>foo 18</div>
  <div>foo 19</div>
  <div>foo 20</div>
  <div>foo 21</div>
  <div>foo 22</div>
  <div>foo 23</div>
  <div>foo 24</div>
</div>

Answer №2

For a different approach, when you have the number of elements (e.g., from the backend) and the desired number of columns known, you can utilize display: grid along with grid-auto-flow: column:

* {
  box-sizing: border-box;
}

.columns {
  --col-width: 60px;
  display: grid;
  grid-template-columns: repeat(var(--columns), var(--col-width));
  grid-template-rows: repeat(calc(var(--items) / var(--columns)), auto);
  grid-auto-flow: column;
  grid-gap: 10px;
}

.columns > div {
  width: var(--col-width);
  padding: 0 8px;
  border: 1px solid black;
}
<!-- Let's say you're aware that there are 24 items, and you want to arrange them into 3 columns 👇  -->
<div class="columns" style="--items: 24;--columns: 3;">
  <div>foo 1</div>
  <div>foo 2</div>
  <div>foo 3</div>
  <div>foo 4</div>
  <div>foo 5</div>
  <div>foo 6</div>
  <div>foo 7</div>
  <div>foo 8</div>
  <div>foo 9</div>
  <div>foo 10</div>
  <div>foo 11</div>
  <div>foo 12</div>
  <div>foo 13</div>
  <div>foo 14</div>
  <div>foo 15</div>
  <div>foo 16</div>
  <div>foo 17</div>
  <div>foo 18</div>
  <div>foo 19</div>
  <div>foo 20</div>
  <div>foo 21</div>
  <div>foo 22</div>
  <div>foo 23</div>
  <div>foo 24</div>
</div>

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

Attempting to dynamically assign a nickname to a CSS element selector during runtime

I have been attempting to adjust the position of a font awesome icon within an input field dynamically. My goal was to have the style calculated at runtime, similar to how I've handled other stylesheet elements in the past. However, I am facing diffic ...

Tips for eliminating excess padding hidden by carousel controls

Below is the code snippet I have implemented: index.html html, body { margin: 0; padding: 0; } body { width: 100%; height: 100%; } .container { width: 100vw; height: 100vh; background-color: #fc2; } /*.container .carousel slide .carou ...

The rendering of Vue-bootstrap is not functioning correctly

I recently set up a new webpack project using Vue and Bootstrap. However, it appears that the styling for bootstrap elements such as b-nav, b-nav-item, and b-badge is not displaying correctly. This issue seems to affect most other element types as well. Y ...

Scroll within the inner container

I am currently working on creating panels, each with a header and content. My goal is to allow scrolling only within the content area. Here is the CSS I have been using: .content-div{ float:left; height: 400px; overflow-x:hidden; overflow-y:hidden; ...

In order to position content at the center of a <div> element

Just recently delving into the world of Bootstrap, I've hit a roadblock. Here's my current conundrum: <div class="container px-4 py-5" id="hanging-icons"> <h2 class="pb-2 border-bottom">Work Experie ...

Enhance user experience with Bootstrap by automatically adding a frame around a card upon clicking

Hello everyone! I am a beginner in the Angular/Web Development world and I am currently working on improving my HTML/CSS skills. While using Bootstrap in my Angular project, I ran into a challenge that I couldn't figure out on my own. I have implement ...

Image that floats or pops over the content

I'm currently working on designing a card similar to the one displayed in the image. However, I'm facing difficulty in creating the floating image effect on the card. Any tips or suggestions on how to achieve this using angular, jquery, css, or a ...

Trouble with jQuery on click function, no actions triggered

I am having trouble with this jQuery function that is supposed to post the value of a variable to a PHP page and display it in the correct status class. I have included the necessary jQuery file, but for some reason, nothing is happening. Any assistance w ...

Position the Bootstrap Modal at the start of the designated DIV

When using a Bootstrap Modal to display larger versions of thumbnails in a photo gallery, there can be some challenges. The default behavior of Bootstrap is to position the modal at the top of the viewport, which may work fine in most cases. However, if th ...

Why is my CSS Grid still not working correctly even though validation services have confirmed it is 100% correct?

After running my HTML and CSS through the validation services, everything seemed to check out fine. However, when I try to implement the grid layout using the CSS below, it doesn't seem to work as expected: body { display: grid; grid-template ...

Guide to Inserting an Image with AngularJS

Recently, I embarked on developing a web application with AngularJS. I encountered an issue while trying to incorporate an image into my page - unfortunately, the image failed to display properly. img src="src/assets/img/f1.png" alt="icon" I utilized the ...

Selecting multiple items from a grid using the Ionic framework

I am looking to create a grid of category images in Ionic framework, where users can select multiple categories and send their values to the controller. However, I'm unsure about how to make this happen with Ionic framework. Below is the view I curre ...

Creating an adaptable image with CSS or Material UI

Is it possible to create a responsive image that shifts from the top right position to the bottom as the size decreases? I've been using Material UI but haven't found any guidance on how to achieve this. .my-photo{ height: 300px; positio ...

Tips on modifying the background color using bb code

I am currently working on creating a custom bbcode system. My goal is to change the format from [bg=color code]color text[/bg] to <span style="background-color:color code;">color text</span> Do you have any suggestions on how I can achieve thi ...

I am having trouble getting my custom CSS file to be applied to my website on Github pages

I attempted to upload a custom CSS file to apply on The raw CSS file is located at: https://raw.githubusercontent.com/themomoravi/SoundOff-Website/master/styles.css I used the following as my href since it directly links to the raw CSS file: href="http ...

When a label or checkbox is clicked, the absolute positioning triggers a sudden jump to the top of the containing div

Encountering an issue where the user is taken to the top of a scrollable div (absolute position) when clicking a label/checkbox. Code Tried different approaches on an onclick event for each of the 4 labels below, but unfortunately none of them are effect ...

Setting Background Color for a Specific Tab in CSS

HTML <div class="woocommerce-tabs wc-tabs-wrapper"> <ul class="tabs wc-tabs"> <li class="paym-plans_tab"> <a href="#tab-paym-plans">Contract Deals</a> </li> <li class="simfree-pla ...

Execute CSS within jQuery code only when the body class is present

I am attempting to use CSS (display: none;) within a script to hide elements from my menu only if a specific language is active. When the body class changes from one language to another, I want the script to check if the body class of a certain language ...

Using CodeIgniter to dynamically load colors from a database into a CSS color selector

My views are populated by an array that holds all the necessary information... This array also includes user profile data, with a link key and color value specified... Is there a way to pass this information into the CSS color selector so that each user& ...

Is there a way to use a specific keyboard input to alter the characteristics of shapes on my webpage?

Is there a way to change certain attributes of a shape onscreen when a specific key is pressed by the user? For example, can I make it so that pressing "a" changes the color of the shape? I attempted to modify a mouse rollover event to work with the desir ...