Reordering Divs in Bootstrap 3 Specifically for Small Screens

Just getting started with my first responsive design project, and I'm wondering if it's possible to achieve something like this using Bootstrap 3. The goal is to switch from the current layout:

https://i.stack.imgur.com/lABXp.jpg

To

https://i.stack.imgur.com/nm3ra.jpg

The main change being transitioning from a 3 column layout on larger screens to moving the logo to the left and stacking the other two columns on smaller screens. Ideally, I would like to accomplish this using Bootstrap classes (col-xs-6 col-md-4 etc.) without duplicating content or using show/hide techniques. I really enjoy the grid layout that Bootstrap 3 provides for larger screens, so I'd like to maintain that while adjusting the layout for smaller screens.

Answer №1

DEMO: http://example.com/demo/123

DEMO w/edit: http://example.com/demo/123/edit

Achieving this layout without JavaScript is possible by utilizing Bootstrap 3 nesting, pushing/pulling classes, and floats clearing techniques:

<div class="container">
  <div class="row">
   <div class="col-xs-6 col-sm-4 col-sm-push-4 boxlogo">
    LOGO
   </div>
   <div class="col-xs-6 col-sm-8">
    <div class="row">
     <div class="col-sm-6 col-sm-pull-6 boxb">
      B
     </div>
     <div class="col-sm-6 boxa">
      A
     </div>
    </div>
    <!--nested .row-->

   </div>
  </div>
 </div>
 

Answer №2

Utilizing Bootstrap 3:

<div class="row row-fluid">
   <div class="col-md-6 col-md-push-6">
       <img src="some-image.png">
   </div>

   <div class="col-md-6 col-md-pull-6">
       <h1>Main Title</h1>
       <p>Lorem ipsum text of impact</p>
   </div>
</div>

This technique is effective because in ample space, the elements maintain their original positions. However, when the space shrinks and sizes reduce to medium, they stack on top of each other due to constraints in floating. It's worth noting that using different column widths also produces the desired effect.

For more details, visit:

Answer №3

When it comes to organizing elements that stack, it's important to consider the specific placement of each element. In your scenario, B and A cannot be grouped together because Logo needs to be positioned between them in certain cases. You have a couple of options to address this issue.

To achieve the desired layout without utilizing JavaScript, one effective solution is as follows: view demo here

<div class="container">
    <div class="logo col-sm-4 col-xs-6 col-sm-push-4">Logo<br/>Logo</div>
    <div class="contentB col-sm-4 col-xs-6 col-sm-pull-4">B</div>
    <div class="contentA col-sm-4 col-xs-6 col-xs-offset-6 col-sm-offset-0">A</div>
</div>

This implementation leverages row properties to adjust the positioning of A in the smaller screen (xs) case. The push/pull classes are utilized to rearrange the order of the divs for larger screens (sm). However, an issue may arise if Logo is taller than B, causing alignment problems. Unfortunately, this limitation cannot be easily resolved using only Bootstrap CSS.

Alternatively, incorporating JavaScript can provide a dynamic solution for moving the div when resizing the window. Check out the code snippet and example here: demo link

<div class="container">
    <div id="column1" class="row col-xs-6 col-sm-8">
        <div id="B" class="col-xs-12 col-sm-6">B</div>
        <div id="logo" class="col-xs-12 col-sm-6">Logo<br/>Logo</div>
    </div>
    <div id="column2" class="row col-xs-6 col-sm-4">
        <div id="A" class="col-xs-12 col-sm-12 ">A</div>
    </div>
</div>

Here is the JavaScript code snippet:

$(function() {
    $(window).resize(function() {
        var w = $(window).width();
        if (w < 768 && $('#column1').children().length > 1) {
            $('#B').prependTo( $('#column2') );
        } else if (w > 768 && $('#column2').children().length > 1) {
            $('#B').prependTo( $('#column1') );
        }
    });
});

Note: For more information on Bootstrap grid classes like push, pull, and offset, refer to the official Bootstrap grid documentation.

Answer №4

By utilizing jQuery, I successfully managed to resolve this issue by switching the content of the columns. Below is the HTML markup:

<div class="container">
    <div class="row">
        <div class="col-sm-4 swap-col">
            columns 1
        </div>

        <div class="col-sm-4 swap-col">
            columns 2
        </div>

        <div class="col-sm-4 swap-col">
            columns 3
        </div>
    </div>
 </div>

Furthermore, here is the accompanying jQuery script:

$(document).ready(function(){

    var col1_data,col2_data;

    col1_data = $(".footer-col:nth-child(1)").html();
    col2_data = $(".footer-col:nth-child(2)").html();

    var w = $(window).width();        

    if (w < 768)
    swap_columns();

    function swap_columns()
    {
        var w = $(window).width();
        if (w < 768)
        {
            $(".footer-col:nth-child(2)").html(col1_data);
            $(".footer-col:nth-child(1)").html(col2_data);
        }
        else
        {
            $(".footer-col:nth-child(1)").html(col1_data);
            $(".footer-col:nth-child(2)").html(col2_data);
        }
    }


    $(window).resize(function() {
        swap_columns();
    });
});

I trust that this solution proves effective for you.

Answer №5

I have created a simple jQuery script that allows you to play around with it in order to achieve the desired result. Essentially, it identifies mobile viewports and then rearranges divs based on their col-class:

let isMobile = window.matchMedia("only screen and (max-width: 760px)").matches;

