Ways to eliminate space between column arrangement in Bootstrap

I've designed 3 cards containing 2 widgets each in the sidebar. To ensure responsiveness, I utilized bootstrap 4 column ordering.

One problem is when the right column is larger than the left one in a row of 2 columns, there's an awkward space between card 1 and card 2 due to size differences.

Any suggestions on how to eliminate this gap?

The Codeply link for a better responsive view:

Here's a preview of my code:

.left-panel {
  background: gray;
}

.box1,
.box2,
.box3 {
  background: white;
  height: 50px;
  border: 1px solid red;
  margin-bottom: 20px;
}

.right-sidebar, .bg-pink {
  background: pink;
}
<div class="container">
    <div class="row">
        <div class="col-12 bg-pink">
            <div class="row d-md-block">
                <div class="left-panel col-md-8  order-0 float-left">
                    <div class="box1 mt-2"><p>Box 1</p></div>
                </div>
                <div class="right-sidebar col-md-4 order-3 float-left">Widget 1</div>
                <div class="right-sidebar col-md-4 order-1 float-left">
                    <p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>

                
                </div>
                <div class="left-panel col-md-8 order-3 float-left">
                    <div class="box2 mt-2 ">Box 2</div>
                </div>
                <div class="left-panel col-md-8 order-5 float-left">
                    <div class="box3 mt-2">Box 3</div>
                </div>
            </div>
        </div>
    </div>
</div>

Real-life example:

enter image description here

Answer №1

It seems a bit unclear what the goal is here, but simplifying the classes could help:

<div class="container">
    <div class="row">
        <div class="left-panel col-md-8">
            <div class="box1 mt-2"><p>Box 1</p></div>
            <div class="box2 mt-2">Box 2</div>
            <div class="box3 mt-2">Box 3</div>
        </div>
        <div class="right-sidebar col-md-4">
            <p>Widget 1</p><p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>
        </div>
    </div>
</div>

If needed, you can add ordering classes back in later, but this adjustment should eliminate the gap. Depending on the content of the boxes and widgets, consider adding rows within each column as well.

Edit

For a cleaner structure, creating a reusable component for the right sidebar might be beneficial. Here's one approach:

<div class="container">
    <div class="row">
        <div class="left-panel col-md-8">
            <div class="box1 mt-2"><p>Box 1</p></div>
            <div class="right-sidebar d-md-none">
                <p>Widget 1</p><p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>
            </div>
            <div class="box2 mt-2">Box 2</div>
            <div class="box3 mt-2">Box 3</div>
        </div>
        <div class="right-sidebar d-sm-none d-md-block col-md-4">
            <p>Widget 1</p><p>Widget 2</p><p>Continue Widget 2</p><p>Widget 2 About to End</p><p>Widget 2 Ends</p>
        </div>
    </div>
</div>

Answer №2

To simplify your HTML and achieve the desired layout, consider structuring your elements like this:

<div class="container">
    <div class="row">
        <div class="col-md-8">
            BOXES GO HERE
        </div>
        <div class="col-md-4">
            SIDEBAR GOES HERE
        </div>
    </div>
</div>

The current issue is caused by conflicting floats with the Bootstrap grid system.


NEW For a better solution, try using row-eq-height:

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row row-eq-height" style="background-color: blue;">
    <div class="col-md-8">
      <div class="box1 mt-2">
        <p>Box 1</p>
      </div>
    </div>
    <div class="col-md-4">
      <p>Widget 1</p>
    </div>
  </div>
  <div class="row row-eq-height" style="background-color: red;">
    <div class="col-md-8">
      <div class="box1 mt-2">
        <p>Box 2</p>
      </div>
    </div>
    <div class="col-md-4">
      <p>Widget 2</p>
      <p>Continue Widget 2</p>
      <p>Widget 2 About to End</p>
      <p>Widget 2 Ends</p>
    </div>
  </div>
  <div class="row row-eq-height" style="background-color: yellow;">
    <div class="col-md-8">
      <div class="box1 mt-2">
        <p>Box 3</p>
      </div>
    </div>
    <div class="col-md-4">
      <p>Widget 3</p>
    </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

Executing a child function from the parent component in React 16

Ever since I upgraded to react 16, I've been encountering null in my console.log(this.child) The Main Component import EditReview from './partials/editReview' class VenueDetails extends Component { constructor(props) { super(props) ...

midway through processing HTML form

In the past, I have come across web forms that span multiple pages. If invalid information is entered on any page, it triggers an error message and requires the user to rectify their input. Despite this, I am having trouble locating resources to guide me ...

Using Bootstrap4 to merge rows into a single column or apply rowspan in Bootstrap

