Column-Flexbox with vertical scrollbar: background color is cropped

Is there a way to always keep the red background of the main content visible, regardless of scrolling or the height of flex-items within each section?

Currently, the issue I am experiencing is that when I scroll, the background of the main div disappears. The background is set to be the same height as the body, which is not what I want.

Changing the align-items property of the main content from stretch to flex-start causes the background to extend correctly for taller flex-items, but not for shorter ones.

If I leave the align-items property as stretch, another problem arises where the background shifts to the bottom when scrolling, resulting in it being lost from view.

How can I ensure that my red/orange background remains visible at all times?

Answer №1

It seems that achieving this layout using flexbox is not possible. However, I have discovered that if the container is set as a grid container instead of a flexbox, you can arrange the grid items in a row successfully.

To achieve this, apply grid-auto-flow: column to the grid container to place the flex-item boxes horizontally with the appropriate backgrounds - please refer to the demo code snippet below:

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}

.flex-container { /* added */
  display: grid;
  grid-auto-flow: column;
  overflow: auto;
}

.flex-item {
  background-color: red;
  border: solid 1px white;
}

.box {
  width: 300px;
  height: 300px;
  border: solid 2px black;
  margin: 4px;
}

.orange {
  background: orange;
}
<div style="display:flex; height:100%;">
  <div style="flex:0 0 300px; background-color:green; height:100%; overflow:auto;">
    We are two divs, I am the sidebar and the one next to me is the main content. Each of us has its own scrollbar. But my red brother has an issue with his background if you scroll...<br><br> How could we solve that?
    <br><br><br> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum
  </div>

  <div class="flex-container">
    <div class="flex-item">
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
    </div>
    <div class="flex-item orange">
      <div class="box">i am a box. The background behind me should be orange.</div>
      <div class="box">i am a box. The background behind me should be orange.</div>
      <div class="box">i am a box. The background behind me should be orange.</div>
      <div class="box">i am a box. The background behind me should be orange.</div>
      <div class="box">i am a box. The background behind me should be orange.</div>
    </div>
    <div class="flex-item">
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
    </div>
  </div>
</div>

Answer №2

Returning to this code snippet, I wanted to demonstrate how a layout can be created without utilizing Flexbox or Grid. (Please note that the design is not responsive, so make sure to view it in "full page" mode.)

* { -moz-box-sizing:border-box;box-sizing:border-box; }
html, body {
    width:100%;
    height:100%;
    padding:0;
    margin:0;
}

