Troubleshooting Flexbox: Content within a Flex Item is not displaying properly within a scrollable container

I recently embarked on converting one of my websites to flexbox and encountered an issue with vertical scrolling within a flex container. It appears that Firefox displays it correctly, while Chrome and Safari do not. You can check out the code here.

.container {
  height: 200px;
  border: solid 1px #000;
  display: flex;
  flex-direction: column;
  overflow-y: scroll;
}

.item {
  border: solid 1px red;
  padding: 20px;
  display: flex;
  justify-content: space-between;
}

.item div {
  border: solid 1px green;
}
<div class="container">
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
</div>

It's tricky because the box 1 & 2 text is not properly rendered inside the flex item. Can someone shed some light on what might be going wrong here and how I can resolve it?

Thank you!

Answer №1

If you want to achieve a padding of 20px, simply add flex-shrink: 0; to the .item class in your CSS. This will ensure that the padding works as expected.

The issue with the current setup is that there are too many items to fit within the 200px high container. As a result, the items collapse because flex shrink defaults to 1, allowing them to shrink until they can no longer do so, at which point scrolling begins.

To see the desired behavior, either increase the height of the parent container or remove some items from it.

.container {
  height: 200px;
  border: solid 1px #000;
  display: flex;
  flex-direction: column;
  overflow-y: scroll;
}

.item {
  flex-shrink: 0;
  border: solid 1px red;
  padding: 20px;
  display: flex;
  justify-content: space-between;
}

.item div {
  border: solid 1px green;
}
<div class="container">
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
</div>

Answer №2

To resolve the issue, utilize vertical alignment for the box divs. It seems to be related to collapsing padding. I opted for center alignment, but flex-start and flex-end are also viable options.

.container {
  height: 200px;
  border: solid 1px #000;
  display: flex;
  flex-direction: column;
  overflow-y: scroll;
}

