"Utilizing a flexible grid system in Bootstrap to create 2 or

I am looking to create a layout with a variable number of columns using row span. Specifically, I want 3 columns on XS, 2 columns on SM (with the first 2 columns stacked on top of each other), and 3 columns on LG. I am unsure how to make the row span responsive for the SM screen size.

Here is a visual representation of what I want to achieve:

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

Here is the code I have been working on:

<div class="container text-center">  
    <div class="row">   

        <div class="col-sm-4 col-lg-3 col-lg-push-9"> 
            <div class="alert alert-warning">A</div>
        </div>   

        <div class="col-sm-4 col-lg-3 col-lg-pull-3">  
            <div class="alert alert-info">B</div>
        </div>     

        <div class="col-sm-8 col-lg-6 col-lg-pull-3">
            <div class="alert alert-danger">C</div>
        </div>  

    </div>
</div>

If you'd like to see the code in action, check out this Codepen link: http://codepen.io/nebitno/pen/ORxjLv

Answer №1

Figuring this out was quite challenging. After some brainstorming, I managed to come up with a solution that involves integrating a bit of jQuery for the desired outcome.

Below is the html structure where I made some modifications to the columns:

<div class="container text-center">  
    <div class="row">   

        <div class="col-sm-4 col-lg-3 col-lg-push-9"> 
            <div class="alert alert-warning">A</div>
        </div>   

        <div class="col-sm-8 col-lg-6 col-lg-pull-0 big">
            <div class="alert alert-danger">C</div>
        </div>  

        <div class="col-sm-4 col-lg-3 col-lg-pull-9 small">  
            <div class="alert alert-info">B</div>
        </div>     

    </div>
</div>

Additionally, I have included the jQuery script that dynamically switches the display based on the window width:

<script>
          var $iW = $(window).innerWidth();
          if ($iW < 768){
             $('.small').insertBefore('.big');
          }else{
             $('.big').insertBefore('.small');
          }
    </script>

It is important to note that the jQuery used here is not directly linked to window resizing post document load, but this can be adjusted by incorporating $(window).resize(function(){});

If you prefer to avoid JavaScript altogether, there is an alternative solution that involves duplicating one of the blocks. This method can work well if the content within that block is static. Below is the modified html structure:

<div class="container text-center">
     <div class="row">   

        <div class="col-sm-4 col-lg-3 col-lg-push-9"> 
            <div class="alert alert-warning">A</div>
        </div>   

         <div class="col-sm-4 small-screen-only">  
            <div class="alert alert-info">B</div>
        </div>   

        <div class="col-sm-8 col-lg-6 col-lg-pull-0 big">
            <div class="alert alert-danger">C</div>
        </div>  

        <div class="col-sm-4 col-lg-3 col-lg-pull-9 small">  
            <div class="alert alert-info">B</div>
        </div>     

    </div>
</div>

The CSS code provided ensures the duplicated block B only displays on smaller screens:

       .small-screen-only{
            display: none;
        }

        @media all and (max-width: 767px)
        {
            .small-screen-only{
                display: block
            }

            .small{
                display: none;
            }
        }

Personally, I recommend the CSS approach as it aligns better with the browser's native functionality. Even for dynamically added block content, there are ways to adapt this setup to suit your requirements.

Answer №2

To achieve a CSS-only solution, you can position column C absolutely in relation to the .row at the breakpoint sm and clear column B. Your HTML & CSS can remain the same with the following additional CSS:

@media screen and (min-width: 768px) and (max-width : 1200px) {

    .row {
        position:relative;
    }

    .col-sm-4:nth-child(2) {
        clear:left;
    }

    div[class^="col-"]:last-child {
        position:absolute;
        right:0;
    }

}

One drawback of this method is that the .row may not clear properly if the height of Column C exceeds the combined height of Column A + B, as discussed in Clearfix with absolute positioned elements.

You can view an updated version of your Codepen here.

Answer №3

After making some minor adjustments to the HTML code, I incorporated two different jQuery methods to accomplish the desired functionality. Feel free to choose the approach that best suits your needs.

HTML-

<div class="container text-center">  
    <div class="row">   

        <div class="col-sm-4 col-lg-3 col-lg-push-9"> 
            <div class="alert alert-warning">A</div>
        </div>   

        <div class="col-sm-4 col-lg-3 col-lg-pull-3 B">  
            <div class="alert alert-info">B</div>
        </div>     

        <div class="col-sm-8  col-lg-6 col-lg-pull-3 C">
            <div class="alert alert-danger" >C</div>
        </div>  

    </div>
</div>

jQuery(1st way)-

 var wH = $(window).innerWidth();
              if (wH < 992 && wH>=768){
                 $('.C:parent').each(function () {
                    $(this).insertBefore($(this).prev('.B'));
                 });
              }else if(wH<768 || wH>=992)
              {
                 $('.B:parent').each(function () {
                    $(this).insertBefore($(this).prev('.C'));
              });   
}
$(window).resize(function() {
          wH = $(window).innerWidth();
          if (wH < 992 && wH>=768){
             $('.C').insertBefore('.B');        
          }else if(wH<768 || wH>=992)
          {
             $('.B').insertBefore('.C');        
          }
          })

Check out this fiddle: http://jsfiddle.net/SkyT/wVVbT/150/

jQuery(2nd way)-

var wH = $(window).innerWidth();
              if (wH < 992 && wH>=768){
                 $('.C').insertBefore('.B');        
              }else if(wH<768 || wH>=992)
              {
                 $('.B').insertBefore('.C');        
              }
