Bootstrap change column number at breakpoints - css

I'm having some trouble with a layout I'm producing. I am dealing with blocks of content that are generated from my database, so I'm essentially ending up with:
<div class="container">
<div class="row">
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
... etc
The problem is, as the columns are floated they are ending up stacking on top of each other in the event that one has more content than the other.
I know that technically I should be using rows like so:
<div class="container">
<div class="row">
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
</div>
<div class="row">
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
</div>
<div class="row">
<div class="col-sm-6 col-lg-4 record"></div>
<div class="col-sm-6 col-lg-4 record"></div>
Except, obviously, on larger screens I need to have three elements per row.
Is there a way around this or a CSS rule I can add to clear the floated elements and keep everything lining up at both the lg and sm breakpoint?
Demo: http://codepen.io/EightArmsHQ/pen/RWoMmM

You could add something like this:
/* On medium screens, clear every 2 elements starting at 3rd element */
#media(min-width: 768px) and (max-width: 1199px){
.row > div:nth-child(2n+3){
clear: both;
}
}
/* On large screens, clear every 3 elements starting at 4th element */
#media(min-width: 1200px){
.row > div:nth-child(3n+4){
clear: both;
}
}

Related

Based on different resolutions, how to make design responsiveness using bootstrap grid system?

<div class="row">
<div class="col-xl-4 col-md-12 col-sm-12 col-lg-4 data-one">
<one />
</div>
<div class="col-xl-4 col-md-12 col-sm-12 col-lg-4 dashboard-two">
<two />
</div>
<div class="col-xl-4 col-md-12 col-sm-12 col-lg-4 dashboard-three">
<three />
</div>
</div>
#media screen and (min-width: 500px) and (max-width: 992px) {
.data-one {
}
.data-two {
}
.data-three {
}
}
Using bootstrap grid system, how to make responsiveness. like below.
enter image description here
and i am also having issue like, when elements touch each other, getting collide(intersecting each other).
Any example will be helpful thanks.
So on a Bootstrap row with three items, using col-md-12 as one of the classes doesn't make much semantic sense. Essentially because a col-12 is supposed to span the full-width. For a responsive Bootstrap design, it would be semantically correct to use col-lg-4 for large screens, col-md-4 for medium screens, and it will use col-sm-4 for mobile devices like phones. I added w-100 for 100% width and text-center to align text to the center. See the changes I made below.
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
</head>
<div class="container w-100">
<div class="row text-center">
<div class="col-lg-4 col-md-4 col-sm-4">
One
</div>
<div class="col-lg-4 col-md-4 col-sm-4">
Two
</div>
<div class="col-lg-4 col-md-4 col-sm-4">
Three
</div>
</div>
</div>

Bootstrap css block among each other

this is my situation with bootstrap I put 2 blocks next to each other:
class="col-6 col-sm-6 col-md-6 col-lg-6"
At a certain point when we reach 1000px I want the boxes to be among each other like this:
this now only happens when i reach 576px,
does someone how i can accomplish this.
code:
<div class="col-sm-6">
<div class="modal-dialog modal-dialog-enable">
<div class="modal-content modal-dialog-enable" >
<div class="modal-header modal-dialog-enable">
</div>
<div class="modal-body modal-dialog-enable">
<p>
</p>
</div>
<div class="modal-footer modal-dialog-enable">
</div>
</div>
</div>
</div>
THX a lot
You can read this information:
"At a certain point when we reach 1000px I want the boxes to be among each other like this."
1000px belongs medium, and now display 2 columns (each has 50% width).
Way 1: Rewrite class name
<div class="col-6 col-sm-6 col-md-12 col-lg-6">First column</div>
<div class="col-6 col-sm-6 col-md-12 col-lg-6">Second column</div>
Way 2: Overwrite default bootstrap.min.css
#media (min-width: 992px)
.col-md-6 {
width: 100%;
}

How to responsive for 3 item using col-sm-6 in Bootstrap?

I create a price table include 3 columns.
My code like:
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6">
</div>
<div class="col-lg-4 col-md-4 col-sm-6">
</div>
<div class="col-lg-4 col-md-4 col-sm-6">
</div>
</div>
</div>
Using col-xx-4 is work perfect. Only at col-sm-4, my page is broken. Like:
I decide using col-sm-6, it shows like:
Look good. But at the final item, it should be at center screen and margin-top: 15px to look pretty.
I don't know how to do it. Have any method to resolve my problem?
Try use offset
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6">
</div>
<div class="col-lg-4 col-md-4 col-sm-6">
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-sm-offset-3">
</div>
</div>
</div>
The problem with Bootstrap's columns is that you can't make the columns equal in height. I recommend you use flex for this.