.item {
  border: solid 1px red;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item div {
  border: solid 1px green;
}
<div class="container">
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>

Answer №3

To achieve the same result, simply include align-items:center

Make sure to update the CSS section as follows:

.item {
  border: solid 1px red;
  padding: 20px;
  display: flex;
  align-items: center; /*Include this line */
  justify-content: space-between;
}

.container {
  height: 200px;
  border: solid 1px #000;
  display: flex;
  flex-direction: column;
  overflow-y: scroll;
}

.item {
  border: solid 1px red;
  padding: 20px;
  display: flex;
  align-items: center; /*Add this line */
  justify-content: space-between;
}

.item div {
  border: solid 1px green;
}
<div class="container">
  <div class="item">
    <div>box 1</div>
    <div>box 2</div>
  </div>
  <!-- More item divs here -->
</div>

Ensure that you have the following code included:

.item {
  border: solid 1px red;
  padding: 20px;
  display: flex;
  align-items: center; /* Make sure to add this line */
  justify-content: space-between;
}

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

The FirefoxDriver in Selenium is having trouble starting on a Windows 7 system due to issues with loading the profile

I come across a plethora of ancient questions dating back to the times of mammoths, mentioning Firefox 22 or something along those lines. I encountered the same problem: Can't load the profile. Profile Dir: c:\users\alp\appdata\loc ...

Creating CSS-style overlayed images that are resizable

I have a collection of images all the same size, and I want them to be overlaid on top of each other. I've tried setting the position of the images to absolute, but this causes an issue with the container not adjusting its size accordingly. Additiona ...

Getting rid of background color in a tooltip using Material UI

I'm currently tackling an issue with Material UI's tooltip. I can't seem to find a way to make the background of the tooltip completely transparent. By default, it displays with a grey background and white text. Changing the background color ...

Enhancing the Syntax of If and If Else in Jquery Functions

I'm struggling to simplify and optimize this block of code. Would appreciate any help in making it more efficient! var status = "closed"; $(document).on('click', '[data-toggle-nav]', function(event) { if ($(this) && status = ...

How to align items at the center in material-ui styling

I have a row of cards inside a container that I want to align in the center with equal spacing around them. I am using the material-ui UI library for the layout. Despite adding the justifyContent: center property, the cards are not evenly spaced. This is ...

Center contents of Bootstrap row vertically

I have a simple property grid layout in Bootstrap 3 structured as follows: <div class="row"> <div class="col-md-3"> <div id="propGrid"> <div class="row pgTable"> <div class="col col-md-12"> <d ...

What could be causing the unresponsive hamburger button on my navbar?

As a beginner in HTML and Flask, I am attempting to create a navbar. However, I am encountering an issue with the hamburger button not functioning properly. When I resize the window smaller, the hamburger button appears but does not show the items like "Lo ...

Conceal elements that are overflowing slightly

I need help coming up with a purely CSS solution to hide div 3, which has extended beyond its container. Look at the picture linked below for reference. https://i.stack.imgur.com/isfvU.jpg ...

Changes in browser size will not affect the height

Is there a way to smoothly change the height of the navigation bar when resizing the browser? Currently, it snaps to the new height when the width goes below or above 1000px. Any suggestions? nav.cust-navbar { -webkit-transition: height 2s; tran ...

The hyperlink within the dropdown menu in HTML code is malfunctioning

I'm currently working on my personal website using HTML and CSS with textedit. I've successfully created functional links for the main navigation menu headings, but I'm encountering issues with the dropdown options. Whenever I try to click o ...

Challenges with displaying HTML website on mobile platform

Hey there! I admit that my website may not be the greatest, but bear in mind that this is just a school project page. My current issue is with the mobile view styling - the hamburger menu should replace the nav bar on smaller screens, but it's not wor ...

Jquery div mysteriously disappearing without explanation

I need some help with my image options. Whenever I hover over the image, the options appear below it. However, when I try to move down to select an option, they disappear. I have configured the slideUp function so that it only happens when the user moves a ...

What is the best technique to position a div at the bottom of a container that has a height of 100

After many attempts and searching for answers, I'm still struggling to figure out how to make the div float to the bottom with 100% height. I've even considered if it's an issue with Pure or something else entirely. It's definitely frus ...

Show the user's input within a clickable button

I'm trying to create a functionality where I have an input field and a button next to it. When I type something in the input field and click on the button, I want the result to be displayed in another button. Here is what I have attempted so far: f ...

Full height Footer using Flash and HTML

Seeking advice: I successfully implemented a Flash object with 100% height and width on an HTML page. However, I am struggling to add an HTML footer that stays fixed at the bottom of the page. Attempts made: I have tried various solutions, searched on Go ...

Is there a way to make the X-editable datepicker appear next to the date field rather than in the same spot every time?

When utilizing X-editable, I encountered a problem where specifying the datepicker field mode as "popup" results in the popup displaying correctly. However, when dealing with a long table (either vertically or horizontally), the position of the date/dateti ...

The Iframe contains its own scroll feature within the body

I am facing an issue with displaying an Iframe inside a modal. The challenge is that the content within the Iframe varies in height, making it difficult to set a fixed height for the Iframe itself. When I try setting the height to 100%, the result is a 150 ...

Installing assets automatically in Netbeans for Symfony2

Whenever I test my application, I find myself having to run the following command in Netbeans: assets:install Repeating tasks gets quite tedious... Symfony2 requires running this command to ensure that modified files are placed in the correct location. ...

Set the search input to occupy the full width of the container by utilizing Bootstrap's

I'm having trouble adjusting the width of my search input to 100% on screen resize (using @media (max-width: 1200px)). The issue: https://i.sstatic.net/EMr6W.jpg Here's how it should look (I can achieve this with a specific pixel width, but not ...

Scrolling to zoom in on the div content

I need the ability to resize the content within a div without changing the size of the div itself when the user scrolls. The function I currently have is as follows: var zoomable = document.getElementById('zoomable'), zX = 1; window.addEvent ...