What is the best way to make a flexbox item expand based on its content without exceeding the "flex: 1" property?

Need assistance with a container that has two children: a list and a button.

The button should have a fixed height of 40px. When the list is set to "flex: 1", it grows to take up all the space in the container except for the 40px used by the button at the bottom.

How can I make the list grow based on its content (like a regular div), but limit it to consuming all available space without overflowing if the content is too large?

Here's the HTML snippet:

.container {
  display: flex;
  height: 300px;
  width: 100px;
  flex-direction: column;
}
.list {
  display: flex;
  flex: 1;
  overflow-y: scroll;
}
.button {
  display: flex;
  height: 40px;
}
<div class="container">
  <div class="list">
    <ul>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
    </ul>
  </div>
  <div class="button">
    View
  </div>
</div>

Check out this example: https://jsfiddle.net/odrbey4c/

The first container's list should not take up all the space, just behave like a normal div. The list in the second container is working as expected.

Answer №1

Get rid of the flex:1 property from the list...which is what you need.

On the other hand, I understand that you want the button positioned at the bottom of the container. So simply include margin-top:auto to the button and it will work perfectly.

Also, adjust the button to flex: 0 0 40px instead of setting a specific height so it remains unchanged in size.

.container {
  display: inline-flex;
  /* for demonstration purposes */
  height: 300px;
  width: 100px;
  flex-direction: column;
  border: 1px solid #ff0000;
}
.list {
  display: flex;
  background: #bbb;
  overflow-y: scroll;
}
.button {
  display: flex;
  flex: 0 0 40px;
  margin-top: auto;
  background: #cccccc;
}
<div class="container">
  <div class="list">
    <ul>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
    </ul>
  </div>
  <div class="button">
    View
  </div>
</div>

<div class="container">
  <div class="list">
    <ul>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
      <li>Hello</li>
    </ul>
  </div>
  <div class="button">
    View
  </div>
</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

Is there a way to automatically determine the text direction based on the language in the input text?

When posting in Google Plus (and other platforms), I noticed that when I type in Persian, which is a right-to-left language, the text direction changes automatically to rtl and text-alignment:right. However, when I switch to English, the direction switches ...

The top margin is experiencing issues and is not functioning correctly

I'm struggling to identify the problem with this script: http://jsfiddle.net/AKB3d/ #second { margin-top:50px; ... } My goal is to have the yellow box positioned 50px below the top border of the right box. However, every time I add margin-top to ...

CSS - Button with upwards pointing arrow, requiring it to point downwards when hovered over

Struggling with the following issue: I have a button with a downward-pointing arrow in its description. When I hover over the button (or any content within it), I want the arrow to point upwards. However, currently, the arrow only points up when I hover ...

Chart: Observing the Discrepancy in the Initial X-Axis Points

I'm working on incorporating multiple charts onto one page, with each chart centered and stacked one after the other. I've set a fixed width for the graph canvas. The challenge arises from the varying ranges of tick values - one chart may have a ...

Sorting rows by words and numbers in JavaScript explained

Hello, I'm a beginner and I need help sorting the table rows in the following table. I also want to attach an onclick listener to the header after it is displayed. ID Name Inventory Volume 1 Rachel Data is not enough 2 Ross 100 3 Monica 1 ...

Fixed positioning on scroll on the right side

I need assistance to create a fixed right side with scrollable content on the left. I attempted using position: fixed for the left div but it didn't work as expected. Can someone please assist me with this? Link to the product ...

Using CSS to create a smooth transition effect when a form is placed

I have a box that fades out when clicked, but the form inside it is not working properly. When I click on '17 | Lampe im Esszimmer ... C301 | Frau Müller', the 'Beschreibung' and 'Notiz' sections appear, but the complete for ...

The hyperlinks are not functioning correctly on my template

I'm using a template with the following preview link: The template includes an image of a laptop on the left side, with clickable points representing links. My goal is to make these points clickable so that they open specific pages like mbank.pl or ...

Position various images at specific coordinates on the screen using CSS

Looking for help with positioning four images on the screen using coordinates specified in the code. The top-left coordinate positioning is not displaying properly as intended. Can you assist in identifying and correcting the error? <!DOCTYPE html&g ...

Hide a CSS class when it is the only one present

Is it possible to set the display option to none for a css class only when it is used alone, without affecting it when used with another class? Let's consider how the HTML code looks when I want to display the biography class: <div class="user bi ...

What is the process for adjusting the background to fit the image properly?

Before (On my display with a resolution of 1024px) https://i.sstatic.net/NPpK9.png After (On a different resolution - for example, screen width:1500px) https://i.sstatic.net/9dGTF.png #quotes_div img{ width: 100vh; margin-left: 3%; ...

Is it possible to place the object in the middle of the item above it?

Is there a way to horizontally center an item in the middle of another item? Here is a visual example: [ . ] (uncentered object = container-1) [.] (object centered horizontally compared to the one above = container-2) <div class=&q ...

Move the button to the following line and center it. Ensure that the background image remains at full size

.home{ background-image:url("https://cdn.pixabay.com/photo/2016/12/27/21/03/lone-tree-1934897_960_720.jpg"); background-repeat: no-repeat; height: 100vh; display: flex; align-items: center; justify-content: center; } .hero-text{ fon ...

Utilize JQuery variables for toggling the visibility of various DIV elements

On my webpage's splash page, there are 4 divs but only the home div is initially visible. The other three are hidden. Each of these divs has a button associated with it that triggers a jquery click event to swap out the currently visible div for the ...

Achieving a slanted line in full screen using bootstrap

Currently, I am working on creating a slanted line that stretches across the entire page. However, there seems to be a small gap on both the left and right margins of the line. Here is the HTML code: <div class="container-fluid"> ...

Click to dynamically toggle classes with jQuery

I am trying to apply a class called 'select' when I click on a paragraph tag. However, the code I have written doesn't seem to be working. Can someone please provide some suggestions? Here is the code: <style type="text/css"> #el ...

Steps for setting up dart sass on Fedora 37

I'm facing difficulties while trying to install Dart Sass on my Fedora 37 system. Despite following the official instructions and watching several tutorials, I keep encountering an error. According to the official Sass website: You can install Sass ...

The document scales down automatically as I decrease the size of the window, but does not adjust when I switch to a mobile viewport

I've been working on developing a web app with a focus on mobile-first design. When I scale down my Chrome window, everything looks responsive and functions well, adjusting objects to fit the smaller screen size. However, I noticed that when I switch ...

Rely on the content to establish the width, rather than the parent element

My elements include the following: .nav { width: 100%; background: red; display: flex; } .parent { background: blue; flex-grow: 1 } .child { background: yellow; } <div class="nav."> <div class="parent"> <div class="chi ...

Display an image in a DIV by loading it from a URL provided in a TEXT BOX

I am having trouble loading an image into a div tag from a URL that is obtained from a text box. Here is my current code snippet, but I can't figure out what else needs to be done: <html lang="en"> <head> </head> <bod ...