Alternating bootstrap grid columns and mobile view - css

I have this design:
Desktop or large screens.
Notes* Red or green boxes are not the same height.
Mobile or lower screens.
How to create this layout type on bootstrap 4 grid or custom CSS grid, what is better?
Thanks, have a nice day.

First of all, if you would like to build a masonry-like columns, a regular Bootstrap grid system won't give you what you want. Instead, you will have to use the card columns or JavaScript masonry plugins.
Layout with Card Columns
<div class="container">
<div id="example" class="card-columns">
<div id="card1" class="card border-success">
<div class="card-body">
<p class="card-text">Card #1 - height: 20rem;</p>
</div>
</div>
<div id="card2" class="card border-success">
<div class="card-body">
<p class="card-text">Card #2 - height: 12rem;</p>
</div>
</div>
<div id="card3" class="card border-danger">
<div class="card-body">
<p class="card-text">Card #3 - height: 18rem;</p>
</div>
</div>
<div id="card4" class="card border-danger">
<div class="card-body">
<p class="card-text">Card #4 - height: 14rem;</p>
</div>
</div>
</div>
</div>
With little bit of styling to setup 1 column for mobile view and 2 for others:
#example.card-columns {
column-count: 1;
}
#media(min-width: 576px) {
#example.card-columns {
column-count: 2;
}
}
You can get something closed to what you would like to achieve:
demo: https://jsfiddle.net/davidliang2008/n3fhyp8c/60/
Problems on Mobile View
Now there is a problem on the mobile view. Since the order of the CSS columns is top to bottom, left to right, and there is no way to change it, the only way to get what you want on the mobile view is to duplicate contents and hide/show them based on the breakpoints...
Again, your second screenshot is missing a red box so I don't know which one you want to put in the middle. Here I am assuming you want to put both red boxes there:
<div id="card1" />
<div id="card2" class="card border-success d-none d-sm-block">
...
</div>
<div id="card3" />
<div id="card4" />
<div id="card2" class="card border-success d-block d-sm-none">
...
</div>
Then you can get something you want to achieve on mobile view:
demo: https://jsfiddle.net/davidliang2008/n3fhyp8c/63/

Related

How to align card images in bootstrap 4.4 so that all the cards have equal width and height.?

So basically while making a simple website with Django and bootstrap 4.4 I came up with this issue. I was using 'cards' to add images of books in a grid format like in a bookstore web application. But the cards are not having equal dimensions.
How to align card images in bootstrap 4.4 so that all the cards have equal width and height while keeping it responsive to the window size change.??
My problem in the image below. As you can see there the cards are not of same size when i add different images it changes its size as well.
enter image description here
html
<div class="container">
<div class="row">
{% for book in object_list %}
<div class="col s3">
<div class="card">
<img class="img-fluid" src="{{book.book_image}}" alt="">
<div class="card-body">
</div>
</div>
<p class="text-white">{{book.name}}</p>
</div>
{% endfor %}
</div>
</div>
and .css
body{
background: white url('images/webbg.jpg') no-repeat center center;
background-size: cover;
}
Example image of what I actually want to have enter image description here
I want exactly like what there is in the above picture with images loading from my database in a loop.(so not hardcoded image links embedded in html code)
It sounds like you could use .card-deck to achieve your goals
https://getbootstrap.com/docs/4.4/components/card/#card-decks
If .card-deck won't work for you you can use the new Grid Cards. They allow you to select how many cards you want per row based on screen size. The don't automatically set themselves to being equal height however that is easily remedied by adding .h-100 to the cards.
As an example, if you wanted 4 cards per row after the xl breakpoint, 3 after md and 2 on mobile you would write the following. Note there is some margin on the columns to give them some space as they wrap.
<div class="row row-cols-2 row-cols-md-3 row-cols-xl-4">
<div class="col mb-4">
<div class="card h-100">
<!-- content -->
</div>
</div>
<div class="col mb-4">
<div class="card h-100">
<!-- content -->
</div>
</div>
<div class="col mb-4">
<div class="card h-100">
<!-- content -->
</div>
</div>
<div class="col mb-4">
<div class="card h-100">
<!-- content -->
</div>
</div>
</div>

Image Not Aligning to Center in Bootstrap Row

