Stacking a Bootstrap column above another column

Is there a way to stack a bootstrap column on top of another column while maintaining its size and responsiveness?

Consider this scenario with 4 columns arranged in a row:

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

Upon clicking the Overlay btn, the E column should overlay the B column like this:

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

This is the test code I am currently experimenting with:

View live demo: https://jsfiddle.net/8pmt6ck2/2/

$(".a .btn").click(function() {
  $(".e").toggleClass("d-none");
});
.one .a {
  background-color: red;
}

.one .b {
  background-color: blue;
}

.one .c {
  background-color: green;
}

.one .d {
  background-color: pink;
}

.one .e {
  background-color: orange;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f0fdfde6e1e6e0f3e2d2a7bca0bca0">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">

<div class="container one">
  <div class="row">
    <div class="col-sm-3 a">
      <div class="vh-100 text-center">
        <span>A</span>
        <button class="btn btn-primary">Overlay btn</button>
      </div>
    </div>
    <div class="col-sm-3 b">
      <div class="vh-100">
        <span>B</span>
      </div>
    </div>
    <div class="col-sm-3 c">
      <div class="vh-100">
        <span>C</span>
      </div>
    </div>
    <div class="col-sm-3 d">
      <div class="vh-100">
        <span>D</span>
      </div>
    </div>
    <div class="col-sm-3 e position-absolute d-none">
      <div class="vh-100">
        <span>E</span>
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80e2efeff4f3f4f2e1f0c0b5aeb2aeb2">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>

I've applied the class d-none to column E to prevent it from affecting the layout as an actual column, since there should be only 4 columns including the overlay in the row.

The current implementation utilizing position-absolute partially works but the positioning is incorrect, and the width of the column doesn't match the other 4 columns (it appears wider):

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

The confusion also arises from the Bootstrap documentation which suggests limited positioning options using position-absolute such as top-, start-, end-, and bottom-:

https://getbootstrap.com/docs/5.0/utilities/position/

However, these predefined positioning classes do not cater to my requirements for maintaining responsiveness and matching existing column widths in the row.


How can I properly overlay my column E? The solution should ideally accommodate any number of columns in a row, not just restricted to 4 as demonstrated above.

Answer №1

Absolute positioning can often create issues and may not be necessary. Instead, try toggling the columns in a standard layout.

$(".a .btn").click(function() {
  $(".b, .e").toggleClass("d-none");
});
.one .a {
  background-color: red;
}

.one .b {
  background-color: blue;
}

.one .c {
  background-color: green;
}

.one .d {
  background-color: pink;
}

.one .e {
  background-color: orange;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="187a77776c6b6c6a7968582d362a362a">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">

<div class="container one">
  <div class="row">
    <div class="col-sm-3 a">
      <div class="vh-100 text-center">
        <span>A</span>
        <button class="btn btn-primary">Overlay btn</button>
      </div>
    </div>
    <div class="col-sm-3 b">
      <div class="vh-100">
        <span>B</span>
      </div>
    </div>
    <div class="col-sm-3 e d-none">
      <div class="vh-100">
        <span>E</span>
      </div>
    </div>
    <div class="col-sm-3 c">
      <div class="vh-100">
        <span>C</span>
      </div>
    </div>
    <div class="col-sm-3 d">
      <div class="vh-100">
        <span>D</span>
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4969b9b808780869584b4c1dac6dac6">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>

If you do require both columns to be visible simultaneously, consider toggling the contents instead of the columns themselves using absolute positioning. The style class on the column can also be toggled in this scenario.

$(".a .btn").click(function() {
  $('.b-e').toggleClass('b e');
  $(".b-e .col-overlay").toggleClass("d-none");
});
.one .a {
  background-color: red;
}

.one .b {
  background-color: blue;
}

.one .c {
  background-color: green;
}

.one .d {
  background-color: pink;
}

.one .e {
  background-color: orange;
  left: 0;
  right: 0;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c3e3333282f282e3d2c1c69726e726e">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"><div class="container one">
  <div class="row">
    <div class="col-sm-3 a">
      <div class="vh-100 text-center">
        <span>A</span>
        <button class="btn btn-primary">Overlay btn</button>
      </div>
    </div>
    <div class="col-sm-3 b-e b position-relative">
      <div class="vh-100 col-overlay position-absolute d-none">
        <span>E</span>
      </div>
      <div class="vh-100">
        <span>B</span>
      </div>
    </div>
    <div class="col-sm-3 c">
      <div class="vh-100">
        <span>C</span>
      </div>
    </div>
    <div class="col-sm-3 d">
      <div class="vh-100">
        <span>D</span>
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c6e6363787f787e6d7c4c39223e223e">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>

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

What is the best way to set the keyframes CSS value for an element to 0%?

Would like to create a progress indicator div using CSS animations? Check out the JSBin link provided. The desired behavior is to have the div width increase from 30% to 100% when the user clicks on stage3 after stage1, rather than starting from 0%. Althou ...

Display XML information when a row in the table is selected

I am working with an XML data sheet and utilizing ajax to extract information from it in order to generate specific tabs and construct a table of data. However, I am encountering a challenge when attempting to display details in adjacent divs once a row of ...

Learn how to assign an image to a div element when it is collapsed, and then reveal the content when clicked on to expand

I am working on a three div with expand collapse functionality. When I expand one div, I want to set some image to the other divs. And when I go back to normal mode, I need the original content that was inside each div. $("a.expansion-btn").click(functi ...

Modifying the class of an HTML element using JavaScript

Is it possible to dynamically change the class of an HTML element based on a user's selection with a radio button? I am facing an issue where I receive the following error message: "Error: missing ) after argument list Source File: website Line: 1, C ...

Unique float structure within a division of divs

Can divs be floated within another div like this? Here is the code: <div class="event_month"> <div class="event1">1</div> <div class="event2">2</div> <div class="event3">3</div> <div class="event4">4</di ...

Altering the appearance of an input field upon submission

https://jsfiddle.net/3vz45os8/7/ I'm working on a JSFiddle where I am trying to change the background color of input text based on whether the word is typed correctly or incorrectly. Currently, it's not functioning as expected and there are no e ...

Is it possible to use CSS to create a gap between the cursor and the placeholder text within an input field?

Could you please assist me with a bug I have encountered on an older version of Firefox for OSX (37.0.2)? I have included a screenshot of the issue here: https://i.sstatic.net/dzK0G.png Is there a way to use css to move the first character of the placehol ...

Restangular will maintain the Http Content-Type as it is

I encountered an issue while trying to send a file as part of my form. Despite changing the content type to "multipart/form-data", the console still indicates that it is using application/json;charset=utf-8. As a result, Node.js (with Sails.js framework) t ...

Tips for creating a responsive carousel slider for images

No matter how much I've tried, I can't seem to find a solution on how to make my image responsive along with the caption. Below is my HTML code: <section id="banner"> <div class="banner-bg"> <div class="banner-bg-item ...

Unexpected error occurs when modifying HTML5 video source using JQuery on Internet Explorer

Currently, I am working on developing a web application using asp.net, Bootstrap, and JQuery. While testing it on LocalHost, I encountered an issue that needs debugging. The navigation bar of my application has a dropdown menu with links to tutorial video ...

What is the best way to make grid-column-start and span work seamlessly together?

I'm having some trouble with creating a table using the grid function in CSS. Specifically, I am stuck on how to add dynamic categories at the top without hardcoding styles for each category count. I have tried using the grid-column-start property to ...

Troubleshooting a malfunctioning PHP script that verifies user ranks

I wrote a PHP script to determine the user's rank and grant access to view specific content based on that rank. Unfortunately, the script is not functioning as intended. Regardless of the user's rank, the content is still displayed. Here is the ...

Animating the height with jQuery can result in the background color being overlooked

For those curious about the issue, check out the JSFiddle. There seems to be an anomaly where the background of the <ul> element disappears after the animation on the second click. Oddly enough, even though Firebug shows that the CSS style is being a ...

Position the Radio Buttons on Top of the Image

I need assistance with positioning radio buttons on top of an image at a specific location. Both the image and radio buttons are currently in a Div container, but I am unsure about the next steps. Below is where I would like to place the radio buttons (red ...

Ways to alter the color of an icon when hovering over it

Is there a way to change the color of a material icon inside an IconButton material component when hovering over the IconButton? I've tried adding a class directly to the icon, but it only works when hovering over the icon itself and not the IconButto ...

Displaying an alert on a webpage that shows a label input using JavaScript and

I'm currently working with HTML5 and JavaScript and I'm facing a challenge. I want to create a feature where users can input any word into a label, and when they click on a button, an alert is triggered with the given text. However, despite my ...

How can you ensure that this intricate text on the page is verified using Selenium with Java?

I need to confirm if the following HTML code is present on the page, which contains // characters, etc. I am using selenium and Java for this task. <div class="powatag" data-endpoint="https://api-sb2.powatag.com" data-key="b3JvYmlhbmNvdGVzdDErYXBpOjEyM ...

I created a news platform using Next.js server-side rendering, but I'm having trouble with the export const metadata = { title: "TEST" } not functioning as intended

After building my News Website using Next.js with Server Side Rendering, I encountered an issue with setting the page title. Despite using export const metadata = { title: "TEST" }, the title remained unchanged. The metadata object appears to be ...

The issue of AngularJS failing to bind object properties to the template or HTML element

Just dipping my toes into angularJS, following Todd Motto's tutorials, and I'm having trouble displaying object properties on the page. function AddCurrentJobs($scope){ $scope.jobinfo = [{ title: 'Building Shed', description: ...

Analyzing user input in PHP against a mysqli database

I'm currently working on an online quiz application. I have everything in place with the form, and it's successfully sending emails to the administrator. However, there's a specific functionality that I want to implement: I need the script ...