Issue with Bootstrap 4 grid not respecting col-* sizes when arranging columns

I have developed a bootstrap 4.1 application. My main goal is to design a group of 6 cards.

For wide screens in landscape mode, I aim to show 2 rows with 3 columns each.

[1] [2] [3]
[4] [5] [6]

However, on narrower screens in portrait mode, I want to display 3 rows with 2 columns where the cards maintain order vertically, like this:

[1] [4]
[2] [5]
[3] [6]

The challenge I'm facing is that the col-* settings are not being followed when the grid is set to column direction, resulting in this layout:

[1]
[2]
[3]
[4]
[5]
[6]

Here's how my HTML markup looks like:

<div class="container">
    <div class="row flex-column flex-md-row">
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 1</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 2</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 3</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 4</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 5</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 6</h5>
            <div class="card-body"></div>
        </div>
    </div>
</div>

To make sure the cards stack into multiple columns at smaller screen sizes, I've used the flex-column class for the row div and switched to flex-row at medium screen widths. This setup works well for wider displays but fails to create the desired layout on narrow screens. Any suggestions for improving this behavior?

Answer №1

There are two methods you can use to tackle this:

Method One: Rearranging Columns

To maintain the default column behavior, you may eliminate the flex-column class from the row and then rearrange each column using the order-* classes to position them as desired on the row.

Bootstrap Docs for Ordering Classes

Option Two: Setting a maximum height on the row

As rows function as flex containers, they span the entire width of the page if space permits or is explicitly defined. When applying flex-column, the row essentially rotates, so it's advisable to set a max-height on the row to signal when it should wrap its flex-items (columns in this case) into a new column.

