Adjust column content to fit width, instead of setting width to 100%

To create columns for the ul, I used flex-direction: column and flex-wrap: wrap.

When the elements within the ul are split into multiple columns, each column takes on the width of the widest content in that column. However, if there is only one column, it spans 100% of the width.

I am trying to make sure that a single-column layout adjusts its size based on its content.

/* boilerplate */

* {
  margin: 0;
  padding: 0;
}

div {
  max-height: 100px;
  background-color: #cacaca;
}

ul {
  list-style-type: none;
  height: 100px;
  background-color: #dadada;
}

li {
  margin-right: 10px;
  background-color: #eaeaea;
}


/* actual code */

div {}

ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: center;
}

li {}
<div>
  <ul>
    <li>normal</li>
    <li>x</li>
    <li>y</li>
    <li>z</li>
    <li>short</li>
    <li>a</li>
    <li>b</li>
    <li>c</li>
    <li>wiiiiiiiiiiiiiiide</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>normal</li>
    <li>just two</li>
  </ul>
</div>

<div>
  <ul>
    <li>why</li>
    <li>full</li>
    <li>width?</li>
  </ul>
</div>

https://codepen.io/anon/pen/eRYjLd

Answer №1

Try using display: inline-flex instead of display: flex.

This will change the container from block-level (occupying the full width) to inline-level (adapts to the content width).

Similar behavior can be seen with display: block vs. display: inline-block.

OR

Transform the parent div into a flex container by adding justify-content: center.

This will limit the width of the ul flex container to the size of the flex item, where the default is flex-basis: auto (content width).

/* boilerplate */

* {
  margin: 0;
  padding: 0;
}

div {
  max-height: 100px;
  background-color: #cacaca;
}

ul {
  list-style-type: none;
  height: 100px;
  background-color: #dadada;
}

li {
  margin-right: 10px;
  background-color: #eaeaea;
}


/* actual code */

div {
  display: flex;             /* NEW */
  justify-content: center;   /* NEW */
}

ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: center;
}

li {}
<div>
  <ul>
    <li>normal</li>
    <li>x</li>
    <li>y</li>
    <li>z</li>
    <li>short</li>
    <li>a</li>
    <li>b</li>
    <li>c</li>
    <li>wiiiiiiiiiiiiiiide</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>normal</li>
    <li>just two</li>
  </ul>
</div>

<div>
  <ul>
    <li>why</li>
    <li>full</li>
    <li>width?</li>
  </ul>
</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

The div smoothly descended from the top of the page to the center under the control of jQuery

I am trying to implement a feature where a div slides down from the top of the page to the center when a button is clicked. However, my current code seems to be causing the div to slide from the bottom instead of the top. Ideally, I want the div to slide ...

Having trouble getting a DIV to appear on top of another DIV containing an image

I need help with positioning an "InfoBox" above the main header image on my website. Despite trying z-indexes and adjusting position, I can't seem to get it right. If anyone has any suggestions, please share. Thank you! For reference, here is the HTM ...

Adjusting the image placement within a modal window (using bootstrap 3)

Behold, an example modal: <!-- Large Modal --> <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg"> <div class="modal-content"> ...

What is the best way to activate a post function using a hyperlink?

I'm struggling to figure out how to call my post function using a link within my navbar. The issue is that I'm working with links in the navbar code, and not sure how to integrate calling the post function with them. Below is the code for my pos ...

How to Align Video Alongside Image in HTML without Using CSS

Is there a way to place a video on the left and an image on the right of the same line in HTML without relying on CSS? ...

Switching the theme color from drab grey to vibrant blue

How can I change the default placeholder color in md-input-container from grey to Material Blue? I have followed the instructions in the documentation and created my own theme, but none of the code snippets seems to work. What am I doing wrong? mainApp. ...

How can I adjust the size of my elements to be responsive in pixels

While browsing through Quora and Facebook feeds, I noticed the fixed size of user display pictures: width: 40px; height: 40px; Even when resizing the browser window for a responsive design, these pictures maintain their size at 40px x 40px. What other f ...

I am experiencing an issue where my JavaScript code does not redirect users to the next page even

I'm experiencing an issue with this code where it opens another webpage while leaving the login screen active. I've tried multiple ways to redirect it, such as window.location.href and window.location.replace, but nothing seems to be working. Ide ...

Jumping Sticky Table Headers

Looking for a solution to prevent the table header from moving when scrolling through the data: tbody { overflow-y: scroll; } thead, tbody tr { display: table; width: 100%; table-layout: fixed; text-align: left; } thead, th { position: sti ...

Stop the button from spanning the entire width of its container

Utilizing the Material UI button in my application with customizations as styled-components, I encountered an issue where setting the size="small" prop on the button caused it to only take up the width of the button text with some padding. This behavior pe ...

Develop a custom dropdown menu using JavaScript

I've been working on creating a dropdown menu that appears after selecting an option from another dropdown menu. Here's the HTML code I'm using: <br> <select id ="select-container" onchange="addSelect('select-container') ...

Creating a list of definitions in the form of an HTML table using a multidimensional array in

Currently, I am tackling the following challenge: I must create a public static buildDefinitionList() method that produces an HTML list of definitions (using <dl>, <dt>, and <dd> tags) and then returns the resulting string. If the array c ...

Successful PHP submission confirmation

I have implemented a basic PHP script on my website to handle email sending. <?php $headers ="From:<$from>\n"; $headers.="MIME-Version: 1.0\n"; $headers.="Content-type: text/html; charset=iso 8859-1"; mail($to,$su ...

Having difficulty in displaying database values in HTML modal using PHP

On my PHP page, I have a setup that displays data based on the fetched id from the URL. Everything is working smoothly, but when I click a button to show a modal with additional information, the modal appears blank. Here is the code snippet I am using: ...

I am experiencing difficulties with my focusin not functioning properly for the input element that I added

I am working on a program that includes a dynamic table with the following structure: <table id="selectedItems"> </table> I plan to use jQuery to append elements to it like so: var i=0; $("#selectedItems").html(''); $("#sel ...

Prevent any sliding animations of content when the button is triggered

I've implemented a basic slideToggle functionality in jQuery. You can check out the code on JSFiddle here: $(document).ready(function() { $(".panel_button").on('click', function() { $(".panel").slideUp(); var targetPanel = $(thi ...

Tips for arranging Radio buttons in multiple columns within the same Radio group in Material UI

As a newcomer in the world of React.js, I am feeling a bit anxious about posting my question on Stack Overflow. I hope everyone can overlook my lack of experience and forgive me for any mistakes. I have been struggling to find the right solution to my prob ...

Using a single jQuery script, implement multiple modals on a webpage that are responsive on both desktop and mobile devices

My Modal is functioning well on desktop, but I'm facing an issue on mobile where the screen doesn't close when clicking outside the modal area. Is there a way to solve this for mobile without adding the 'X' button to close it? I attem ...

Getting a jquery lightbox up and running

After experimenting with three different jquery plugins in an attempt to create a lightbox that appears when clicking on a link containing an image, I am currently testing out this one: . Despite adding the plugin source in the head and ensuring that the l ...

Displaying a banner at the bottom of a React page

I need to display a banner with the text "All items..." on every page. Currently, I have set the position attribute to fixed, but the banner is aligned to the left and I want it to be centered horizontally instead. Below is my code snippet: export const Ba ...