Ways to eliminate the gap between two columns within a row, where the third column begins directly after the second one, causing a significant gap

I am facing an issue with the layout of my columns within a row. I have two columns inside the row and another column outside of the first row.

The problem is that the third column (col-3) starts at the end of col-1, causing a gap between col-1 and col-3 in desktop mode. This gap keeps expanding as big as col-2 is. How can I adjust this so that col-3 does not start after col-2's height, eliminating the large space between col-1 and col-3?

Here is how it currently looks:

|col-1  ||col-2|
|       ||     |
 ------- |     |
         |     |
---------
col-3
---------

This is how it should look instead:

|col-1  ||col-2|
|       ||     |
 ------- |     |
col-3    |     |
---------|     |

I have tried placing all columns inside one row and using order-first and order-last attributes without success. Here is my code snippet:

<div class="row">
  <div class="col-md-8">
   col-1
  </div>
  <div class="col-md-4"> 
     col-2
  </div>
  <div class="col-md-8 pull-left">
       col-3
  </div>
</div>

The expected result is for there to be no space after col-1 with col-2, ensuring that col-3 does not start after col-2's height. Any suggestions on how to achieve this would be appreciated.

Answer №1

If you're looking to solve this problem, I recommend utilizing the bootstrap flex CSS as it offers a more straightforward solution. Below is a sample code snippet that may help address your issue:

HTML

<div class="flex-row">
  <div class="flex-column">
    <div class="col-md-8 red">
      Column 1
    </div>
    <div class="col-md-8 red">
      Column 2
    </div>
  </div>
  <div class="col-md-4 green">
    Column 3
  </div>
</div>

CSS

.flex-row {
  display: flex;
  flex-direction: row;
}

.flex-column {
  display: flex;
  flex-direction: column;
}

.red {
  background: red;
  padding: 10px;
}

.green {
  background: green;
  padding: 10px;
}

For further reference and experimentation, check out this JS Fiddle link: https://jsfiddle.net/SJ_KIllshot/pqtxL9bs/

Answer №2

UPDATE

Check out this example of using flexbox on YouTube. Feel free to optimize the CSS code.

View on CodePen

https://codepen.io/matrixme/pen/wbqvZb

HTML Structure


    <div class="container">
        <div class="col--one">
            <div class="row video">
                <h2>Video</h2>
            </div>
            <div class="row forum">
                <h2>Forum</h2>
                <ul>
                    <li class="thread"></li>
                    <li class="thread"></li>
                    <li class="thread"></li>
                </ul>
            </div>
        </div>
        <div class="col--two">
            <h2>Related</h2>
            <ul>
                <li class="thumbnail">

                </li>
                <li class="thumbnail">

                </li>
                <li class="thumbnail">

                </li>
                <li class="thumbnail">

                </li>
            </ul>
        </div>
    </div>

CSS Styles

body {
  width: 100vh;
  margin: 0;
  top: 0;
  height: auto;
  background-color: #323232;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-content: flex-start;
}

h2 {
  width: 100%;
  text-align: center;
  font-size: 30px;
  font-weight: 300;
  font-family: Arial, sans-serif;
}

.container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-content: flex-start;
  width: 70%;
  background-color: #F2F2F2;
}

.col--one {
  position: relative;
  width: 70%;
  height: 100vh;
  background-color: #F2F2F2;
}

.col--two {
  position: relative;
  width: 30%;
  height: auto;
  background-color: #F2F2F2;
  border-left: 1px solid #CCCCCC;
}

.row {
  position: relative;
  display: flex;
  flex-direction: row;
  justify-content: center;
  width: 100%;
}

.video {
  position: relative;
  height: 350px;
  background-color: #EEEEEE;
}

.forum {
  flex-wrap: wrap;
  align-content: flex-start;
  height: 500px;
  background-color: #dddddd;
  position: relative;
}

ul {
  position: relative;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
  padding: 0;
  list-style: none;
  margin: 0;
}

.thread {
  position: relative;
  height: 50px;
  background-color: #ffffff;
  width: 90%;
}


.thumbnail {
  position: relative;
  height: 200px;
  background-color: #ffffff;
  width: 80%;
  margin-bottom: 20px;
}

Answer №3

.content {
  background: #999;
  border: 1px solid red;
}

