Make a flexbox item scroll in the cross-axis once it aligns with the size of another element

In my search, I've come across several questions that are somewhat similar but none quite address the exact issue at hand. While this question on Stack Overflow bears some resemblance, I am specifically looking to utilize a flex element instead of a static-size picture.

With the use of flexbox, my aim is to align a single element's height (cross-axis) with all other elements, and introduce a scroll option if the content in that particular element overflows, as opposed to expanding the container's height.

I have put together a straightforward example on JSFiddle to better illustrate my requirement.

Essentially, what I'm after is for the blue section to collapse and offer a scrollbar when it exceeds the length of the red section below, rather than stretching the flexbox vertically.

I've experimented with various solutions, and the closest result I could achieve was:

Adjusting the styling of ".element2" { min-height:0; height:0; } and ".body" { overflow-y:auto; }

though unfortunately, the outcome achieved can be observed by viewing the fiddle which does not allow scrolling, albeit the first element displays correctly.

Any assistance on this matter would be greatly appreciated, and I remain open to alternative methods besides using flexbox if there exists a simpler solution.

Below is the code from the initial fiddle demonstration:

.container{
  display:inline-flex;
}

.element1{
  background-color:red;
}

.element2{
  min-width:180px;
}

.title{
  background-color:green;
}

.body{
  background-color:blue;
}
<div class="container">
  <div class="element1">
    I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. 
  </div>
  <div class="element2">
    <div class="title">
      v Needs scrollbar v
    </div>
    <div class="body">
      Scroll Element <br/>
      Scroll Element <br/>
      Scroll Element <br/>
      Scroll Element <br/>
      Scroll Element <br/>
      Scroll Element <br/>
      Scroll Element <br/>
      Scroll Element <br/>
    </div>
  </div>
</div>

Answer №1

  • Adjust the right column by setting position: relative and adding overflow-y: auto to enable scrolling when necessary

  • Set the children of the right column to have position: absolute and position them correctly using top, left, and right.

This adjustment will prevent the children from affecting the height, allowing the box to expand and contract freely.

Example 1 - Title scrolls

Consider adding a min-height for better control over the right column's size.

.container {
  display: inline-flex;
}
.element1 {
  background-color: red;
}
.element2 {
  min-width: 180px;
  overflow-y: auto;
  position: relative;
  min-height: 50px;
}
.element2 > * {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
}
.element2 .body {
  top: 1em;
}
.title {
  background-color: green;
}
.body {
  background-color: blue;
}
<div class="container">
  <div class="element1">
    I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT.
    I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT.
  </div>
  <div class="element2">
    <div class="title">
      v Needs scrollbar v
    </div>
    <div class="body">
      Scroll Element
      <br/>Scroll Element
      <br/>Scroll Element
      <br/>Scroll Element
      <br/>Scroll Element
      <br/>Scroll Element
      <br/>Scroll Element
      <br/>Scroll Element
      <br/>
    </div>
  </div>
</div>

Example 2 - Title is fixed

To fix the title in place, apply overflow-y: auto and position: absolute to the .body element. Restrict its height with bottom: 0 to align it with the bottom of its parent (.element2)

.container {
  display: inline-flex;
}
.element1 {
  background-color: red;
}
.element2 {
  min-width: 180px;
  position: relative;
  min-height: 50px;
}
.element2 .body {
  overflow-y: auto;
  position: absolute;
  top: 1em;
  left: 0;
  right: 0;
  bottom: 0;
}
.title {
  background-color: green;
}
.body {
  background-color: blue;
}
<div class="container">
  <div class="element1">
    I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT. I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT.
    I WANT THIS ELEMENT TO CONTROL THE HEIGHT OF THE OTHER ELEMENT.
  </div>
  <div class="element2">
    <div class="title">
      v Needs scrollbar v
    </div>
    <div class="body">
      Scroll Element
      <br/>Scroll Element 1
      <br/>Scroll Element 2
      <br/>Scroll Element 3
      <br/>Scroll Element 4
      <br/>Scroll Element 5
      <br/>Scroll Element 6
      <br/>Scroll Element 7
      <br/>
    </div>
  </div>
</div>

Answer №2

When you are aware of the dimensions of the "red" box, you can set a max-height for the "blue" box and apply overflow-y: scroll to handle the overflow.

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

Blurring of text that occurs after resizing in CSS

When I use scale transform to zoom a div in HTML, the text inside becomes blurred. Is there a solution to make the text clear like the original? @keyframes scaleText { from { transform: scale(0.5, 0.5); } to { transform: scale(2, 2); } } ...

Click the icon to introduce a fresh row into the table

I have embedded a table with a "plus" font awesome icon. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorig ...

When a user scrolls over an element with a data-attribute,

