Determining the number of columns dynamically in CSS based on the maximum content of children elements

I am experimenting with using CSS columns to showcase a variable number of columns based on the maximum width required for the content to display elegantly (without any unnecessary line breaks). Additionally, I aim to expand each element to fill the available space in the column.

Presenting what I currently have (You can view it on JSFiddle):

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

However, this is my desired outcome (all elements having equal widths):

https://i.sstatic.net/1ZLvO.png

This is the code snippet I have attempted:

<div class="columns">
  <div>
    Lorem
  </div>
  <div>
     Ipsum
  </div>
  <div>
    Dollar
  </div>
  <div>
    Euro
  </div>
  <div>
    Bitcoin
  </div> 
</div>
* {
  padding: 0;
  margin: 0;
}

.columns {
  columns: auto;
  column-gap: 1rem;
  background: #4cafff;
  padding: 1rem 0;
}

.columns > div {
  width: max-content;
  background: #4caf50;
  color: white;
  font-size: 2rem;
  border: 2px solid green;
  margin: 1rem;
}

Answer №1

max-content operates differently from what you may think. Instead of causing content to wrap, it ensures that the width or height of the content will be the maximum size needed to fit all the content without wrapping.

If you want all elements to have equal width, you can specify a specific pixel value for the width like so:

.cols > div {
  width: 100px;
  text-align: center;
  background: #f4511e;
  color: black;
  font-size: 1.8rem;
  border: 2px solid red;
  padding: 0.5rem;
}

Answer №2

Check out this CSS snippet

* {
  padding: 0;
  margin: 0;
}

.columns {
  display: flex;
  flex-wrap: wrap;
  background: #4cafff;
  padding: 1rem 0;
}

.columns > div {
  background: #4caf50;
  flex: 1;
  color: white;
  font-size: 2rem;
  border: 2px solid green;
  margin: 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

Tips for optimizing CSS when two classes share common styles

I have limited experience with CSS, but I am currently using it for a project. I encountered a situation where two CSS classes share some common properties: .KPIDashboardContainerInertiaMatrixCell_data { display: table-cell; font-size: 18px; padding-top: ...

Use flexbox to arrange labels and input fields in a single line for better display

I've been working on a project where I need to lay out labels and input fields on separate lines using flexbox. <form class="form"> <label> Credit Card Number </label> <input type="searc ...

Conceal navigation button within react-material-ui-carousel

After successfully incorporating the React Material UI carousel, I found it to be quite simple. However, one thing that eluded me was how to hide the buttons and only display them on hover. I tried setting the props navButtonsAlwaysVisible to false, but it ...

Instructions for adjusting the size of my modal window when the keyboard appears?

While developing a next.js app, I encountered an issue with the chat modal. When the input field is in focus, I want to resize the modal height so that the keyboard popup does not hide the input field. I attempted to modify the DOM but could not get it to ...

The keyframe spinner fails to function properly on Firefox and results in blurry text

I am facing an issue with a keyframe spinner that rotates an image on the y-axis. It works perfectly in Chrome but fails to function in Firefox. I have tried changing the HTML tags to div or span class/id, but nothing seems to work. Interestingly, other ke ...

Delivering styled coldfusion outcomes utilising css and jquery

After using jquery to retrieve some data, I've noticed that the results are displayed correctly but the CSS is not being applied. Is there a way to style the results with CSS? function doSearch() { //Making an AJAX call to the server $.ajax({ / ...

Tips for including a verification symbol next to your username

How can I implement a verification badge next to a user's username on a website using jQuery, JavaScript, PHP, HTML, and CSS? Currently, I am using isset($user[verification]) to display the badge, but I believe this approach is not optimal as it requi ...

When there are multiple elements within the parent container, the CSS hover margin-top alteration fails to take effect

I am currently working on creating a dynamic menu bar that has elements which smoothly move upwards on hover. However, I have encountered an issue where this movement effect only works when the parent container contains exactly one element (such as in titl ...

Turning away swiftly from a popover triggered by hovering does not automatically conceal the popover

When utilizing hover-triggered popovers for specific highlighted rows within a table, I noticed an issue. If you scroll away quickly using the mouse wheel, the popover remains visible for an extra second even though you are no longer hovering over the targ ...

Issue with footer position not remaining at the bottom of the page

Hey there, I've been experiencing some issues with my website's layout. I tried setting a min-height of 1000px on the mainbody to make all the pages the same size, but it's caused a problem with my footer. Now, the footer is positioned in th ...

Determining the dimensions and placement of a triangular div element relative to its containing parent

I created a child div that forms a colorful triangle shape: .shape-triangle { width: 0; height: 0; border-left: 25px solid transparent; border-right: 25px solid transparent; border-bottom: 50px solid green; /* placed on top ...

Issue with DIV element wrongly breaking on iPhone Safari and Chrome - fix verified

I am currently working on a basic web application, but I'm facing an issue that's proving to be quite puzzling. When viewed on Desktop Chrome with the display set to iPhone 12 Pro (although I have a 13 Pro), everything looks perfectly aligned in ...

Is there an improved method for toggling animations in CSS using jQuery compared to my current approach?

Looking to create a toggle effect for a dropdown box that appears and disappears when a button is clicked. var clickState = false; $("#show").on("click", function() { if (!clickState) { $(".animated").removeClass("off"); refreshElement($(".an ...

Chic and perfectly aligned stripe button design

I need assistance with centering and styling the Stripe checkout button labeled "update card." Additionally, I want to adjust the appearance of the card number input field to be normal but readonly. How can I achieve this without relying on Bootstrap' ...

Scrolling horizontally is an option depending on the content displayed on the screen

Imagine having a webpage structured like this: <header>...</header> <body> <section id="A">...</section> <section id="B">...</section> <section id="B2">...</section> <section id="C">...&l ...

The HTML page failed to load the bootstrap.min.css stylesheet

I am currently working on a project using Java with Spring Boot in IntelliJ IDEA, managed by Maven. However, I am facing an issue when trying to connect bootstrap.min.css on my HTML page. This problem seems to appear randomly without any clear cause. Inte ...

How to center a div vertically in a window

I have a CSS layout that positions a div both horizontally and vertically relative to the window. It works well, but when I scroll down the page, the div stays centered as if the page hadn't moved. width:600px; height:300px; position:absolute; left:5 ...

Display or conceal a div depending on whether the user is on a mobile or desktop browser

In order to optimize the display on different devices, I organized my content into two divisions. The left division contains quantity, price, and item name, while the right division includes product names. For mobile browsers, I would like to hide the pro ...

Custom navigation bar created using Bootstrap 4

My webpage was created using Bootstrap 4, but I am having an issue with the navbar toggler not aligning properly within the navbar on mobile view. You can see the problem in the screenshot below: https://i.sstatic.net/aBejI.png If you visit the url for t ...

Ways to automatically adjust the margins of my HTML body page

I'm currently working on a school project to develop a website. The challenge I'm facing is customizing the navbar and header background colors in such a way that they extend seamlessly to the end of the page. Despite my efforts, there seems to b ...