Issue with Cart Items Container's Scroll Behavior on Mobile Devices

Exploring a web layout where the .cart-items-container needs to occupy the remaining space above the .cart-rest-details section. The .cart-items-container should be scrollable if it overflows while the .cart-rest-details remains fixed at the bottom of the viewport. However, facing challenges with the layout specifically on mobile devices.

Providing my codebase below:

.cart-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
  height: 100vh;
}

.cart-items-container {
  display: flex;
  flex-direction: column;
}

.cart-items {
  padding: 8px;
  max-height: 300px;
  overflow: auto;
  background-color: #EEEEEE;
  border: 2px solid orange
}

.cart-rest-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 90%;
  padding: 5%;
  background-color: dodgerblue;
  color: white;
}

@media screen and (max-width: 768px) {
  .cart-items-container {
    flex-grow: 1;
  }

  .cart-items {
    flex-grow: 1;
    max-height: none;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="cart-container">
    <div class="cart-items-container">
      <h1>Cart Items</h1>

      <div class="cart-items">
        <p>Item 1</p>
        <p>Item 2</p>
        <p>Item 3</p>
        ...
        <p>Item 25</p>
      </div>
    </div>

    <div class="cart-rest-details">
      <p>Description 1</p>
      <p>Description 2</p>
      <p>Description 3</p>
      ...
      </div>
  </div>
</body>

</html>

Experiencing issues on mobile devices where the .cart-items-container doesn't fill the remaining space correctly and the scrolling behavior in the .cart-items isn't as expected.

Current Behavior:

https://i.sstatic.net/1KKAshY3.png

Expected Behavior:

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

Answer №1

By adding the CSS rule resize: both to your .cart-container, you can now drag from the bottom right corner to see how the layout adapts:

body {
  margin: 1vh;
}

* {
  box-sizing: border-box;
}

h1 {
  margin: 0;
}

.cart-container {
  resize: both;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 16px;
  height: 98vh;
}

.cart-items-container {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
}

.cart-items {
  padding: 8px;
  overflow: auto;
  background-color: #EEEEEE;
  border: 2px solid orange;
  height: 50px;
  flex-grow: 1;
}

.cart-rest-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 90%;
  padding: 5%;
  background-color: dodgerblue;
  color: white;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="cart-container">
    <div class="cart-items-container">
      <h1>CART ITEMS</h1>

      <div class="cart-items">
        <p>Item 1</p>
        <p>Item 2</p>
        <p>Item 3</p>
        <p>Item 4</p>
        <p>Item 5</p>
        <p>Item 6</p>
        <p>Item 7</p>
        <p>Item 8</p>
        <p>Item 9</p>
        <p>Item 10</p>
        <p>Item 11</p>
        <p>Item 12</p>
        <p>Item 13</p>
        <p>Item 14</p>
        <p>Item 15</p>
        <p>Item 16</p>
        <p>Item 17</p>
        <p>Item 18</p>
        <p>Item 19</p>
        <p>Item 20</p>
        <p>Item 21</p>
        <p>Item 22</p>
        <p>Item 23</p>
        <p>Item 24</p>
        <p>Item 25</p>
      </div>
    </div>

    <div class="cart-rest-details">
      <p>Description 1</p>
      <p>Description 2</p>
      <p>Description 3</p>
      <p>Description 4</p>
      <p>Description 5</p>
    </div>
  </div>
</body>

</html>

The flexbox algorithm prioritizes your max-height as a minimum height and won't shrink below it, so using a low initial height and allowing flex-grow to adjust dynamically should achieve the desired outcome.

Answer №2

To address the issue, you can try inserting min-height: 0; into the styling for .cart-items-container.

Typically, a child element within a flexbox layout won't shrink below its intrinsic content size (min-content) even with flex-shrink applied. By including min-height: 0, this default behavior is overridden, allowing the element to properly resize and display the expected scrollbar.

.cart-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
  height: 100vh;
}

.cart-items-container {
  display: flex;
  flex-direction: column;
  /* added */
  min-height: 0;
}

.cart-items {
  padding: 8px;
  max-height: 300px;
  overflow: auto;
  background-color: #EEEEEE;
  border: 2px solid orange
}

.cart-rest-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 90%;
  padding: 5%;
  background-color: dodgerblue;
  color: white;
}