Looking to create a dynamic header effect on scroll? I have two headers, menu1 by default and menu2 hidden with display:none. In specific sections of my website, I've added a special attribute (data-ix="change-header") to trigger the header change. T ...

The transition from Vuetify 2 to Vuetify 3 involves replacing deprecated properties like app, clipped-left, and offset-y with the new property called "

Seeking guidance on upgrading from Vuetify/Vue 2 to 3. As a non front-end developer tasked with updating legacy code, I'm facing challenges due to the migration guide assuming CSS knowledge and lacking examples for resolving removed features. The gui ...

Tips for positioning a solitary md-tab component to the right

Can a single md-tab element be aligned to the right of md-tabs while keeping the rest of the tabs in their original position? ------------------------------------------------------------------------- | Tab 1 | Tab 2 | Tab 3 | ...

HTML Element Split in Two by a Line in the Middle

Greetings to all, after observing for a while I have decided to make my first post here (and just to clarify, I am an electrical engineer, not a programmer). I am in search of creating a unique element in HTML where I can detect clicks on both its upper a ...

Tips for customizing scroll bar padding and margin using CSS classes or IDs

I am looking to customize three different types of scrollbars with unique padding and margins. Here is the global style for my scrollbars: /* Modifying the scroll bar across the whole application */ /* width */ ::-webkit-scrollbar { width: 5px; he ...

Ensure that the child div fills the entire parent area when resizing the page

I am facing an issue with a container div that contains two elements. When I resize the window, one inner div moves under the other. However, my requirement is that when one div moves under another on resizing, the first div should occupy the full width o ...

Tips on how to lock the ag-grid to the highlighted row's position and force it to scroll

We have integrated ag-grid into our system, and I am facing an issue with displaying the scrollbar in a child window based on the selected row position in ag-grid. Please refer to the Plunkr link below and click on source_id which will lead you to an edit ...

CSS guidelines for handling single line breaks with the white-space property set to pre-wrap

Is it considered acceptable to include solitary carriage return characters in an HTML block styled with white-space: pre-wrap? I am working with an application that receives SOAP messages and logs them to a file. The logging process involves removing new- ...

Encountering problems with Node Sass versions while trying to import custom scss files into app.js

I've encountered several articles on stack that didn't quite solve my issues. The problem arose when I tried to import a custom SCSS file into my React app.js to override bootstrap theme colors. Here are the style imports in my App.js: import &q ...

Is there a CSS fix for containing a floating div inside another div with overflow hidden?

I've managed to create a cool effect with a div of an airplane inside a parent div that has 'overflow: hidden' applied. This setup gives the illusion that the airplane div is flying from under an element on the page and carrying a banner alo ...

Can the site be shown in the background?

I have a unique idea for a games website that I want to share. It's a bit difficult to explain, so please bear with me as I try to convey my vision! The inspiration for this project comes from a website called . Have you seen it before? The concept i ...

The size of HTML select input varies among IOS, Android, and Windows devices

Does anyone know of a CSS property that can be used to tailor the styling specifically for IOS devices in order to achieve a similar look to that of windows and android?https://i.sstatic.net/VMSXb.jpg I attempted using -webkit, but unfortunately it did no ...

Tips for automatically scrolling images horizontally in responsive layouts

Recently, I've been working on a website called . On the homepage, my portfolio images are arranged in a grid format using the <li> tag. I'm looking to align them side by side and have them scroll horizontally automatically, preferably with ...

The alignment of elements side by side in Angular Flex is not supported, specifically in versions Angular 8.2.1 and Flex-Layout 8.0.0.beta 26

I'm attempting to arrange my boxes in a single line, with the ability to wrap and switch to column mode on smaller phone screens. Desired layout: Wins Losses Ties Points On shrinking screen: Wins Losses Ties Points Code ...

Breaking the layout using recursive components in VueJS

I am currently facing an issue where I need to dynamically load components. However, when some of these components are loaded, they appear with faulty or missing CSS styles due to a problematic first div element after the template. Removing this DIV manual ...

How can I customize a CSS media class?

In my project, I am using a CSS library that includes the following class: @media only screen and (max-width: 480px) { .nav-tabs>li { margin-bottom:3px; } .nav-tabs>li, .nav-tabs>li>a { display:block !important; ...

The fuse-sidebar elements are not being properly highlighted by Introjs

I have recently developed an angular project that utilizes the fuse-sidebar component. Additionally, I am incorporating introjs into the project. While introjs is functioning properly, it does not highlight elements contained within the fuse-sidebar. The ...

The selected text on the webpage is not showing up highlighted

Encountering an unusual issue that is challenging to comprehend and difficult to find a solution for using Google search. I have created a website with multiple web pages. Each page follows the same format: header with links at the top, content area in th ...