.no-order {
   max-height: 300px
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
   <h1>Without Column Order</h1>
    <div class="row no-order flex-column flex-md-row">
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 1</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 2</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 3</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 4</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 5</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4">
            <h5 class="card-header">Card 6</h5>
            <div class="card-body"></div>
        </div>
    </div>
</div>

<!-- With Column Order -->
<div class="container mt-5">
   <h1>With Column Ordering</h1>
    <div class="row">
        <div class="card col-6 col-md-4 order-1 order-md-1">
            <h5 class="card-header">Card 1</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4 order-3 order-md-2">
            <h5 class="card-header">Card 2</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4 order-5 order-md-3">
            <h5 class="card-header">Card 3</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4 order-2 order-md-4">
            <h5 class="card-header">Card 4</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4 order-4 order-md-5">
            <h5 class="card-header">Card 5</h5>
            <div class="card-body"></div>
        </div>
        <div class="card col-6 col-md-4 order-6 order-md-6">
            <h5 class="card-header">Card 6</h5>
            <div class="card-body"></div>
        </div>
    </div>
</div>

Example in action: https://codepen.io/IvanS95/pen/aQjBdz

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

I prefer images to remain static in size while adjusting the browser dimensions

On my webpage, I initially specified the sizes of images in pixels like this: <IMG src="image.png" width=700px height=100px> However, when viewed on machines with different screen resolutions, the page didn't always display as intended. To ad ...

Sending the selected "Id" checkbox data to a Bootstrap Modal and then passing it on to the controller

I have a collection of articles that I need to manage, including the ability to delete specific articles or all articles using a modal popup window. Additionally, my View contains checkboxes for selecting articles. My plan is to retrieve the "Id" of each s ...

Ways to arrange specific columns within a table

I am facing an issue with aligning text in a table properly. In my table, I have two columns - first column and second column, both with colspan="4": It is supposed to look like this: <tr> <td colspan="4"> First column </td> ...

Using the Bootstrap 3 class visible-lg will cause the span to be placed on a separate line

Is there a way to prevent the class visible-lg from causing the span element to move to a new line? When using the following HTML code, the content displays on a single line: <p>Device is:<span>Unknown</span></p> However, when ut ...

Tips for ensuring the position of buttons remains consistent no matter the size of the image

I am facing an issue with maintaining the button location when an image is added. My goal is to ensure that the button's position remains horizontal consistently. Front-end: React CSS framework: semantic-ui-react render() { return ( &l ...

The webpage transforms with the holiday spirit

I have created multiple backgrounds for my website (available here), and I am looking for a way to have them automatically change based on specific occasions like Christmas, Halloween, etc. Manually changing them is an option, but where's the fun in t ...

Jquery unable to change the css property 'background-image'

I'm currently working on creating a photo viewer for my website by cycling through an array of pictures. The cycling aspect is functioning properly, but I am encountering difficulties when trying to transfer the message to the CSS. I'm not sure i ...

Scrolling on an iPhone's Div

When I tried to access a website () using my iPhone and other gadgets, I encountered an issue with scrolling through the content. Clicking on the "Insight" link at the top-left corner led me to another page. On a computer, I had no trouble scrolling smoo ...

Using Jquery to apply a class if the height of an image is larger than its width

Can someone assist me with a jQuery question regarding adding classes to images? I am currently working on a DEMO on codepen.io The issue I'm facing is that the JavaScript isn't adding a class to the image if the height is greater than the widt ...

My @media queries are causing me some issues

Struggling with my @media queries. Once I upload my website on github, it fails to scale properly. However, when testing it in Webstorm (JetBrains), everything seems to be working fine. Any assistance would be greatly appreciated! Here is the link to my w ...

Searching for table row elements using CSS and Vue.js

I am trying to alternate row colors in a table, but for some reason all rows end up being gray. <table> <tr v-for="i in item.env_vars" :style="{'background': index % 2 === 0 ? '#eee' : '#ccc' }"> & ...

Unexpected behavior of Codeigniter sessions

In my system, I am storing various session data such as id, type, isloggedin and a session called comp_city where I keep the name of a city. Recently, I set the value of comp_city to 'San Francisco' successfully. However, when I redirected to an ...

What is the best way to create a responsive sidebar that stays in place?

One feature that stands out on this site is the elegant sidebar: I've figured out how to create a fixed sidebar in css, but I'm struggling with making it stop scrolling at a specific point. I believe making it disappear at a certain width involv ...

My HTML gets rearranged by browsers, causing a change in appearance

UPDATE: I have discovered that the issue lies with Internet Explorer which alters the class attribute of an HTML element from: "<img class="blah".." to "<img class=blah..". This inconsistency only occurs in IE for me. It does not affect other attr ...

Keep the image proportion (square) the same while allowing the container height to automatically adjust

Encountering an issue while attempting to maintain image aspect ratio In a container with height: auto; max-width: 640px, I aim to showcase four 1:1 aspect ratio images per row The dilemma arises when the window's/container's width falls below ...

Developing a sleek hover effect with overlaid text on top of an image

I am currently working on a website using Bootstrap 4. One of the features I have implemented is a set of thumbnail images with text overlays that change color when hovered over. However, the hover effect disappears when hovering over the text. Any suggest ...

Align the image and text vertically within the <ul> tag

I'm trying to center-align the items in the navbar. https://i.stack.imgur.com/FmDYS.png Although the image looks centered, I believe it's because its size is stretching the navbar. How can I achieve this without changing the size of the picture ...

Having trouble with the footer in CSS? Seeking assistance

GOAL: I am seeking to adjust the placement of the footer based on various factors. More details can be found below: 1) The objective is to position the footer at the bottom of the screen when there is minimal content (or only 1 or 2 lines) on the page. Th ...

The problem of SVG rendering order requires a solution that does not rely on javascript

I am new to experimenting with inline svg, and I wanted to share my code: <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="350" height="350"> <defs> <style> .ring { transform-origin: 175px 175px; } ...

Font reference in IE and Chrome causing HTTPS to fail

Take a look at in both Internet Explorer and Chrome. The header tags (h1 through h6) all have font specifications that rely on a javascript fonts.com specification. However, there seems to be an issue with rendering properly. Interestingly, the fonts ar ...