I require varying numbers of columns on each row based on the breakpoint in Bootstrap4

After realizing it was easier than expected, I simply consolidated all columns into one row and the responsiveness now works perfectly. Thank you to everyone who helped. https://i.sstatic.net/4Sx41.png

I am utilizing Bootstrap 4 grid system to structure 3 rows with columns.

For lg breakpoint:
1st row with 3 columns
2nd row with 4 columns
3rd row with 4 columns
For md breakpoint:
1st row with 3 columns
2nd row with 3 columns
3rd row with 3 columns
For sm breakpoint:
1st row with 2 columns
2nd row with 2 columns
3rd row with 2 columns

<div class="example-container">
        <div class="example-row">
            <div class="example-content-main top">Main content</div>
            <div class="example-content-secondary top">Secondary content</div>
            <div class="example-content-third top">Third Content</div>
        </div>
        <div class="example-row">
            <div class="example-content-main">Main content</div>
            <div class="example-content-secondary">Secondary content</div>
            <div class="example-content-third">Third content</div>
            <div class="example-content-fourth">Fourth content</div>
        </div>
         <div class="example-row">
            <div class="example-content-main">Main content</div>
            <div class="example-content-secondary">Secondary content</div>
            <div class="example-content-third">Third content</div>
            <div class="example-content-fourth">Fourth content</div>
        </div>
       </div>

UPDATE: My custom SCSS using Bootstrap 4 mixins for breakpoints:

$grid-columns:      12;
$grid-gutter-width: 30px;

$grid-breakpoints: (
 xs: 0,
 sm: 320px,
 md: 510px,
 lg: 780px,
 xl: 1000px
);

.example-container {
 @include make-container;
}

.example-row {
 @include make-row;
}


//ROWS 1,2,3,4
.example-content-main {
background: gold;
@include make-col-ready;

@include media-breakpoint-up(md) {
  @include make-col(6);
}
@include media-breakpoint-up(lg) {
  @include make-col(4);
}
@include media-breakpoint-up(xl) {
  @include make-col(3);
}

&.top {
background: gold;
@include make-col-ready;

@include media-breakpoint-up(md) {
  @include make-col(6);
}
@include media-breakpoint-up(lg) {
  @include make-col(4);
}
@include media-breakpoint-up(xl) {
  @include make-col(3);
}
}
}

.example-content-secondary {
background: grey;
@include make-col-ready;

@include media-breakpoint-up(md) {
  @include make-col(6);
}
@include media-breakpoint-up(lg) {
  @include make-col(4);
}
@include media-breakpoint-up(xl) {
  @include make-col(3);
}

&.top {
background: white;
@include make-col-ready;

@include media-breakpoint-up(md) {
  @include make-col(6);
}
@include media-breakpoint-up(lg) {
  @include make-col(4);
}
@include media-breakpoint-up(xl) {
  @include make-col(3);
}
}
}

.example-content-third {
background: yellow;
@include make-col-ready;

@include media-breakpoint-up(md) {
  @include make-col(6);
}
@include media-breakpoint-up(lg) {
  @include make-col(4);
}
@include media-breakpoint-up(xl) {
  @include make-col(3);
}

&.top {
background: grey;
@include make-col-ready;

@include media-breakpoint-up(md) {
  @include make-col(6);
}
@include media-breakpoint-up(lg) {
  @include make-col(4);
}
@include media-breakpoint-up(xl) {
  @include make-col(6);
}
}
}

.example-content-fourth {...}

On lg screens, it displays correctly as shown in this image:

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

However, on md screens where I need 3 columns per row across 3 different rows, instead I get a layout like this:

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

Is there a way to keep the columns aligned even when on separate rows?

Answer №1

Upon running the following code, it became apparent that you encountered an issue with:

- The first row when using xs (as the number of columns reaches up to 18);

- The second and third rows when using md (as the number of columns goes up to 16).

    <!DOCTYPE html>
<html lang="en>
<head>
  <title>Molecoder's example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container-fluid">
  <h1>Hello! This was created by Molecoder to assist Theo Itzaris!</h1>
  <p>Resize the browser window to see the outcome.</p>
  <div class="row">
    <div class="col-xs-6 col-sm-4 col-lg-4" style="background-color:red;">.col-xs-6 .col-sm-4 .col-lg-4</div>
    <div class="col-xs-6 col-sm-4 col-lg-4" style="background-color:lavender;">.col-xs-6 .col-sm-4 .col-lg-4</div>
    <div class="col-xs-6  col-sm-4 col-lg-4" style="background-color:purple;">.col-xs-6 .col-sm-4 .col-lg-4</div>
  </div>
  <div class="row">
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:pink;">.col-xs-6 .col-sm-4 .col-lg-3</div>
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:black;">.col-xs-6 .col-sm-4 .col-lg-3</div>
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:lavenderblush;">.col-xs-6 .col-sm-4 .col-lg-3</div>
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:grey;">.col-xs-6 .col-sm-4 .col-lg-3</div>
  </div><div class="row">
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:orange;">.col-xs-6 .col-sm-4 .col-lg-3</div>
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:yellow;">.col-xs-6 .col-sm-4 .col-lg-3</div>
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:green;">.col-xs-6 .col-sm-4 .col-lg-3</div>
    <div class="col-xs-6 col-sm-4 col-lg-3" style="background-color:blue;">.col-xs-6 .col-sm-4 .col-lg-3</div>
  </div>
