My preference is for the flex box to expand in width (sideways) without increasing in height (up and down)

Is there a way for flex items to expand in width but only in height when necessary? I enjoy using flexbox, but I find it frustrating that all flex items in a row grow to the same height even when there isn't enough content to fill them, resulting in additional items being displayed further down the page. Ideally, I would like the flex items to adjust themselves to available space when preceding items lack sufficient content to fill their box, rather than leaving a large gap.

Could this be due to my lack of knowledge or is it simply not feasible? If it's not feasible, could this feature be added in future updates?

(Apologies for the lack of reputation to upload diagrams to clarify!)

[EDIT. Code added as requested. Some style remains to demonstrate the white space I wish other flex items to occupy.]

<!doctype html>
<html>
 <head>
   <style>
   .content {
   font-size: 2em;
   width: 100%;
   margin: 0px;
   padding: 0px;
   background-color: coral;
   display:flex;
   flex-direction:row;
   flex-wrap: wrap;
   justify-content: center;
   align-content: stretch;
   align-items: flex-start;
}
.flex-item {
  box-sizing: border-box;
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis:40vw;
  background-color: rgba(255, 255, 255, 0.9);
  border: 2px solid white;
  border-radius: 2px;
 margin: 2vmin;
 padding: 2vmin;
}
</style>
</head>
<body>
<div class="content">
<div class="flex-item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu venenatis nisi. Sed nec purus consectetur, sodales mi vel, efficitur arcu. Vivamus id congue quam. Fusce imperdiet bibendum egestas. Mauris porttitor risus id pharetra pharetra. Vivamus et lorem erat. Nullam ac nulla ex. Nulla sit amet semper ligula. Integer augue sem, pharetra in ex ut, finibus mollis neque. Integer vulputate dolor massa, a maximus sem vehicula malesuada. Morbi a nulla ornare, egestas nisl in, ultrices est. Integer ut maximus elit. Cras ac velit condimentum, dapibus dui quis, mattis ex.
</div>
<div class="flex-item"><img src="https://pixabay.com/get/ec8630811c846e5862cb/1442266437/wheat-797086_1280.jpg" width="100%">
</div>
<div class="flex-item">Vivamus semper at tortor a lacinia. Nulla a suscipit felis. Aliquam erat volutpat. Integer dignissim suscipit nibh a accumsan.Fusce gravida nisl nec elit placerat porta. Ut feugiat feugiat lorem nec commodo. Morbi porttitor vel sapien id tincidunt. Vivamus venenatis pellentesque tempus.
</div>
<div class="flex-item"><img src="https://pixabay.com"/get/ec8630811c846e5862cb/1442266437/wheat-797086_1280.jpg" width="100%">  </div> 
  </div>
 </body>
</html>

It seems my question may be unclear. Let me try to elaborate within the constraints of the platform, as I am unable to post a diagram.

In the Flex item boxes, there are text, images, or a combination of both. Items containing just images scale based on available space. At higher resolutions, text-only boxes have equal width and height (forming a square), allowing images to scale appropriately. However, at smaller viewports like 400px wide, the text-only boxes become elongated (for instance, 200 x 1000px) while image boxes maintain a square size of 200 x 200px. The next line then displays below the bottom of the text in the flex item, creating a sizable gap (around 800px high) under the image. Despite having ample space for additional flex boxes after the compressed image, they do not move into the gap. Is this clear to those who placed the question on hold?

Answer №1

If you're searching for the 'align-content' property, it defaults to 'stretch' and aligns elements vertically (on the cross-axis).

In contrast, 'justify-content' defaults to 'flex-start', which aligns elements horizontally (on the main axis).

'align-self' defaults to 'auto' and can be used to control individual items. Sometimes setting max-height and height properties to the same value works as well.

The choice of which option to use depends on your specific needs.

A valuable resource for further information is: Codrops CSS Reference - Flexbox

@Sharon

I think this provides the solution you were looking for. Essentially, every element in your solution has a defined width and height, including your inner box. Setting both min and max heights for your 'flex-item' will prevent resizing. Take a look at the code for more guidance.

        body {
          overflow: hidden;
          /* just for testing, remove */
        }
        .flex-content {
          width: 100%;
          margin: 0;
          padding: 0;
          display: flex;
          flex-flow: row wrap;
        }
        .flex-item {
          flex: 1 1 40vw;
          min-height: 50px;
          max-height: 50px;
          background-color: rgba(255, 255, 255, 0.7);
          text-align: center;
          border: 2px solid white;
          border-radius: 2px;
          margin: 2vmin;
          padding: 2vmin;
          background-color: #fce4ec; /* for testing*/
          
        }
<div class="flex-content">
  <div class="flex-item">some text</div>
  <div class="flex-item">some text data</div>
  <div class="flex-item">some more text data</div>
  <div class="flex-item">again some more text data</div>
  <div class="flex-item">some text</div>
  <div class="flex-item">some text data</div>
  <div class="flex-item">some more text data</div>
  <div class="flex-item">again some more text data</div>
  <div class="flex-item">some text</div>
  <div class="flex-item">some text data</div>
  <div class="flex-item">some more text data</div>
  <div class="flex-item">again some more text data</div>
</div>

Answer №2

By including the CSS property height: min-content in the inner div, I was able to successfully resolve the issue I was encountering (in my particular scenario, this solution proved to be effective).

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

