Flexbox is the perfect solution for creating a container with a fixed height that wraps items and adjusts its

I'm having trouble making flexbox CSS work for a specific use case scenario. I have written JavaScript that meets my needs, but I want to explore the possibility of achieving this with pure CSS. The challenge lies in ensuring that a container div only takes as much width as needed to contain its items, which are displayed from top to bottom and wrap into new columns when necessary.

Below is some sample HTML code I am testing:

<div class="my-container">
    <div class="my-item">Item 1</div>
    <div class="my-item">Item 2</div>
    ...
    <div class="my-item">Item 25</div>
</div>

Accompanied by the CSS used for testing:

.my-container {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    background-color: rgb(33, 150, 243);
    padding: 15px;
    height: 600px;
}

.my-item {
    width: 250px;
    height: 25px;
    color: white;
    padding: 10px;
    flex: 0 0 auto;
}

The issue I am facing is that while the items wrap correctly into new columns, the container expands to fill the viewport. But changing the display attribute to "inline-flex" causes the container to be too narrow to display multiple columns effectively. This dilemma arises due to fixed-sized items needing to flow into columns based on the container's height, with the container's width adjusting accordingly.

I attempted exploring CSS grid as an alternative solution, but it appears that knowing the number of columns beforehand is essential for implementation. Flexbox seems like a promising approach, although not without its limitations.

Are there any other CSS-based techniques that could address this challenge more effectively? While my JavaScript version works, I am keen on discovering a CSS-centric solution if viable.

Answer №1

Utilize the autofill property within a grid to dynamically adjust the number of columns.

.my-container {
    display: grid;
    grid-auto-flow: column;
    grid-template-rows: repeat(auto-fill, minmax(45px, 1fr));
    background-color: rgb(33, 150, 243);
    padding: 15px;
    height: 600px;
    width: fit-content;
}

.my-item {
    width: 250px;
    height: 25px;
    color: white;
    padding: 10px;
    flex: 0 0 auto;
}

Check out this JSFiddle demo for reference: https://jsfiddle.net/92sak0zc/6/

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 apply an outer border to a table using CKEDITOR?

Is there a way to only add an outer border to an HTML table in CKEDITOR? I've attempted to remove the inner borders using the advanced options in CKEDITOR, but it doesn't seem to be working. <table border="1" cellpadding="1" cellspacing="1" ...

reactjs dynamically adjusting background image size based on window dimensions

I'm tackling a ReactJs project at the moment. How can I adjust the background image to resize based on window size, particularly when toggling the device toolbar? .container{ background: url('../image/2.png'); } Currently, the backgrou ...

Division element containing an anchor tag

At first, I am aware that there are numerous questions similar to this one, but none of the resources I have come across have provided a solution. For some reason, my current approach is not working... I am trying to create a clickable area inside the cla ...

Resolved the issue of the background image flickering during the transition of the slideshow at the top of

I'm experiencing a unique issue in Chrome only. On my webpage, I have a div with a fixed background image placed below a slider at the top of the page. However, when the slider advances to the next slide, the fixed background image almost completely d ...

Arrange the div next to each other

Looking to position two divs side by side with widths totaling 100%? The left div will have a specified width in pixels that can change, while the right div should expand to fill the remaining space. Having trouble with this layout? Check out my example o ...

Include a border around the image on the overlay, along with a close button or link

Looking for some help with my code that opens images in an overlay div. I am struggling to add a border to the image object, as it only displays the raw image without any border. How can I modify this code to include a border around the image and also ad ...

Styling Text with Shadow Effects in Mobile Web Browsers

I'm attempting to apply a text-stroke effect using text-shadow like this: .text-stroke { text-shadow: -1px -1px 0 white, 1px -1px 0 white, -1px 1px 0 white, 1px 1px 0 white; } Unfortunately, it's not rendering correctly on mobile versions of Fi ...

How come the transition does not take effect when removing and adding the class to the same element with the removeClass() and addClass() methods?

Two images are present, with the first one having the class "opacityOne". When a button is clicked, based on the variable index, I want the current image to fade in while the other fades out. It works well when I remove the "opacityOne" class from one ima ...

Error Alert: The attempt to load the image 'data:image/svg+xml,%3csvg > xmlns='http://www.w3.org/2000/svg' was unsuccessful after selecting a Bootstrap 5 radio button

Whenever I click on the radio button below, a message appears in the console of my various web browsers (Firefox, Chrome, etc). [Report Only] It is refusing to load the image 'data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' vi ...

change visibility:hidden to visible in a css class using JavaScript

I've put together a list of Game of Thrones characters who might meet their demise (no spoilers included). However, I'm struggling with removing a CSS class as part of my task. Simply deleting the CSS is not the solution I am looking for. I' ...

How about "Switching Background Images Using a Click Trigger"?

I have a single web page with three different tabs. By clicking on each tab, the text displayed changes accordingly. I am looking to add three separate images for each tab so that when a user clicks on a specific tab, the image associated with it also chan ...

There are no visible CSS styles detected in the email

Why do the CSS styles not appear when viewing this HTML page in an email client like Yahoo or Gmail using mutt? The table appears plain without any styling, but it displays correctly in a web browser. Is there something I am overlooking? mutt -e "set cont ...

Ensuring full height on Bootstrap 4 columns without triggering a scrollbar in the browser

I'm currently working with Bootstrap 4 and attempting to make 3 columns on the page fill 100% height, only displaying scrollbars within the columns and not on the entire page. I have experimented with applying h-100 to the row div, as well as implemen ...

Utilizing nested Bootstrap 3.1.1 collapse functionality

Hey there! I need some assistance. Currently, I am working with Bootstrap 3.1.1 and implementing nested collapse menus in a sidenav. My goal is to apply a background color (using a specific class) to the open menu and remove it when the menu is closed. Ev ...

Traversing a multidimensional array with jQuery

In my quest to iterate through a multidimensional array, I encountered the need to remove the class "list" from the divs within the array. However, there could be multiple divs with this class on my site. My initial approach involved using two for-loops, ...

Implementing a button on a polygon shape within an SVG

I have an SVG at the bottom and I would like to place a button on the bottom line that is also responsive. How can I achieve this using CSS? What I am trying to accomplish: Example Image Is it possible with just CSS and how should I go about implementin ...

Tips for Creating a CSS Radio Button in ASP.NET

I've recently developed a web application using asp.net and I'm facing an issue while trying to style the radio buttons using CSS like the example below: https://i.sstatic.net/k7qGB.jpg However, after applying this design, the radio buttons are ...

Creating smooth transitions with CSS in React by fading text in when a user clicks a button

Within this presentational component: const HowToControls = props => { return ( <div className="col-md-6 how-to"> {props.isOpen ? <p className={`text ${props.isOpen ? 'visible' : ''}`}> lorem ...

My Bootstrap/Flexbox layout is not behaving as anticipated. Can anyone identify any issues in my code?

I'm currently working on a MVC website that features a login/signup form. I have been trying to utilize bootstrap to format the forms. My main goal is to center the entire form on the screen, but so far I have only been successful in centering it on t ...

Issue with cdk-virtual-scroll-viewport: Scroll function malfunctioning when using navigation up/down button

While utilizing cdk-virtual-scroll-viewport within my mat-autocomplete, I have noticed that scrolling works perfectly when using the mouse but does not function properly when pressing the keydown button. Expected Behavior: The list should automatically sc ...