Move items from one row to another when screen size changes Bootstrap

On my page I have a calendar, I want it to be responsive, so that On Large screens it will look like this:
[Jan] [Feb] [Mar]
[Apr] [May] [June]
[July] [Aug] [Sept]
[Oct] [Nov] [Dec]
When resizing to middle this:
[Jan] [Feb]
[Mar] [Apr]
[May] [June]
[July] [Aug]
[Sept] [Oct]
[Nov] [Dec]
The problem is that I group them by 3 elements to one row (large screens view).
(I assigned these classes to my "months": class="col-lg-4 col-md-6")
So when I change screen size to middle it is still grouped in rows and looks so:
[Jan] [Feb]
[Mar]
[Apr] [May]
[June]
[July] [Aug]
[Sept]
[Oct] [Nov]
[Dec]
Is there any way to solve this?
you can just use one .row and use .clearfix visible-lg-block for large screens at each 3nth item. See Bootstrap Docs about Responsive Columns Resets
[class^="col-"] {
border: solid red;
height: 50px;
text-align: center
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-6 col-lg-3">Jan</div>
<div class="col-md-6 col-lg-3">Feb</div>
<div class="col-md-6 col-lg-3">Mar</div>
<div class="clearfix visible-lg-block"></div>
<div class="col-md-6 col-lg-3">Apr</div>
<div class="col-md-6 col-lg-3">May</div>
<div class="col-md-6 col-lg-3">Jun</div>
<div class="clearfix visible-lg-block"></div>
<div class="col-md-6 col-lg-3">Jul</div>
<div class="col-md-6 col-lg-3">Aug</div>
<div class="col-md-6 col-lg-3">Sep</div>
<div class="clearfix visible-lg-block"></div>
<div class="col-md-6 col-lg-3">Oct</div>
<div class="col-md-6 col-lg-3">Nov</div>
<div class="col-md-6 col-lg-3">Dez</div>
</div>
</div>

Ruby on rails - Bootstrap - 3 columns made responsive

I have home page with following code:
<% #events.each_slice(3).to_a.each do |chunk| %>
<div class="row row-centered">
<% chunk.each do |event| %>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 col-centered">
centered content with max-width 300px
</div>
<% end %>
</div>
<% end %>
On big screen it works well. But when I change the size of browser, columns goes from 3 per row to 2 per row and the one left column goes to next row. So I have:
2 columns
1 column
2 columns
1 column
Is there any universal solution for responsive columns generated using #objects.each_slice(3)?
That's because you have the following classes:
col-xs-12 col-sm-6 col-md-6 col-lg-4
That means: 3 per row on a big screen, 2 per row on the middle screens, 1 per row on the mobile.
If you change to:
col-xs-12 col-sm-4 col-md-4 col-lg-4
This will make 3 per row on all the screens except from mobile where will be 1 per row.
So, my solution is pretty simple. On homepage I'll be showing exactly 12 posts. Given that, i changed my code to this:
<div class="row row-centered">
<% #events.each_slice(3).to_a.each do |chunk| %>
<% chunk.each do |event| %>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 col-centered">
<div class="thumbnail">
</div>
</div>
<% end %>
<% end %>
</div>
Given that I'm not generating row with 3 columns, but one row with 12 columns - and I let Bootstrap to deal with.
HTML example showing how Bootstrap responsive grids react to re-sizing
I wrote this HTML snippet to help whenever I want to figure out which combination of Bootstrap classes to use. It might help you too.
<div class="row">
<div class="page-header">
<h1>Bootstrap grid examples</h1>
<p class="lead">Basic grid layouts to get you familiar with building within the Bootstrap grid system.</p>
</div>
<h3>Three equal columns</h3>
<p>Get three equal-width columns <strong>starting at desktops and scaling to large desktops</strong>. On mobile devices, tablets and below, the columns will automatically stack.</p>
<div class="row">
<div class="col-md-4" style="background-color:red">.col-md-4</div>
<div class="col-md-4" style="background-color:pink">.col-md-4</div>
<div class="col-md-4" style="background-color:yellow">.col-md-4</div>
</div>
<h3>Three unequal columns</h3>
<p>Get three columns <strong>starting at desktops and scaling to large desktops</strong> of various widths. Remember, grid columns should add up to twelve for a single horizontal block. More than that, and columns start stacking no matter the viewport.</p>
<div class="row">
<div class="col-md-3" style="background-color:red">.col-md-3</div>
<div class="col-md-6" style="background-color:pink">.col-md-6</div>
<div class="col-md-3" style="background-color:yellow">.col-md-3</div>
</div>
<h3>Two columns</h3>
<p>Get two columns <strong>starting at desktops and scaling to large desktops</strong>.</p>
<div class="row">
<div class="col-md-8" style="background-color:pink">.col-md-8</div>
<div class="col-md-4" style="background-color:yellow">.col-md-4</div>
</div>
<h3>Two columns with two nested columns</h3>
<p>Per the documentation, nesting is easy, just put a row of columns within an existing column.</p> <p>This gives you two columns <strong>starting at desktops and scaling to large desktops</strong>, with another two (equal widths) within the larger column.</p>
<p>At mobile device sizes, tablets and down, these columns and their nested columns will stack.</p>
<div class="row">
<div class="col-md-8" style="background-color:yellow">
.col-md-8
<div class="row">
<div class="col-md-6" style="background-color:lightgreen">.col-md-6</div>
<div class="col-md-6" style="background-color:pink">.col-md-6</div>
</div>
</div>
<div class="col-md-4" style="background-color:lightgray">.col-md-4</div>
</div>
<h3>Full width, single column</h3>
<p class="text-warning">No grid classes are necessary for full-width elements.</p>
<h3>Mixed: mobile and desktop</h3>
<p>The Bootstrap 3 grid system has four tiers of classes: xs (phones), sm (tablets), md (desktops), and lg (larger desktops). You can use nearly any combination of these classes to create more dynamic and flexible layouts.</p>
<p>Each tier of classes scales up, meaning if you plan on setting the same widths for xs and sm, you only need to specify xs.</p>
<div class="row">
<div class="col-xs-12 col-md-8" style="background-color:red">.col-xs-12 .col-md-8</div>
<div class="col-xs-6 col-md-4" style="background-color:lightgray">.col-xs-6 .col-md-4</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-4" style="background-color:lightgray">.col-xs-6 .col-md-4</div>
<div class="col-xs-6 col-md-4" style="background-color:yellow">.col-xs-6 .col-md-4</div>
<div class="col-xs-6 col-md-4" style="background-color:lightgreen">.col-xs-6 .col-md-4</div>
</div>
<div class="row">
<div class="col-xs-6" style="background-color:lightgray">.col-xs-6 </div>
<div class="col-xs-6" style="background-color:lightgreen">.col-xs-6</div>
</div>
<hr>
<h3>Mixed: mobile, tablet, and desktop</h3>
<div class="row">
<div class="col-xs-12 col-sm-6 col-lg-8" style="background-color:lightgray">.col-xs-12 .col-sm-6 .col-lg-8</div>
<div class="col-xs-6 col-lg-4" style="background-color:yellow">.col-xs-6 .col-lg-4</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-4" style="background-color:lightgreen">.col-xs-6 .col-sm-4</div>
<div class="col-xs-6 col-sm-4" style="background-color:pink">.col-xs-6 .col-sm-4</div>
<div class="col-xs-6 col-sm-4" style="background-color:lightgray">.col-xs-6 .col-sm-4</div>
</div>
<hr>
<h3>Column clearing</h3>
<div class="row">
<div class="col-xs-6 col-sm-3" style="background-color:lightgray">
.col-xs-6 .col-sm-3
<br>
Resize your viewport or check it out on your phone for an example.
</div>
<div class="col-xs-6 col-sm-3" style="background-color:yellow">.col-xs-6 .col-sm-3</div>
<!-- Add the extra clearfix for only the required viewport -->
<div class="clearfix visible-xs"></div>
<div class="col-xs-6 col-sm-3" style="background-color:lightgreen">.col-xs-6 .col-sm-3</div>
<div class="col-xs-6 col-sm-3" style="background-color:pink">.col-xs-6 .col-sm-3</div>
</div>
<hr>
<h3>Offset, push, and pull resets</h3>
<p>Reset offsets, pushes, and pulls at specific breakpoints.</p>
<div class="row">
<div class="col-sm-5 col-md-6" style="background-color:lightgray">.col-sm-5 .col-md-6</div>
<div class="col-sm-5 col-sm-offset-2 col-md-6 col-md-offset-0" style="background-color:yellow">.col-sm-5 .col-sm-offset-2 .col-md-6 .col-md-offset-0</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-5 col-lg-6" style="background-color:lightgreen">.col-sm-6 .col-md-5 .col-lg-6</div>
<div class="col-sm-6 col-md-5 col-md-offset-2 col-lg-6 col-lg-offset-0" style="background-color:pink"<>.col-sm-6 .col-md-5 .col-md-offset-2 .col-lg-6 .col-lg-offset-0" </div>
</div>
</div>

Resources