Responsive center alignment of stacked `<div>` elements using CSS

My goal is to center a stack of divs in 3 columns per row if the browser window is wider than 1024px, and switch to 2 columns per row if it's smaller, while still displaying all 6 divs. Unfortunately, I'm facing difficulties trying to apply what ...

Laravel error message for unresolved symbolic link

Previously, I had developed a form for uploading profile pictures using the code snippet below, $image = $request->file('avatar'); $pass = Hash::make($request->password); if($image!=null){ $avatar_na ...

Stop the form submission from redirecting or refreshing by utilizing JavaScript

I'm facing an issue with a page on my website that includes textboxes generated from php, along with two buttons - GetData and SaveData. My goal is to enable users to press the enter key while editing a textbox and trigger the same action as clicking ...

What is the best way to implement CSS Float in typescript programming?

For a specific purpose, I am passing CSS Float as props. To achieve this, I have to define it in the following way: type Props = { float: ???? } const Component = ({ float }: Props) => {......} What is the most effective approach to accomplish this? ...

Enhancing CSS with Additional Properties

As a web developer, I recently had the opportunity to explore the new LOGIN PAGE preview of GMAIL and was very impressed with the Sign In button's UI. After examining the Page's CSS, I discovered some interesting properties, such as: **backgroun ...

Adding and adjusting the size of different DIV elements within a shared area

Looking to create a dynamic bar with the ability to add multiple child DIVs (similar to this: https://i.stack.imgur.com/tdWsq.jpg). Utilizing jQuery, jQuery UI, Bootstrap, and various plugins. The structure for the div (or span) system could be structured ...

Implementing Enter key functionality to add items to a Todo list using pure DOM manipulation

var listLis=document.getElementById('list'); const addbutton=document.querySelector('.fa-plus') const inputBar=document.querySelector('.show') function showInput(e){ inputBar.classList.toggle('show') } addbutt ...

Creating a navbar dropdown that is clickable on mobile devices and opens on hover on desktop is a simple way to improve

Utilizing Bootstrap 5 for my website creation, I am facing an issue with the navbar dropdown functionality. On mobile devices, the dropdown opens upon clicking but fails to close when clicked again. Meanwhile, on desktops, hovering over the dropdown works ...

Contact form repair completed - Messages successfully delivered

I am facing an issue with the contact form on my HTML landing page. Currently, when you click the 'Submit' button, it redirects to a new PHP page displaying 'Success'. Is there a way to make it so that upon clicking 'Submit' a ...

The height of the Material UI dropdown is displaying in an improper manner

I'm currently experimenting with the Material UI dropdown on my website, utilizing the react-select library. However, I've encountered an issue where the UI gets compromised when using an option that exceeds the width of the dropdown. If anybody ...

Issue with 'backface-visibility' CSS3 property not functioning on any versions of Internet Explorer

I'm looking to implement a specific animation in Internet Explorer. The goal is to rotate an image like a coin, displaying a different image on the other side. I suspect that the issue lies with Backface-visibility in IE, but I'm not entirely sur ...

Which web browser(s) can properly display the CSS property display: run-in?

Compatibility with IE7 and FF2.0 Suitable for IE8 and Opera 9+ Works only on IE7 Optimized for IE8 and FF3.5 Supports IE7 and Safari This question came up in a quiz, but I don't have all the browsers to test it. Any assistance would be greatly apprec ...

PHP - Unable to store the input content

I've encountered an issue with my website where I need to create a form for users to submit new information or news. Below is the code snippet that I have: <?php include "0begin.php"; $title=$_POST["title"]; isset($title) or $title=$_GET["tit ...

Numerous options available in a dropdown menu

I have a Dropdown menu that offers various functions. "Update" option redirects to a page for updating information "Export Form" option redirects to a page for exporting the form Although the current code allows these actions, I am facing an issue with ...

How to utilize jQuery to eliminate HTML onchange code

This block of code features an HTML onchange attribute as well as the use of jQuery's change event on an input text. I want only the jQuery change event to take precedence. I have attempted to use both unbind and off without success. Can someone prov ...

Two liquid level divs within a container with a set height

I hesitated to ask this question at first because it seemed trivial, but after spending over 3 hours searching on stackoverflow and Google, I decided to give it a shot. The issue I'm facing can be found here: http://jsfiddle.net/RVPkm/7/ In the init ...

Unveiling the Mystery: Java Tips for Capturing the Chosen Value from a DropdownChoice in Apache W

In my Java class, I created a method where I instantiate a DropDownChoice object for a select menu and add it to the form. Within this method, I am populating the projects list into the billableProjectsList. public class ReportCriteria implements Serializa ...

Experiencing difficulties connecting JavaScript with HTML

I'm encountering difficulty with implementing my JavaScript on my website. While it functions properly in this JS Fiddle, I am experiencing issues when trying to use it on my actual site. Below is how I have linked the JS on my site: <head> < ...

Is it possible to conceal a link once it has been visited?

I am trying to figure out how to remove a link from a page once it has been visited. However, I am facing privacy restrictions with the :visited pseudo-class. Is there a way to achieve this without using display: none? For example: .someclass a:link {dis ...

Make the minimum height of one div equal to the height of another div

My query is somewhat similar to this discussion: Implementing a dynamic min-height on a div using JavaScript but with a slight twist. I am working on a dropdown menu within a WordPress site, integrated with RoyalSlider. The height of the slider div adjust ...