Hey there, I have a specific requirement that I need help with. Check out the image here. I want to enable the LCM information box when the LCM checkbox is checked. Below is my code: <div class="panel-body "> <div class="c ...

The focus is on the last row that was selected

I've noticed that when I check and select a row in ui-grid, only the last selected row is focused with a background color. Should all rows be automatically painted by default? Thank you. ...

Issue encountered while attempting to load bootstrap in NodeJS

I encountered an error while running my jasmine test case in node. The error message says that TypeError: $(...).modal is not a function. This error pertains to a modal dialog, which is essentially a bootstrap component. To address this issue, I attempted ...

Can we create a dynamic Context Menu?

I visited the following link: https://codepen.io/templarian/pen/VLKZLB After clicking on More Options, I need to display dynamically populated names like var Obj = [{name:"1st Item",taste:"sweet"},{name:"2nd item",taste:"spicy"}]; Instead of "Alert ...

What is the best way to have my transparent navigation bar float on top of my background in a parallax-scrolling website?

Currently, I am working on creating a website with a navigation bar that has no background and floats on top of a background image. However, I am facing an issue with my parallax-scrolling website. Whenever the page is scrolled to the second section, the n ...

Ways to make logo image go over/out of header

I am attempting to create a design where the logo image extends beyond the header and into the content as you scroll, giving the impression that the logo is oversized for the header and protruding from the bottom. Visit the website: For an example of a s ...

Regular expression for calculator input including a decimal point and negative sign

I'm in need of a regular expression to validate specific inputs: -0.12-12.56 -0.12+12.56 -0.12*12.56 -0.12/12.56 12.3--12.56 12.3+-12.56 12.3*-12.56 12.3/-12.56 -0.12--12.56 -0.12+-12.56 -0.12*-12.56 -0.12/-12.56 0.12-12.56 0.12+12.56 0.12*12.56 0.12 ...

Javascript is not recognizing if and elseif conditions

I recently developed an IQ Test using JavaScript. I implemented specific conditions for different score ranges, however, the test only displays the IQ score based on the last condition, regardless of the number of correct answers. Despite numerous attempts ...

Ensuring the existence of a MySQL database prior to executing a Node.js application

I am currently working on a Node.js/Express application that communicates with a MySQL server using Sequelize. I want to make sure that a particular database is created before the app starts running when using npm start. I think I need to create a one-ti ...

Passing ng-model data from a directive component to a controller in AngularJS

My inspiration from angular ui has led me to create a front-end library made up of components, all as angularjs directives. This allows users to easily implement directives with specific configurations and achieve the desired component results. Here is an ...

A guide to turning off tab discarding in Google Chrome with JavaScript

Is there a way to prevent Chrome from discarding tabs on chrome://discards/ using JavaScript so that the tab remains open even after closing the page or browser? I am aware that this can be achieved with an extension, but I am interested in creating my o ...

How do I resolve the issue of a non-iterable 'int' object?

1 How can I troubleshoot the error that is popping up? I believe the issue may be related to it being a dictionary. Any suggestions on how to fix this problem? views search(request): if "q" in request.GET: querystring = request.GET.get(" ...

How can I create an admin layout overlay in AngularJS?

Hi there, I have a question regarding using angularJS in an application we are currently developing at work. We need to implement an "overlay" admin style panel. For example, refer to this image: In the first scenario, the URL is "myapp.com/#/works". To ...

Troubleshooting Three JS Projection Challenges

I have been attempting to find the centroid of a polygon in 3D space and then convert its vertices to 2D screen coordinates. Unfortunately, the code I currently have is not producing the desired results. I would greatly appreciate any assistance with this ...

The Fetch API's Post functionality is currently experiencing issues

fetch('http://localhost:9000/api/app/contact', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ email: this.state.email, contactNumber: ...

Automatically trigger a Bootstrap 5.2 modal when the page loads

Currently, I've been utilizing Bootstrap in conjunction with JQuery and have a specific code snippet that displays a Modal when a page is loaded. However, with the latest version 5.2 of Bootstrap, there is a push to move away from using JQuery (which ...

NodeJS: Issue when implementing try/catch with async/await causing SyntaxError

As a newcomer to Node Js, I am struggling to understand why the code below is throwing a syntax error with catch(). I recently updated to Node JS V14. Any assistance would be greatly appreciated. async function demoPromise() { try { let message ...

The reload button retains functionality while being present within an Angular2 form, allowing for a seamless user experience. The

After some observation, I have realized that when a button is placed inside a form but has no connection or function related to the form, such as a back or cancel button, it unexpectedly reloads the entire page. For instance, let's consider having ...