Increase the width of a flexbox container with column wrapping to accommodate its contents

Is there a way to create an element that adjusts its size based on the content using only CSS rules?

Consider this example:

ul {
  list-style: none;
  padding: 1rem 1rem 1rem 0;
  background-color: lightblue;
  max-height: 300px;
  display: inline-flex;
  flex-flow: column wrap;
  align-items: flex-start;
  width: 238px;
}

ul li {
  width: 100px;
  border: 1px solid red;
  margin-left: 1rem;
}

/* HTML */
<ul>
   <li>item</li> // repeated n times
</ul>


Check out this code snippet

The desired outcome should be similar to: this image

Answer №1

To ensure that each element on the list is as wide as the ul element, it's necessary to remove any hardcoded height and flex properties from the ul element. Instead, set width: 100%; on the lis:

ul {
  list-style: none;
  padding: 1rem 1rem 1rem 0;
  background-color: lightblue;
  width: 235px;
}

ul li {
  width: 100%;
  border: 1px solid red;
  margin-left: 1rem;
}

You can view the code in action on this CodePen link. I hope this explanation helps!

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

Adjust my website to fit various screen sizes and mobile devices

I'm encountering an issue on my website regarding resizing elements and content on various resolutions, particularly on mobile devices. You can view the website here. I've been testing it on both an iPad and a 14" laptop. While everything appear ...

The clarity of an HTML input field is still unclear despite attempting to clear it using the clear()

When I created a form in HTML using PHP, AJAX, JavaScript, and jQuery, I encountered an issue where after entering data and updating it in the database, the input fields were not being cleared. I tried using clear() in JavaScript but it didn't work. C ...

Quick and hassle-free form editing is a breeze with our user-friendly platform

Currently, the forms I have require certain files to be filled out before the submit button can be clicked. What I need is for the 'Title' field at the top of the list to also be marked as required. I know how to accomplish this for text fields, ...

Tips for displaying "No Results" without causing any lag in the browser

I'm encountering difficulty trying to implement a simple feature without resorting to a "messy" solution. The performance is suffering, and I am certain it's not done in a "professional" manner. What I desire is straightforward – displaying a ...

What is the reason border property does not transition effectively?

I am trying to remove the border property after a certain period of time (1 second) and make it smooth. Here is what I have attempted: var elem = $('div').css({'border-top':'6px solid #FC9A24', 'border-le ...

Integrate a PHP form that includes a redirect feature and an optional opt-in box

I'm a beginner and I'm trying to enhance this code by adding some extra features. When the form is submitted, I want the page to redirect to an external URL like www.google.com The checkbox should be automatically checked and when the client re ...

CSS3 divs scattered everywhere

I am really struggling with this CSS3 code: * { margin: 0px; padding: 0px; border: none; } body { font-family: Arial, Helvetica, sans-serif; font-size: 14px; background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#DBDBDB), to(#FFFFFF), co ...

Creating a blurred effect for a div element

Looking for assistance with adding an iOS-like blur behind text in columns using HTML code that utilizes Bootstrap 3's rows and columns layout. I have already tried applying the .transparent CSS class with blur and opacity properties, but it ended up ...

What is the reason behind receiving email alerts whenever visitors access my website?

While developing a website, I ran some tests and noticed that whenever I visit the URL, an email notification is generated. However, the notification received is just a blank one. Could this issue be related to what I entered in the action field? <for ...

The navigation bar fails to respond when clicked on mobile devices

Calling all developers! I need a helping hand to tackle an issue that's holding me back from completing this website. Can someone please take a look at on their smartphone browser and figure out why the responsive menu icon isn't working when cl ...

Are there any JavaScript functions available that can navigate to a different HTML page?

Here is an example of the functionality I am attempting. Can it be implemented? function CloseHTML(){ ApplyCSSClosingTransition(); setTimeout(function() { RedirectToAnotherPage(); }, 2000); } <div onClick='CloseHTML()'&g ...

The implementation of @font-face is failing to display on all currently used web browsers

Hey there, I could really use some help with getting @font-face to work properly. Despite trying numerous solutions, I still seem to be missing something. If anyone can spot what I'm doing wrong, please let me know! @font-face { font-family: " ...

Alignment issues with the layout

I am facing an issue with two containers stacked on top of each other. The bottom container is wider than the top one, and both have auto margins set for the left and right sides. I want the top container to be aligned evenly on top of the bottom one. I c ...

Is HTML5 local storage not functioning properly on IE11 in Windows 8.1 x64? Encountering an error message:

Is anyone else experiencing this issue where information cannot be found, even though it was previously fixed in a patch a long time ago? Has anyone else encountered this? localStorage.setItem("hello", "somedata"); alert(localStorage.getItem(" ...

AngularJS ng-class to remove a class

I am currently dealing with a frustrating issue caused by the following code <input type="text" ng-class="GLOBAL_ERROR == 1 ? 'error-branch' : 'defaulttxt'" ng-model="frm.FirstName" name="FN" ng-required="true" ng-minlength="2" ng-m ...

Having trouble viewing a dynamically adjusting Angular NVD3 graph embedded in bootstrap

I have been utilizing the NVD3 library along with bootstrap 3.0 for my project. I have two divs with the classes "col-md-6 col-sm-12" positioned side by side. My goal is to showcase charts in both of these divs. To ensure that the charts are displayed corr ...

Tips for duplicating specific div elements

Is there a way to create copies of selected divs within the same panel using a Javascript report designer? I attempted to achieve this by using the following code snippet: function DesignerClone() { $(".ui-selected").each(function () { va ...

Different custom background images for every individual page in Nuxt

Looking for a unique background-image for each page in my app is proving to be challenging. Currently, I have defined the background-image in style/app.scss. @import 'variables'; @import 'page_transition'; @import url('https://f ...

What is the best way to retrieve JSON data using JavaScript within a loop?

I'm struggling to retrieve data from a JSON object. Here's the JSON structure I'm working with: var data = [ {"mes":{ "January":{ "Inversion":"1000","Fans":"1020"} ...

I am having difficulty toggling the show feature

I am currently facing an issue where I want the answer to be displayed when clicking on the question, but unfortunately, it's not working as expected. Can someone please assist me in resolving this problem? Here is my code: <html> <he ...