.h50 {
  height: 50%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row eq-height">
    <div class="col-8">
      <div class="h-50 content">Atul</div>
      <div class="h-50 content">Rajput</div>
    </div>
    <div class="col-4">
      <div class="content h-100">Kumar</div>
    </div>  
  </div>
</div>

Instead of writing extensive CSS, you can modify the HTML structure slightly and make use of some simple styling.

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

Is it acceptable to nest an HTML document within the <body> tag of another HTML document?

Is it acceptable to nest an HTML document within the body tag of another HTML document? I am considering this approach in order to execute a JavaScript function on body load. Due to the dynamic generation of the main HTML code by a controller (Yii), I ...

How to Dynamically Load SVGs in Angular 10 Without Using IMG or OBJECT Elements

I have been exploring a more streamlined method for loading SVG files without relying on IMG or OBJECT tags, as it limits my ability to control fill colors through external CSS. While using inline SVG seems like the best option, managing numerous component ...

Receive alerts in Swift from WKWebView when a particular screen is displayed in HTML

Check out this sample HTML file I have. I'm currently using the WKWebView to display this HTML. My goal is to receive a notification in our Swift code when the user finishes the game and the "high score" screen appears, so we can dismiss the view and ...

Scaling down in CSS does not center the element properly using the margin 0 auto technique when the scaled element was larger than the container before scaling

Upon scaling down an element that previously overflowed its container, I noticed that margin: 0 auto no longer centers the element as expected. Interestingly, even using transform-origin: center center did not fix this issue. It seems that the auto margins ...

Addressing the delay of "Rasterize Paint" on mobile devices while implementing css3 opacity transitions

I'm currently working on a project that involves users navigating back and forth between modals. To achieve this, I decided to use CSS transitions to change the opacity from 0 to 1. However, I've encountered some issues with slow transitions. So ...

Can PHP retrieve data when the form submit function is overridden with AJAX?

I have customized the .submit function of a form on this webpage. It is being loaded inside the #mainContent in an "index.php" and I want the Submit button to only replace this #mainContent. My goal is to retrieve data from this form and send it to a .php ...

The appearance of Bootstrap 4 post cards on my website appear to be distinct

I came across a Bootstrap 4 card snippet that I wanted to integrate into my WordPress plugin. However, after integrating it, the display seemed different from the original form. My goal is to showcase posts in a 3-column layout. I noticed that some post ...

Utilizing a server-side PHP environment alongside a dynamic JQuery slider

I recently completed a PHP course on Codecademy and wanted to dive into a new project to further enhance my skills. However, I've hit a roadblock while working on it - specifically having trouble with the AJAX functionality. I'm struggling to ide ...

Issue with jquery .click function not triggering after CSS adjustment

In the process of creating a fun game where two players take turns drawing on a grid by clicking <td> elements. Currently, I have implemented the functionality to toggle the background color of a <td> element from black to white and vice versa ...

Could someone explain why Bootstrap columns are positioned in the same row but stacked on top of each other?

In the HTML structure I have provided below, you can see that there is a nested row inside another row, and within that inner row, there are two more columns (trying to split col-9 into more columns). However, the issue is that instead of appearing next t ...

Toggle visibility of div content when hovering over a link by leveraging the data attribute, with the div initially visible

I have a collection of links: <p><a href="#" class="floorplan initial" data-id="king"><strong>King</strong></a><br> 4 Bedrooms x 2.5 Bathrooms</p> <p><a href="#" class="floorplan" data-id="wood">< ...

Adjusting the tab order dynamically within a navigation menu

I am currently facing an issue where the focus is not being ignored when the drawer is closed. Even with the showDrawer reference set to false, I can still tab through links in the closed drawer. Setting tabindex="-1" on the <nav> element does not pr ...

What is the best way to ensure my arrow text remains properly positioned when using fullpage.js?

Seeking guidance from the web development community. I am currently working on a client's website that utilizes fullpage.js and encountering a persistent issue. On slide1 of the website, I am struggling to display the correct text next to the arrows. ...

Click on every link to reveal a hidden div

When I click on and select the first link in the mainPart, I want to automatically select the first subLink div in the secondPart while hiding the other subLink classes. This sequence should be maintained: when the second link is selected, the second sub ...

Center the logo within the NavBar

I'm having trouble centering the logo in my navbar and getting elements to spread out from the logo on both sides. I also want to position the buttons at the end of the navigation bar. Here's the desired layout In my code, I attempted to create ...

Modify the class of a div element depending on the value of a data attribute

I am faced with a situation where I have an iframe and two different sets of CSS codes - one for landscape orientation and one for portrait orientation. However, these are not specifically for the device's orientation but rather for the content loaded ...

Failure to validate two dates, even when they are both in date format within AngularJS

Although it may seem silly to ask, I am confused as to why this is not working. Even though all the values appear fine in debug mode. My goal is to display an error if the productionStartFrom date is before the current date. Controller scope.currentDate ...

"Upon invoking the console log, an empty value is being returned when attempting to access the

Why is console.log returning a blank line when trying to retrieve the value of a text input variable? HTML <label for="subject">Subject</label> <input type="text" id=" ...

The Flask server push appears to be functioning correctly, yet the EventSource.onmessage event is not triggering as expected

My implementation includes a route /stream that is designed to push the string 'test' every second. Upon accessing this URL (localhost:12346/stream) on Chrome, I observed that it opens a blank page and adds "test" to the page every second. This ...

Upgrading from Vuetify 2 to 3 involves replacing the deprecated styles like .v-application, rounded, and flat with

I need help with upgrading from Vuetify/Vue 2 to Vue 3. I don't have much experience in front-end development, but I am responsible for updating some old code to keep things running smoothly. The migration guide is not very clear and assumes a certain ...