$(window).resize(function() {
              var wH = $(window).innerWidth();
              if (wH < 992 && wH>=768){
                 $('.C:parent').each(function () {
                      $(this).insertBefore($(this).prev('.B'));
                 });
              }else if(wH<768 || wH>=992)
              {
                 $('.B:parent').each(function () {
                      $(this).insertBefore($(this).prev('.C'));
                 });    
              }
              })

Checkout this fiddle:https://jsfiddle.net/SkyT/tst5g7ec/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

Leverage CSS to generate a visually appealing table of contents incorporating images and text blocks, perfectly aligned for an enhanced

I am in the process of designing a unique clip showcase on my website. The concept involves creating a table-like structure with a thumbnail image on the left and various information on the right, including a title, publication source, date, and descriptio ...

Creating a list element after clicking in ReactJS is achieved by using event handlers in

I have buttons and inputs that I want to use to generate a list item in the ul section at the end of my code. However, despite adding functions to handle this feature, it still doesn't work as intended. import { useState } from 'react'; impo ...

What is the best way to trigger a javascript modal to open automatically after a specific duration

Let's take an instance where my modal has the ID #modal1. It usually appears through a button-based action. ...

Determination of Vertical Position in a Displayed Table

I am trying to incorporate infinite scrolling with an AJAX-based loader in the body of an HTML table. Here is a snippet of my HTML code: <table> <thead> <tr><th>Column</th></tr> </thead> <tbody> ...

Problem with Bootstrap 3 navbar on mobile devices - not tappable or responsive

After years of using Bootstrap, I've come across a new issue with my implementation of a Bootstrap 3 Nav. While testing on a desktop browser with device emulation, the nav collapses and functions properly. However, when tapping on the header on an ac ...

Preserve the authentic picture along with a blur mask that can be dragged and applied to it

Is there a way to preserve the original image while having a draggable blur mask over it? If you want to see an example of a draggable blur mask over an image, you can check out this link: https://codepen.io/netsi1964/pen/AXRabW $(function() { $("#ma ...

Safari is displaying the HTML5 range element improperly

My HTML input type=range element is styled perfectly in Chrome, but it's a disaster in Safari. The track ball disappears or only partially renders when moved, and it seems to take forever to load. Any idea what could be causing this strange behavior? ...

Draggable element with a centered margin of 0 auto

Encountering an issue with jQuery UI sortable/draggable where the element being dragged, when centered using margin:0 auto, starts dragging from the left side of the container instead of the center where it is positioned. Check out this demo to see the pr ...

Adjust the background color of the arrow in an HTML Select element

My select menu looks different in Firefox compared to Chrome/Edge In Firefox, it displays an "arrow button" https://i.stack.imgur.com/BGFvO.png However, in Chrome/Edge, the arrow button is not present https://i.stack.imgur.com/PXNJn.png Is there a way ...

Creating multiple div elements with changing content dynamically

I am facing an issue with a div named 'red' on my website where user messages overflow the 40px length of the div. To prevent this, I want to duplicate the 'red' div every time a message is sent so that the messages stay within the boun ...

The head tag specification doesn't properly include the CSS file reference

I have a question regarding my HTML5 web page and the way I am referencing a CSS file in the head tag. Currently, I have it set up like this: <head> <link rel="stylesheet" href="css/style.css" type="text/css"/> </head> However, it d ...

Select the dropdown menu to access the last line of the table

My custom dropdown is causing a scroll to appear at the bottom of the table when it's open, making it hard to view all the elements. I want to find a way to either expand the body of the dropdown or enlarge it so that this doesn't happen. Can any ...

Deleting a database query once the modal is closed

When visiting the artist's about page on my website, clicking on the "button" will open a modal that retrieves information from a database. Currently, when you click on one artist button, close the modal, and then click on another artist, both artists ...

Steer clear of utilizing CSS pseudo-elements like :before and :after

I am seeking a solution to hide a <label> when viewing on a small mobile device. Below is the HTML code: <label class="page_createpassword_label" for="page_createpassword"> <span class="page_label_main">Span Text</span> <span c ...

Issue on my website specifically occurring on Google Chrome and mobile devices

Hello, I seem to be having a CSS issue. Interestingly, my menu (burger menu) works perfectly fine on Firefox but is not functioning properly on Chrome and mobile devices. When I click the button, the menu doesn't open. Has anyone encountered a simil ...

Decimal error causing Bootstrap grid column division issue

I've been attempting to divide the Bootstrap grid column into 20 grids using the code below: <div style="width: 4.999999998%; margin-top: -27.5px; margin-left: 25px;" class="col-md-.6 col-sm-6"> <div style="height: 32.5px; ...

What is the best way to format an image within an <a> element?

Below is a code snippet that includes a tags, with one containing an image. <div class="container"> <a href="#">Not styled</a> <a href="#"><img src="image.png"></a> </div> If I specifically want to style ...

What is the best way to horizontally align an inline div using CSS?

Is it possible to apply CSS to center an inline div that has an unknown width? Note: The width of the div cannot be specified beforehand. ...

The website does not display properly on larger screens, while showing a scrollbar on smaller screens

I'm encountering an issue with my website where I can't seem to find a solution no matter what I try. My goal is to ensure that my website looks consistent across all computer screen sizes. However, when viewed on larger screens, there's a ...

Side menu and grid display using HTML and CSS

Looking to revamp the layout of my angularJS website, specifically one page. Currently, information is displayed in a table format but I would like to switch it up by adding a left sidebar featuring a list of names (tiles). When a user clicks on a name, th ...