What is the best way to display just four boxes on a screen that has a scrolling element?

I have a container element with a specific width (while the height can vary) and a scrollbar. Inside this container, there are boxes. Each box has dimensions equal to half of the width/height of the container with the scrollbar. My goal is to display only the first 4 boxes in the scrollable element before it starts scrolling. However, uncommenting the fifth box causes everything to break.

How can I resolve this issue?

.main {
    display: flex;
    flex-direction: column;
    width: 400px;
    height: 300px;
    overflow: auto;
    overflow-x: hidden;
    background-color: #dfdfdf;
}

.main .row {
    display: flex;
    flex-direction: row;
    flex-grow: 1;
}
  
.main .row .col-6 {
    width: 200px;
}
    
.main .row .col-6 div {
    border-width: 1px;
    border-style: solid;
    border-color: #fff;
    background-color: #888;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>

<div class="main">
    <div class="row no-gutters">
        <div class="col-6">
            <div>Rectangle 1</div>
        </div>
        <div class="col-6">
            <div>Rectangle 2</div>
        </div>
        <div class="col-6">
            <div>Rectangle 3</div>
        </div>
        <div class="col-6">
            <div>Rectangle 4</div>
        </div>
        <!--
        <div class="col-6">
            <div>Rectangle 5</div>
        </div>
        -->
    </div>
</div>

Link to jsFiddle

Answer №1

Include the height property in the styles for both .main .row and .main .row .col-6 as demonstrated below to prevent the reduction of heights:

.main {
  display: flex;
  flex-direction: column;
  width: 400px;
  height: 300px;
  overflow: auto;
  overflow-x: hidden;
  background-color: #dfdfdf;
}

.main .row {
  display: flex;
  flex-direction: row;
  height: 100%;  /* added */
  flex-grow: 1;
}

.main .row .col-6 {
  width: 200px;
  height: 50%;  /* added */
}

.main .row .col-6 div {
  border-width: 1px;
  border-style: solid;
  border-color: #fff;
  background-color: #888;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" />

<div class="main">
  <div class="row no-gutters">
    <div class="col-6">
      <div>Rectangle 1</div>
    </div>
    <div class="col-6">
      <div>Rectangle 2</div>
    </div>
    <div class="col-6">
      <div>Rectangle 3</div>
    </div>
    <div class="col-6">
      <div>Rectangle 4</div>
    </div>
    <div class="col-6">
      <div>Rectangle 5</div>
    </div>
  </div>
</div>

To view the implementation, check out this fiddle link: https://jsfiddle.net/1sdqn0hg/1/

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

Setting an ElementImpl property explicitly in EMF/GMF/Papyrus outside of the code

I am working with an EMF-model and its generated editor. In this model/editor, there is a connection between Element "Unit"(U) and "Specification"(S). My goal is to have a specific CSS style for S if at least one U satisfies S. Unfortunately, I have not fo ...

Display Modal Dialog box with AngularJs only on a specific section of the page

Currently, I am utilizing a Modal component from AngularJs UI Bootstrap, with detailed documentation available here My objective is to have the modal displayed in a specific section of the page (such as a designated DIV), darkening only its surrounding co ...

Alignment of text fields in a vertical manner

How can I vertically align text input fields using CSS? <form action='<?= $_SERVER['PHP_SELF']; ?>' method='post'> <p><label for='username' class=''>Username:</label& ...

Having trouble with the Wordpress JS CSS combination not functioning properly and unable to determine the cause

I recently set up a page on my WordPress site using the Twenty Seventeen theme. At the top of the page, I created a toolbar for users to easily adjust the font size and background color. Check out the image below for reference: See Image for Bar Here&apo ...

The error with Bootstrap4 alpha6 modal is in the process of transitioning

Currently, I am facing an issue with the bootstrap4 alpha 6 modal. The error message I am receiving is: Error: Modal is transitioning This occurs when attempting to re-trigger the same modal with dynamic data using a JavaScript function like this: funct ...

Steps for compiling SCSS to CSS using Angular-CLI and moving it to the assets directory

I am currently working on an Angular 5 project with an assets folder where I store my common CSS file and reference it in index.html. I now plan to create a new folder called "sasstyles" and place some .scss files there. When I compile or run the project ...

How can I make Firefox render the padding in a textarea the identical way as in a div?

In my efforts to ensure a consistent line width inside a textarea across IE8, Firefox, and Safari, I have encountered an issue where Firefox seems to add an extra pixel of padding compared to the other browsers. This results in text wrapping differently an ...

Incorporating an HTML image into a div or table using jQuery

I am a beginner in using JQuery within Visual Studio 2013. My question is how to insert an img tag into a table or div using JQuery? For example, I have a div and I would like to generate an image dynamically using JQuery. Or, I have a dynamically create ...

Personalizing Bootstrap 5 button designs and attributes for a customized look

In my project, I've set up my custom.scss file with the following code: $primary: #e84c22; $theme-colors: ( primary: $primary, ); @import "~bootstrap/scss/bootstrap.scss"; Currently, the color looks like this: https://i.sstatic.net/lbL ...

Bootstrap 5's gutter-x class is causing alignment issues with divs

My div alignment is getting thrown off due to the default .row class. The margin_left and right call it includes add gutters that cause borders to overlap my div. Although I have disabled gutters in this particular element, it leads to problems elsewhere o ...

Is it possible that the div's height is not being set to 0 pixels

<div style="height:0px;max-height:0px"> </div> It appears that setting the height of a div to 0 pixels is not having the desired effect. The div is still expanding to display its contents, so how can we stop this from occurring? ...

What is the best way to add borders to table cells that have a non-zero value

I am trying to create a table using a function and it currently looks like this: table { border: 1px solid; } td { width: 30px; height: 30px; text-align: center; } <table> <tr> <td>2</td> <td>0</td> ...

Press the button to automatically scroll to a designated div section

One of the challenges I'm facing involves a fixed menu and content box (div) on a webpage. Whenever I click on the menu, the content box should scroll to a specific div. Initially, everything was working fine. Here is a sample of it in action: http ...

Set the modal-dialog to have a fixed width, but ensure that it becomes responsive and adjusts size appropriately when the screen size becomes smaller

Utilizing the default Bootstrap3 CSS files for my project. I've come across an issue where the modal-dialog width is set to 600px by default, while I require it to be 1000px for my screen size. Regardless of the screen size exceeding 1000px, I need t ...

What is the best way to create a responsive navigation bar design?

I'm trying to create a unique navigation bar that features a hexagon-shaped logo and 3 buttons aligned in a specific way. Check out the screenshot for reference here. Although I've managed to align everything perfectly on a fullscreen (1920x1080 ...

Customize Tab indicator styling in Material-UI

Currently, I am attempting to customize the MUI tab by changing the indicator from a line to a background color. Through my research, I discovered that using TabIndicatorProps and setting a style of display:none eliminates the indicator completely. However ...

Weak Password Alert

After spending hours writing code for form validation and password strength checking, I encountered a roadblock. The form validates successfully, but the password strength indicator is not updating as expected. Despite double-checking the logic in my code ...

Mastering TailwindCSS: Enhancing your design with group-hover utilities for top, bottom, right, and left

In a div, there is an <img> tag and a <button> tag. I used group in the className of the div and group-hover:opacity-0 in the img tag, and it worked correctly. However, when I applied group-hover:top-1/2 to the <button> tag, it did not wo ...

Cursor customized image vanishes upon clicking anywhere on the page on a MAC in various web browsers

I am currently trying to set a custom image as the cursor using jQuery in this manner $(document).ready(function(){ $("body").css('cursor','url(http://affordable-glass.com/test/penguinSm.png),auto'); }); While this method works, ...

Having issues with RTL on MuiTextField when using special characters

Currently, I am working on a project where Mui is being used. I have successfully applied RTL according to the Mui guide. However, I am encountering a frustrating problem where special characters in MuiTextField are aligning to the left instead of the righ ...