I used Bootstrap grid to centre the position of the 4 images in Angular. The images align perfectly to the centre in the row when I placed them within the same HTML sheet. However, if I placed them in a different HTML sheet, the 4 images are not exactly positioned in the centre, as I notice they are slightly off to the left.
To summarise, this works (./home.component.html):
<div class="container">
<p class="h2-responsive itemsTitle">Items with free shipping</p>
<hr>
<div class="row">
<div class="col-sm">
<img class="itemCard" src="https://images.unsplash.com/photo-1505429155379-441cc7a574f7?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=0ddea5332a8ab2d09cf5eec6a57d3f9f&auto=format&fit=crop&w=334&q=80">
</div>
<div class="col-sm">
<img class="itemCard" src="https://images.unsplash.com/photo-1505429155379-441cc7a574f7?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=0ddea5332a8ab2d09cf5eec6a57d3f9f&auto=format&fit=crop&w=334&q=80">
</div>
<div class="col-sm">
<img class="itemCard" src="https://images.unsplash.com/photo-1505429155379-441cc7a574f7?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=0ddea5332a8ab2d09cf5eec6a57d3f9f&auto=format&fit=crop&w=334&q=80">
</div>
<div class="col-sm">
<img class="itemCard" src="https://images.unsplash.com/photo-1505429155379-441cc7a574f7?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=0ddea5332a8ab2d09cf5eec6a57d3f9f&auto=format&fit=crop&w=334&q=80">
</div>
</div>
</div>
But this doesn't (./home.component.html):
<div class="container">
<p class="h2-responsive itemsTitle">Items with free shipping</p>
<hr>
<div class="row">
<app-home-item></app-home-item>
<app-home-item></app-home-item>
<app-home-item></app-home-item>
<app-home-item></app-home-item>
</div>
</div>
(./home-item.component.html):
<div class="col-sm">
<img class="itemCard" src="https://images.unsplash.com/photo-1505429155379-441cc7a574f7?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=0ddea5332a8ab2d09cf5eec6a57d3f9f&auto=format&fit=crop&w=334&q=80">
</div>
Here's what happens. The circled part shows that the images are slightly off to the left, which doesn't happen when I use the first example code above.
I am not too sure where it goes wrong. All the components are imported and declared in app.module. The bootstrap link is placed in the index.html as well.
CSS (in styles.css):
.itemsTitle {
margin-top: 16px;
}
.itemCard {
width: 15rem;
}
The problem is with the width of col inside app-home-item so in that component add flex: 1; to the wrapper

Order columns through Bootstrap4

I have 3 columns which I want to order in different ways on desktop and mobile.
Currently, my grid looks like this:
<div class="row">
<div class="col-xs-3 col-md-6">
1
</div>
<div class="col-xs-3 col-md-6">
2
</div>
<div class="col-xs-6 col-md-12">
3
</div>
</div>
In the mobile view I want to have the following output:
1-3-2
Unfortunately I don't get how to solve this with the .col-md-push-* and .col-md-pull-* classes in Bootstrap 4.
2021 - Bootstrap 5
The responsive ordering classes are now order-first, order-last and order-0 - order-5
Demo
2018 - Bootstrap 4
The responsive ordering classes are now order-first, order-last and order-0 - order-12
The Bootstrap 4 **push** **pull** classes are now `push-{viewport}-{units}` and `pull-{viewport}-{units}` and the `xs-` infix has been removed. To get the desired 1-3-2 layout on mobile/xs would be: [Bootstrap 4 push pull demo](http://www.codeply.com/go/OmrcmepbUp) (This only works pre 4.0 beta)
Bootstrap 4.1+
Since Bootstrap 4 is flexbox, it's easy to change the order of columns. The cols can be ordered from order-1 to order-12, responsively such as order-md-12 order-2 (last on md, 2nd on xs) relative to the parent .row.
<div class="container">
<div class="row">
<div class="col-3 col-md-6">
<div class="card card-body">1</div>
</div>
<div class="col-6 col-md-12 order-2 order-md-12">
<div class="card card-body">3</div>
</div>
<div class="col-3 col-md-6 order-3">
<div class="card card-body">2</div>
</div>
</div>
</div>
Demo: Change order using order-* classes
Desktop (larger screens):
Mobile (smaller screens):
It's also possible to change column order using the flexbox direction utils...
<div class="container">
<div class="row flex-column-reverse flex-md-row">
<div class="col-md-8">
2
</div>
<div class="col-md-4">
1st on mobile
</div>
</div>
</div>
Demo: Bootstrap 4.1 Change Order with Flexbox Direction
Older version demos
demo - alpha 6
demo - beta (3)
See more Bootstrap 4.1+ ordering demos
Related
Column ordering in Bootstrap 4 with push/pull and col-md-12
Bootstrap 4 change order of columns
A-C-B A-B-C
This can also be achieved with the CSS "Order" property and a media query.
Something like this:
#media only screen and (max-width: 768px) {
#first {
order: 2;
}
#second {
order: 4;
}
#third {
order: 1;
}
#fourth {
order: 3;
}
}
CodePen Link: https://codepen.io/preston206/pen/EwrXqm
even this will work:
<div class="container">
<div class="row">
<div class="col-4 col-sm-4 col-md-6 order-1">
1
</div>
<div class="col-4 col-sm-4 col-md-6 order-3">
2
</div>
<div class="col-4 col-sm-4 col-md-12 order-2">
3
</div>
</div>
</div>
I'm using Bootstrap 3, so i don't know if there is an easier way to do it Bootstrap 4 but this css should work for you:
.pull-right-xs {
float: right;
}
#media (min-width: 768px) {
.pull-right-xs {
float: left;
}
}
...and add class to second column:
<div class="row">
<div class="col-xs-3 col-md-6">
1
</div>
<div class="col-xs-3 col-md-6 pull-right-xs">
2
</div>
<div class="col-xs-6 col-md-12">
3
</div>
</div>
EDIT:
Ohh... it looks like what i was writen above is exacly a .pull-xs-right class in Bootstrap 4 :X Just add it to second column and it should work perfectly.
Since column-ordering doesn't work in Bootstrap 4 beta as described in the code provided in the revisited answer above, you would need to use the following (as indicated in the codeply 4 Flexbox order demo - alpha/beta links that were provided in the answer).
<div class="container">
<div class="row">
<div class="col-3 col-md-6">
<div class="card card-block">1</div>
</div>
<div class="col-6 col-md-12 flex-md-last">
<div class="card card-block">3</div>
</div>
<div class="col-3 col-md-6 ">
<div class="card card-block">2</div>
</div>
</div>
Note however that the "Flexbox order demo - beta" goes to an alpha codebase, and changing the codebase to Beta (and running it) results in the divs incorrectly displaying in a single column -- but that looks like a codeply issue since cutting and pasting the code out of codeply works as described.
You can do two different container one with mobile order and hide on desktop screen, another with desktop order and hide on mobile screen