@media screen and (max-width: 768px) {
  .cart-items-container {
    flex-grow: 1;
  }

  .cart-items {
    flex-grow: 1;
    max-height: none;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="cart-container">
    <div class="cart-items-container">
      <h1>Cart Items</h1>

      <div class="cart-items">
        <p>Item 1</p>
        <p>Item 2</p>
        ...
        <p>Item 24</p>
        <p>Item 25</p>
      </div>
    </div>

    <div class="cart-rest-details">
      <p>Description 1</p>
      <p>Description 2</p>
      ...
      <p>Description 5</p>
    </div>
  </div>
</body>

</html>

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

Steps to switch the displayed ionicon when the menu is toggled

My goal is to display the 'menu-outline' ionicon on my website until it is clicked, at which point the icon will change to 'close-outline' and vice versa. I am utilizing jQuery for this functionality. Although I am aware of how to togg ...

You can utilize the Toggle Button to alternate between multiple classes, not just two

Currently, I am learning Java with Spring Boot and Thymeleaf. While creating some views, I faced a challenge. Initially, I wanted to create a toggle button for "Pending" and "Received". However, now I am attempting to add the option "Cancelled", but I have ...

Retrieving HTML code as a variable using Python

I am attempting to output HTML code as a variable in my Python script, but it's not working properly. What could be causing this issue? Here is an example of my code: return render_template("dashboard.html", services = "<h1>Service 1</h1> ...

Can row and container classes be used together on a single element?

While watching a tutorial today where someone was creating a website using Bootstrap 4, I noticed that they applied both 'row' and 'container' as classes for the same div. This goes against Bootstrap documentation, which states that the ...

Partially Assessed Ajax Response

I am struggling with my ajax response as it seems to evaluate only some of the html, but not all of it. For instance, I have this code that replaces a div with the response from the request: eval(document.getElementById("test").innerHTML-xmlhttp.response ...

Troubles encountered with doctype while utilizing HtmlAgilityPack

My goal is to generate an XHTMl file from scratch using HtmlAgilityPack. Following the guidance provided in this Stack Overflow post, I attempted to add a doctype to the file: private static HtmlDocument createEmptyDoc() { HtmlDocument titlePage = new ...

Extracting text from a JSON file and populating text fields: A step-by-step guide

Utilizing this JSFIDDLE to duplicate text fields has been quite helpful. I am now looking to create JSON data based on the input from all the text fields. When it comes to updating a page, I need to extract the JSON data and accurately populate the corresp ...

Ways to eliminate dates from the text of listed items

Before finalizing their registration, users on our site are shown a review page. This panel displays all the items they have selected, creating a unique and variable list for each individual. However, each item in the list starts with a distracting date/ti ...

Python: Utilizing HTML User Input to Search Database

I am currently working on a project that involves searching a database using HTML. Here is the code snippet I am using: <form action="/question/search" method="POST"> <input id="search" type="text" placeholder="Search.." name="s ...

Is it possible to create a hidden scrollbar that functions seamlessly on a wide range of web browsers?

I'm currently working on a bootstrap website that features two columns that stretch to the height of the viewport, with any overflow being managed by scrollbars. This allows for the columns to be scrollable without affecting the overall page height. ...

How to vertically align text and image in QML Text by utilizing HTML tags with specified height and width dimensions

Here is the QML code snippet that I have been working with. What I have noticed is that when I specify both width and height for the image, it does not align vertically with the text. However, when I remove the specifications of height and width, they ar ...

What is the best way to populate the open areas on a grid using HTML?

I'm facing an issue with my note-taking app where, in a grid layout, short notes leave empty space. How can I push the cards below to move up and stick to the top card? I am currently using Bootstrap's grid layout for displaying the cards. You ca ...

Issues with Bootstrap Navigation Collapse Utilizing Data Attributes

I'm having trouble getting a Twitter Bootstrap 3 navbar to collapse using data attributes, as it is not expanding properly. Just to give some context, the project I am working on is an ASP.NET C# MVC project that runs on DNN (DotNetNuke) CMS. When I ...

Dynamic page url redirection involves creating search-engine friendly URLs for dynamic

After successfully incorporating URL rewriting into my PHP website, I am facing an issue with displaying the links as desired. mydomain.com/komet-india/Best-Indian-Hill-Stations-1/Delhis-Hotel The elements Best-Indian-Hill-Stations,1,Delhis-Hotel in the ...

Attempting to choose everything with the exception of a singular element using the not() function in Jquery

Within the mosaic (div #mosaique) are various div elements. My goal is to make it so that when I click on a specific div, like #langages or #libraries, the other divs become opaque while the selected div undergoes a size change (which works correctly in t ...

Unable to grab hold of specific child element within parent DOM element

Important Note: Due to the complexity of the issue, the code has been abstracted for better readability Consider a parent component structure like this: <child-component></child-component> <button (click)="doSomeClick()"> Do Some Click ...

Contrast in spacing: comparing display block versus inline-block

Today, I stumbled upon something quite intriguing that has left me puzzled. Consider the following HTML: <div class="a"><div class="b"></div></div> And CSS: .a { background: blue; } .b { display:inline-block; height ...

Select from a variety of backgrounds using the dropdown menu on the site

I seem to be struggling with javascript and php, but I know that in order to make this function properly, I need to utilize both languages. My goal is to set up a "select box" featuring 4 different images. There will be a default image displayed, but user ...

Creating a visually appealing background image using WordPress and PHP

Currently struggling with adjusting the width of an image that's set as a background in Wordpress. The text is perfectly centered, but the background image remains full size and I want it to match the container size... You can view the image at the t ...

Media queries for min-width and max-width are being disregarded in inline CSS styling

I am currently designing a pricing page for my Django application, featuring three columns. The intended design is to display all three columns side-by-side when the screen size is more than 768 pixels, and switch to a single column layout with pricing opt ...