.row {
    height: 100%;
}
.row>.col {
    overflow: auto;
    height: 100%;
    float: left;
    width: 300px;
    background: green;
}
.row .row>.col {
background: none;
    overflow: visible;
    height: auto;
    width: 308px;
    border-bottom: 0 none;
}
.row>.colWide { /* prefixed gradient angles are different from the final version of the standard */
    background: -moz-linear-gradient(0deg,#fff 1px,red 1px,red 307px,#fff 307px,#fff 309px,orange 309px,orange 615px,#fff 615px,#fff 617px,red 617px,red 923px,#fff 923px,#fff 924px,transparent 924px);
    background: -webkit-linear-gradient(0deg,#fff 1px,red 1px,red 307px,#fff 307px,#fff 309px,orange 309px,orange 615px,#fff 615px,#fff 617px,red 617px,red 923px,#fff 923px,#fff 924px,transparent 924px);
    background: linear-gradient(90deg,#fff 1px,red 1px,red 307px,#fff 307px,#fff 309px,orange 309px,orange 615px,#fff 615px,#fff 617px,red 617px,red 923px,#fff 923px,#fff 924px,transparent 924px);
    float: none;
    width: auto;
}
.row>.colWide:after { /* to fake an even bottom border without further complicating the backgrounds */
content: '\a0';
    display: block;
    clear: both;
    height: 0;
    overflow: hidden;
    border-bottom: solid 1px #fff;
    width: 924px; /* set the width and border as desired */
}

.col {
    border:solid 1px #fff;
    
}
.box {
    width:300px;
    height:300px;
    border:solid 2px black;
    margin:4px;
}
<div class="row">
    <div class="col">
        We consist of two divs: I serve as the sidebar, while my adjacent counterpart displays the main content. Each of us has our own scrollbar. However, my red sibling faces a background issue when you scroll...<br><br>
        How can we resolve this?
        <br><br><br>
        Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum
    </div>

    <div class="row col colWide">
        <div class="col">
            <div class="box">i am a box. The background behind me should be red.</div>
            <div class="box">i am a box. The background behind me should be red.</div>
            <div class="box">i am a box. The background behind me should be red.</div>
            <div class="box">i am a box. The background behind me should be red.</div>
            <div class="box">i am a box. The background behind me should be red.</div>
        </div>
        <div class="col">
            <div class="box">i am a box. The background behind me should be orange.</div>
            <div class="box">i am a box. The background behind me should be orange.</div>
            <div class="box">i am a box. The background behind me should be orange.</div>
            <div class="box">i am a box. The background behind me should be orange.</div>
            <div class="box">i am a box. The background behind me should be orange.</div>
        </div>
        <div class="col">
            <div class="box">i am a box. The background behind me should be red.</div>
            <div class="box">i am a box. The background behind me should be red.</div>
        </div>
    </div>
</div>

Answer №3

In my opinion, the excessive use of height:100% is causing issues in your layout. The height of the parent flex container is restricted to the viewport height, preventing it from expanding to fit the content and resulting in the content overflowing beyond the background.

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}

.flex-row {
  display: flex;
  min-height: 100%;
  align-content: stretch;
}

.flex-row .flex-row {
  overflow: auto;
}

.flex-item {
  background-color: red;
  border: solid 1px white;
}

.box {
  width: 300px;
  height: 300px;
  border: solid 2px black;
  margin: 4px;
}

.orange {
  background: orange;
}
<div class="flex-row">
  <div style="flex:0 0 300px; background-color:green; overflow:auto;">
    We are two divs, I am the sidebar and the one next to me is the main content. Each of us has its own scrollbar. But my red brother has an issue with his background if you scroll...<br><br> How could we solve that?
    <br><br><br> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum
  </div>

  <div class="flex-row">
    <div class="flex-item">
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
      <div class="box">i am a box. The background behind me should be red.</div>
    </div>
    <div class="flex-item orange">

      ... (remaining code section) ...

    </div>
  </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

How is it possible for a parameter sent via GET to persist through multiple requests?

Within my JSP file, I have implemented an HTML form that utilizes the GET method to send data to my servlet. <form method="GET"> <input name="cmd" type="hidden" value="firstValue"/> ..... </form> Upon triggering another form ...

Create dynamic CSS pseudo content for before/after in Angularjs automatically

I have a question regarding applying pseudo elements dynamically using Angular. I have a div element where I need to push data to the content attribute in CSS dynamically from my scope. How can I achieve this with Angular? Here is a sample CSS: .bar:befo ...

What could be causing the HTML <DATALIST> dropdown to display next to its input rather than below it?

I have an input linked to a datalist, and it's functioning correctly. However, when I initially open the dropdown, the option list appears next to (right of) the input. After entering a few characters to narrow down the list, it then shifts below the ...

Creating a variety of Flexslider slideshows on the fly

Check out this snippet of code: <?php foreach ($objVideos as $objVideo) : ?> jQuery('#carousel-<?php echo $objVideo->id; ?>').flexslider({ animation: "slide", controlNav: false, animationLoop: false, ...

From one corner to the opposite, creating diagonal dividers

I want to create a web page similar to the one below. Although I specialize in back-end programming, I am eager to expand my knowledge of design in order to build visually appealing sites. My goal is to include 7 diagonal divs (each representing a color of ...

What are the best prefetching tactics for Asp.net?

The header of the /pages/dashboard/default.aspx contains the following links: <link rel="prefetch" href="/pages/agent/statistics/"> <link rel="prefetch" href="/pages/agent/statistics/transactions/"> <l ...

Can the @keyframes in CSS3 be modified?

Exploring CSS3 animations has been a fun project for me, and I want to push the boundaries of what is possible. Currently, I have an infinite animation cycle that is working perfectly. However, I'm curious if it's possible to dynamically change ...

Issue with Cascading Dropdowns in jQuery

I am currently experiencing issues with a piece of code on my website (also accessible via this link). The code consists of 2 select fields that should display different data based on the selection made. However, I have identified 2 bugs within the code. ...

How to extract a variable from the selected value of a dropdown menu in HTML using PHP

I've searched through this response enter link description here Unfortunately, I'm having trouble getting my code to work. Here's what I have in my selectcategory.php file. I'm trying to set up the variable $selectedcategory in this fi ...

Changing the URL for the onClick event of a button in Laravel through AJAX

I am working on a basic form that includes a "Submit" button and an extra "Add" button in a blade template. When the form is submitted, there is an ajax call to a controller for validation of the provided value. Depending on the response from the controlle ...

I am eager to add information retrieved from one table in the database into another table using PHP

When creating a relationship between two tables - one containing items and the other containing categories for those items, I encountered an issue. While I am able to populate the dropdown list with values from the categories table, I am facing difficult ...

Refresh the Google Maps location using GPS coordinates

Currently, I am working with an Arduino that has a GPS chip and processing NMEA strings with Python. I have an HTML file set to auto-refresh every x seconds to update the marker's position. However, I would like to update the position information with ...

Accessing mp3 files from the www folder using Phonegap

How can I load an mp3 file on iOS from the www folder located next to my HTML? I am using phonegap and have followed the example provided in the documentation: The code works fine on iOS and Android when loading an mp3 from the web, but I encounter an iss ...

Tips for retrieving the primary key of the highlighted row in bs_grid

I've integrated bs_grid to showcase a lineup of company names, each associated with a unique GUID identifier. Here's how I've set up the grid: $(function() { $('#grid1').bs_grid({ ajaxFetchDataURL: 'CompaniesList.asmx/G ...

Utilizing JSON AJAX and JQUERY for Autocomplete in C# MVC

Model-View-Controller Example public class HomeController : Controller { public JsonResult UniversalSearch() { List<UniversalSearchBar> EmployeeDetails = new List<UniversalSearchBar>(); EmployeeDetai ...

Optimizing background images for various mobile devices using PhoneGap

Currently in the process of creating a cross-platform app for iOS and Android using Phonegap. Facing an issue with implementing a background-image as it gets cropped or distorted on various mobile devices due to size differences. Managed to address this ...

Refusing to allow any alterations to the form until it has been saved

I have a form with various input elements such as text input, radio buttons, and a select dropdown. My goal is to allow the selection of a radio button only once at a time. If the user selects the same radio button again, I want to prevent it unless they ...

Forming a header area by utilizing a 960 Grid container

I'm in the process of designing a website utilizing the 960 Grid system. The header consists of three parts: the logo, the navigation, and the login form. I've implemented media queries to center both the logo and the login section when the pag ...

Understanding how much of a video stream has been viewed by a user using PHP

I am currently involved in a project that involves displaying video advertisements to users, and after 60 seconds, users are presented with a link. My supervisor wants to ensure that users watch the entire 60 seconds of the video. I am now wondering if th ...

Having trouble with the mouse trail code on codepen.io

I am currently attempting to integrate this specific CodePen sample into a Hugo template called Somrat Theme. I'm facing challenges in deciding where to place the HTML file; only the 'no cursor' section should go into style.css, and I need ...