Achieving a snug fit for divs with varying heights

In a row of div elements styled with flexbox, the heights vary, but the tallest one dictates a uniform height for all. You can see an example in this JSFiddle:

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

HTML:

<div>
  <div class="big">
    hello
  </div>
  <div>
    bonjour
  </div>
</div>

CSS:

div {
  display: flex;
  flex-direction: row;
  border-color: gray;
  border-style: solid;
  border-width: 5px;
}

.big {
  height: 100px;
}

The "hello" box is set to 100px in height, causing the "bonjour" box to inherit the same height, despite its smaller content.

Is there a way to have each box's height determined by its content, rather than being influenced by the largest div within the row of aligned boxes?

Answer №1

To align items at the start of the parent div, simply add align-items: flex-start; to the CSS.

div {
  display: flex;
  flex-direction: row;
  border-color: gray;
  border-style: solid;
  border-width: 5px;
  align-items: flex-start; // This line is key
}

View Working Example on Fiddle

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 preventing me from setting the height of the buttons to be the same as their width?

My website needs a panel on the left side of the screen that contains square buttons, each button representing a different action. However, currently these buttons are stretching horizontally when they should be stretched vertically to match my UI design: ...

Delete the status message once the PDF has been successfully created using C# and Rotativa

We have implemented an "export to PDF" feature that generates a detailed report when the button is clicked. The PDF generation process is powered by Rotativa. While the PDF is being generated, we want to display a message (which is easy), and then dismiss ...

The dropdown submenu in Bootstrap 3 remains persistently open

I'm currently working on creating a submenu dropdown and following this reference link: https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_dropdown_events2&stacked=h However, I am facing an issue where all my submenus are always ...

Submenu in submenu not registering CSS hover events

Currently, on a website project that I'm developing, there is a submenu nested within another submenu. Interestingly enough, the level-2 submenu happens to be taller than its parent level-1 submenu. To control the visibility of these submenus, I' ...

What is the best way to make the cursor display as 'wait' while a script is running?

During the execution of a lengthy javascript function, I would like to change the cursor style to 'wait' and then revert it back to normal once the function is complete. I initially tried using the following code: document.body.style.cursor = &a ...

Not every form input is suitable for placement within a card design

I'm currently using the most up-to-date version of materializecss and have the following HTML code: <div class="container"> <div class="section"> <!-- Main content --> <div class="row"> <di ...

Discover the best way to showcase items within an arrayList using dual CSS styles!

I'm currently working on a project that involves an arrayList with two names: Bob and Steve. I have the requirement to display them in different colors, where Bob should be displayed in green and Steve in red. Component.CSS .Bob{ font-weight:bold; ...

Concerned about the security risks posed by html/script injection within an angular application

Currently, I am in the process of developing an angular application that includes a textarea feature. The user input in this textarea is utilized to generate a preview of the text entered, all within the client-side environment. My main concern is the poss ...

PHP tag iteration for unordered list segmentation

My current method of fetching data from MySQL is as shown below: $catmapper = new Application_Model_Mapper_BusinessCategoryMapper(); $result = $catmapper->getBusinessCategory(); To display the results, I use a for loop to iterate through and wrap each ...

Responsive Bootstrap navbar menu expansion issue at certain breakpoints

Just beginning my HTML/CSS journey and currently experimenting with Bootstrap, specifically the navbar component. I've made some adjustments to text alignment and added a logo. However, I encountered an issue when the screen size is reduced to trigge ...

Automatically trigger a page reload using a jQuery function

Currently facing an issue with jQuery. I created a drop-down menu and would like it to expand when the li tag within the menu is clicked. Even though I am using the jQuery click function successfully, there seems to be a problem where the webpage refreshe ...

"Bootstrap4 - Troubleshooting the Issue of Multiple Carousels Not Function

I attempted to incorporate 2 Bootstrap 4 carousels onto a single page, but encountered issues with the functionality. I utilized this code on my page, however, the last element in the carousel does not cycle correctly. There is a delay of 3 transactions be ...

The Html is not having JavaScript executed before it

Within my header, there is a script that is making a call to a web API in order to populate a view model. The script is as follows: @using GigHub.Controllers @using GigHub.ViewModel @model GigHub.ViewModel.ProjectsViewModel @{ ViewBag.Title = "Projects"; ...

What is the best way to display a horizontal list when a link is clicked?

Is it possible to add a "share" link similar to the one in the lower right corner of this page: I have attempted several times to achieve this using CSS only, but without knowledge of javascript/jquery, I am limited in what I can accomplish. Thank you fo ...

Numerous AJAX requests consolidated into a single file

Here is my HTML code snippet: <div class="a1"></div> <div class="a2"></div> <div class="a3"></div> <div class="a4"></div> <div class="a5"></div> I am looking to make an AJAX request in the follo ...

What could be the reason for ThemeProvider (Material UI) failing to function properly in this scenario?

I've encountered something puzzling with Material UI. Despite my experience with it, I can't seem to figure out why my H2 tag in the nested component is rendering in Times instead of Arial. I've double-checked the docs and my code, but the i ...

Stop the webpage from scrolling when clicking on a ui-grid field

Is there a way to prevent page scrolling when clicking on a row field in ui-grid? I'm working with a page that has ui-grid, and each row includes an anchor tag with a URL value linked and target="_blank" to open in a new tab like the example below: ...

When using NVDA, the close button is being read multiple times, whereas in JAWS it is being read correctly

Here is the code for a close button within a modal pop-up: <h2 class="sc-ejdXBC byjYiM MuiTypography-root MuiTypography-h6 sc-EoWXQ kOYWJk MuiDialogTitle-root" id="popupTitle"> Default Pop-UP Title <button class="sc-dm ...

Trigger javascript function with a button click

Is there a way to trigger a JavaScript function from an HTML button that is not functioning properly? REVISED To see the issue in action, please visit this jsfiddle link: http://jsfiddle.net/winresh24/Sq7hg/341/ <button onclick="myFunction()">Try i ...

Looking for assistance with eliminating the excess white space that's appearing between two div elements

Seeking assistance to remove the unwanted whitespace in my code. I have attempted to set *{margin:0} as well as for p and pre tags, but it hasn't been effective. The issue persists even though I am using BS4. Here is the snippet of the troublesome cod ...