</div>

</body>
</html>

Test your code here.

This scenario involves having more than 12 columns in a row, whereas Bootstrap's grid system only allows up to 12 columns per page.

According to their documentation

If more than 12 columns are placed within a single row, each group of additional columns will wrap onto a new line as one unit.

Hence, achieving what you intended is not possible.

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 Flex still wrapping elements even when set to nowrap?

My goal is to create a layout with 4 blocks, where the first section contains 2 blocks each occupying 50% of the width, followed by 2 other blocks in the next section. I am utilizing the flex property for this layout. I want the flex-container to take up ...

Using jQuery .animate() leading to erratic input movements

I am currently utilizing jQuery's .animate() feature to create a smooth animation effect on the width of a <div> element when a child <input> is in focus. Nevertheless, I'm encountering an issue where the input field jumps up and down ...

Compatibility issues with jQuery observed in Firefox and Internet Explorer 11

Check out the code here: jsfiddle demo HTML CODE: <div class="first"> <!-- Part one --> <div class="acord_col"> <div class="img_class" id="exist_site"></div> <div class="intro_text"> </div> </div&g ...

Achieving Full Width Coverage for a div with 100% Width Using the Meta Viewport Tag "width=device-width"

Currently, I am working on optimizing my website to adjust for mobile browsers. In order to achieve this, I have two CSS files - one that is always included and another with media="screen and (max-width:500px)" To ensure proper functionality, I am utili ...

Adjust the text color based on the background image or color

Here on this site, I have designed the first div to display a dark image, while the second one shows a light background. I am aiming to adjust the sidebar text color based on whether it is displayed against the dark or light background. How can I achieve ...

Ways to apply a border-bottom to tr elements without affecting td elements

Is there a way to add a bottom border to <tr> elements without affecting <td> cells that have no border? I attempted the following approach, but setting border: 0 for <td> overrides the border for all <tr> elements as well, leaving ...

Increasing the height of a DIV that is positioned beneath another DIV with a set height

My goal is to convey my message visually. I hope you can understand without too many words. The issue I am facing is that when trying to stretch a 100% height DIV under another DIV with fixed height, it does not stretch properly. If you want to experiment ...

The "date" field seems to be malfunctioning on iOS devices exclusively

I'm having issues with a simple HTML CSS form that includes an input type="date" field which is not working on iOS devices. <fieldset class="form-group border p-3"> <div class="row mx-auto"> <legend class=" ...

"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 respo ...

Adjusting the structure of React Native Flex:1 to optimize performance and organization

In the layout above, the red component should fill the entire screen and the button should be positioned at the bottom. `<SafeAreaView style={{flex: 1, backgroundColor: 'red'}}> //red component <TouchableOpacity //purple component ...

How to customize the page background color in Next JS

I am currently working on a project using Next Js and have encountered an issue. I have a global.css file set up in the project, but I need to change the background color of a specific page without affecting the rest of the project. Although I have tried u ...

The components are not anchored to the window

In the header, I require: The logo to be positioned on the left side of the banner (without space) Both the banner and logo to always be centered on the page regardless of screen size Four images that should always be at the top right corner of the windo ...

Angular Square Grid Design

Attempting to create a square grid layout using CSS for ng-repeat items. Essentially, I am looking to have one big square followed by four smaller squares that combined have the same width and height as the big square. Here is my CSS: .container{ widt ...

Enhancing JQuery UI Accordion with simple ICONS

Is it necessary to use the Jquery CSS file for adding icons, or is there an alternative method? I'm hoping I can avoid using it as I am working within Wordpress and enqueuing the Jquery CSS seems like a complex task for me. Apologies for the basic q ...

How can I align the title of a cell to the left while centering the remaining content?

I have a dilemma with two table cells placed side by side that stack upon triggering a media query for an HTML newsletter. I wish to align the headlines "Be Ready" and "Stay Organized" to the left when the responsive code activates, but the use of "margin: ...

Dynamic CSS containers that adjust according to browser and screen dimensions

01) In my attempt to create a row with two content boxes, I encountered an issue. The first box is supposed to display an image and the second box text. However, I am struggling to make it responsive and interactive based on different browser and screen si ...

Issue with ngStyle function in Internet Explorer

Within my Angular 4 application, I have defined styles as shown below: [ngStyle]="{'border': getInterleaveColor(i)}" The following function is also included: getInterleaveColor(auditNumber) { var borderProperties = '2px solid'; ...

JavaScript Birthday Verification: Regular Expression Pattern

I am currently testing out JavaScript form validation. The information provided is not being sent to any database, it's just for format testing purposes. All the regex checks are functioning correctly apart from birth. It seems like there may be an i ...

Make sure PTable maintains a horizontal layout on any device with PrimeNG

When I view my Ptable on desktop or iPad, it looks like this: https://i.stack.imgur.com/ONqZV.png However, when I switch to a device like an iPhone X, it changes to this layout: https://i.stack.imgur.com/H2q7j.png I want the horizontal layout to displa ...

What is the best way to create a border for the active class nav-item in Bootstrap 4?

I am facing an issue with changing styles in the nav-item. I have created a separate style sheet and added the necessary code to make the changes. Surprisingly, the changes are reflecting well in Internet Explorer but not in Firefox. Can anyone guide me on ...