if (isMobile) {
    var colArray = []; // Store all columns in an array
    $('.column').each(function () { // Select columns using a custom class selector

        if ($(this).hasClass('col-md-6')) { // <--- Customize your conditions here
            colArray.unshift($(this));  // If condition is met, column will be placed at the beginning of the array
        } else {
            colArray.push($(this));
        };
    });

    // Now you have an array of columns ordered by your conditions
    for (var i = 0, l = colArray.length; i < l; i++) {
        // Iterate through the array and insert each div before the next one
        colArray[i].insertBefore(colArray[i+1]);
    }
}

Answer №6

If you're looking to learn more about the responsive classes in bootstrap3, check out this resource for a detailed explanation.

Responsive Bootstrap3 css

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 could be the reason for my CSS animation with :hover @keyframes not functioning?

As a newcomer, I am struggling to understand why this code isn't functioning correctly. My goal is to create the illusion of a flying bird. This is my HTML: <img src="http://dl.dropboxusercontent.com/u/105046436/tw.png" /> <br> <div c ...

What is the best way to bring in this interface from the React-Particles-JS library?

I have been attempting to segregate my parameters from my JSX within the react-particles-js library. I organize my parameters in an Object: let config = { "particles": { "number": { "value": 50 }, ...

Display a unique and unconventional image as a dynamic full-screen background, ensuring the entire image is visible using React/RedwoodJS

Looking for a somewhat unusual solution here - I want to use an image as a background with all edges of the picture visible at all times. Typically, I would use the "cover" setting for this, but that doesn't achieve what I need (image included). After ...

Is IE8 compatible with HTML5 and CSS3?

My client has requested their website to be developed using HTML5 and CSS3. However, it has come to my attention that IE6 and IE7 do not have full support for HTML5 and CSS3. The client claims that IE8 does support these technologies, but I need more inf ...

Select either one checkbox out of two available options

My form includes two checkboxes, allowing users to click on both of them. I'm wondering if it's possible to set it up so that only one checkbox can be selected at a time. For example, clicking on the first checkbox would turn it on while turning ...

Switch Between Different Background Colors for Table Rows With Each Click

This script changes colors when a row in a specific column is clicked: $(document).ready(function(){ $("#rowClick").children("tbody").children("tr").children("td").click(function(){ $(this.parentNode).toggleClass("enroute"); }); }); CSS: .placed{ b ...

Struggling to determine the expense upon button activation - data remains stagnant

My coding project involves a basic ordering system where users can input an integer, click on an update button, and see the total cost displayed below. Despite my efforts, I've encountered issues with two different methods: Using plain JavaScript for ...

Tips for avoiding the freezing of bootstrap-select scroll when a large number of options are present

I have integrated a bootstrap-select with a total of 1000 options. However, I am encountering an issue where when I attempt to scroll down the list of options, it only goes down approximately 60 options and then freezes in that position. Can anyone provi ...

Guide to automatically adjust child div width to match parent container dimensions

I need assistance with structuring my HTML page, which consists of header, main, and footer sections. Below is the HTML and CSS code I'm utilizing: HTML code : <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

Assistance required in adjusting the clickable area of the link

While casually exploring a random website, I decided to experiment with some design elements. However, I encountered an issue where the link was only clickable on the left side and not the right. This is how it appears: There is a div containing a heading ...

Implementing automatic line breaks in Bootstrap

When setting the "overflow scroll able" option, I want it to only allow scrolling in the y direction and if x content overflows, a line break should occur. I tried applying 'white-space', but it didn't work as expected. <ul class="s ...

Customize the color of a Bootstrap btn-group

Is there a way to modify the background color of the active or selected button within a button group? <div class="btn-group" role="group" aria-label="Basic radio toggle button group"> <input type="radio" c ...

The .remove() method is ineffective when used within an Ajax success function

I am facing an issue with removing HTML divs generated using jinja2 as shown below: {% for student in students %} <div class="item" id="{{ student.id }}_div"> <div class="right floated content"> <div class="negative ui button compa ...

How to align Vimeo embed to the left side

Is there a way to align a Vimeo iFrame to the left instead of it automatically centering itself? I want more control over its alignment within my flexbox container. I've created a Flexbox container named .example-div with two internal flex containers ...

The clearfix feature fails to function properly with overflow:auto

I could really use some help with my CSS skills: Here is the HTML code I am working with: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Cluster Test</title> <link rel="stylesheet" href= ...

Exploring Django's view field for efficient data rendering

Is it possible to display multiple views for a single page/template? For instance, imagine a website where users can submit movie reviews and read others' reviews as well. Now, I need to add an edit button to the review pages but only want the button ...

Is there a way to transfer an element from one row to another using Bootstrap?

Can the grid layout transition smoothly from a desktop view to a mobile view using Bootstrap 4 or pure CSS flex classes? Desktop view: Mobile view : Currently, I have a solution in place but I want to avoid repeating content in two different places. Is ...

(React Native) Creating a visually appealing grid layout for displaying an array of item cards

I am working with two arrays named 'word' and 'definition' export default class Dictionary extends React.Component { constructor(props) { super(props); this.state = { word: [], definition:[], index: ...

Google Maps introduces a new feature that creates a blur effect on

Currently, I am utilizing the sample code provided below to superimpose an element on a Google map. <!DOCTYPE HTML> <html> <head> <title>Google Map Test</title> <style> html, body { margin: 0; ...

How can I make CSS text-overflow ellipsis trigger a pop-up to display lengthy text when clicked?

I'm encountering an issue with a lengthy sentence in a table cell which I've truncated using text-overflow to display ellipsis. Is there a way to enable users to click on the dots (...) and view the full text in a popup window? table { wi ...