column images full width responsive

I'm trying make 2 images take up full width of 2 columns. I want the images to be responsive. This seems like it should be simple but im having problems. I'm getting confused on applying width to container, row, or using table. I would like it to look like the first 2 entry boxes under the carousel on this site minus the animation rollover. http://www.montere.it/?lang=en
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="trans"> <img src="headertrans.jpg">
h3>The Latest</h3>
<p>From apartments and rooms to treehouses and boats: stay in unique spaces in 192 countries.</p>
<p>See how to travel on Airbnb</p>
</div>
</div>
<div class="col-md-6">
<div class="trans"> <img src="headertrans2.jpg">
<h3>Designs</h3>
<p>Renting out your unused space could pay your bills or fund your next vacation.</p>
<p>Learn more about hosting</p>
</div>
</div>
my css:
.container{width: 100%;
}
.trans{
transition:.all 0.9s ease;
}
.trans:hover {
opacity: 0.1;
}
Demo JSFiddle
Since it seems you're using Bootstrap, you'll want to do something like this.
HTML
<div class="container-fluid">
<div class="row">
<div class="col-xs-6 text-center box left">
<h3>The Latest</h3>
<p>From apartments and rooms to treehouses and boats: stay in unique spaces in 192 countries.</p>
<p>See how to travel on Airbnb
</p>
</div>
<div class="col-xs-6 text-center box right">
<h3>Designs</h3>
<p>Renting out your unused space could pay your bills or fund your next vacation.</p>
<p>Learn more about hosting
</p>
</div>
</div>
</div>
CSS
.box{min-height:200px;}
.left{background:url("http://cdn.cutestpaw.com/wp-content/uploads/2012/07/l-Wittle-puppy-yawning.jpg"); background-size:cover}
.right{background:url("http://cdn.cutestpaw.com/wp-content/uploads/2012/07/l-Wittle-puppy-yawning.jpg"); background-size:cover}

Blueprint CSS centering in grid

Just started using Blueprint CSS and now playing with the grids but have a simple problem. I created a navbar at the top of my page with each link 2 columns wide (using span-2). What is the right way to center these links inside the grid columns without hacking away at the css.
<div id="navbar" class="container showgrid">
<div class="span-2 border">
News
</div>
<div class="span-2 border">
Gigs
</div>
<div class="span-2 border">
Tunes
</div>
<div class="span-2 border">
Email List
</div>
</div>
I would create a helper class like this:
css:
.text-center{ text-align:center; }
html:
<div class="span-2 border text-center">
News
</div>
<div class="span-2 border text-center">
Gigs
</div>

Resources