Bootstrap Image Grid [duplicate] - html

This question already has answers here:
Bootstrap 4 masonry layout utilizing flexbox grid
(3 answers)
Closed 4 years ago.
I want to create a responsive image grid as in the picture below:
How do I use bootstrap columns and rows to achieve this?
<div class="row">
<div class="col-md-3 sm-12"
<img class="img-thumbnail" scr=".......">
</div>
<div class="col-md-3 sm-12"
<img class="img-thumbnail" scr=".......">
</div>
<div class="col-md-3 sm-12"
<img class="img-thumbnail" scr=".......">
</div>
........
...........
.................
.....................
</div>
I place all the columns inside a single row so that the column elements wrap themselves but this does not happen.
When I implement the code above, due to "height: auto" some images are long and this creates void white space between images of different rows.
I get something like this as a result:

You can achieve this with card columns. JSFiddle here:
<div class="container-fluid pt-3">
<div class="card-columns">
<div class="card">
<img class="card-img-top" style="width:100%; height: 200px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 500px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 300px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 400px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 400px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 300px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 500px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 200px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 200px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 500px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 300px;"/>
</div>
<div class="card">
<img class="card-img-top" style="width:100%; height: 400px;"/>
</div>
</div>
</div>

depending on you requirement, you need to replace col-3 with col-sm-3 or col-md-3, etc:
img
{
width: 100%;
margin: 10px 0px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="row">
<div class="col-3">
<img src="https://www.w3schools.com/w3images/nature.jpg" alt="Lights" >
<img src="https://www.w3schools.com/w3images/falls2.jpg" alt="Lights" >
<img src="https://www.w3schools.com//w3images/wedding.jpg"alt="Lights" >
</div>
<div class="col-3">
<img src="https://www.w3schools.com/w3images/rocks.jpg" alt="Lights" >
<img src="https://www.w3schools.com/w3images/fjords.jpg" alt="Lights" >
</div>
<div class="col-3">
<img src="https://www.w3schools.com/w3images/fjords.jpg" alt="Lights" >
<img src="https://www.w3schools.com/w3images/lights.jpg" alt="Lights" >
<img src="https://www.w3schools.com//w3images/wedding.jpg"alt="Lights" >
</div>
<div class="col-3">
<img src="https://www.w3schools.com/w3images/nature.jpg" alt="Lights" >
<img src="https://www.w3schools.com//w3images/rocks.jpg" alt="Lights" >
</div>
</div>

I don't think that's possible with just bootstrap.
Look for the masonry ( https://masonry.desandro.com/ ) or isotope ( https://isotope.metafizzy.co/ ) plugin for bootstrap.

Related

How to fix equal column the height problem in bootstrap [duplicate]

This question already has answers here:
CSS-only masonry layout
(4 answers)
Closed 2 years ago.
I'm new to bootstrap. I'm trying to create a gallery of some amazing arts. But, I'm facing some issues. All of the columns are taking up equal height even though the image isn't that large. How can I remove the extra space?
<div class="container-fluid">
<div class="row content" id="images">
<div class="col-md-4">
<img src="..\images\allyson-beaucourt-HaD6zYONkg0-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-4">
<img src="..\images\annie-spratt-52hSxHwXE7I-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-4">
<img src="..\images\amr-taha-ig_Qc8qoTSI-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-4">
<img src="..\images\annie-spratt-Pzyyzaiy-LE-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-4">
<img src="..\images\arno-senoner-FOM8031NAG8-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-4">
<img src="..\images\bp-miller-f6BffCg5Ymc-unsplash.jpg" alt="" class="img-fluid">
</div>
</div>
</div>
this is how I want it to look.
This can be helpful
<div class="container-fluid">
<div class="row">
<div class="col-md-6 content" id="images">
<div class="col-md-12 ">
<img src="..\images\allyson-beaucourt-HaD6zYONkg0-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-12 ">
<img src="..\images\annie-spratt-52hSxHwXE7I-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-12 ">
<img src="..\images\amr-taha-ig_Qc8qoTSI-unsplash.jpg" alt="" class="img-fluid">
</div>
</div>
<div class="col-md-6 content" >
<div class="col-md-12 " style="max-height: 50%">
<img src="..\images\annie-spratt-Pzyyzaiy-LE-unsplash.jpg" alt="" class="img-fluid">
</div>
<div class="col-md-12 " style="max-height: 50%">
<img src="..\images\arno-senoner-FOM8031NAG8-unsplash.jpg" alt="" class="img-fluid">
</div>
</div>
</div>
see more on bootstrap grid

Divide Bootstrap card class to 3 horizontal boxes of the same size and 2 vertical boxes

I am using Bootstrap 4 and I have the following Bootstrap code block:
<div class="card-body text-success">
</div>
I need to create an image gallery: 3 horizontal boxes of the same size and 2 vertical boxes, so I tried the following code:
<div class="card-body text-success">
<div class="row">
<div class="col-4">
Your text
</div>
<div class="col-4">
Your text
</div>
<div class="col-4">
Your text
</div>
</div>
<div class="row">
<div class="col-4">
Your text
</div>
<div class="col-4">
Your text
</div>
<div class="col-4">
Your text
</div>
</div>
</div>
But this generates the gallery with some space between images. I need images without any space between. What could I do to achieve this?
CSS
.borders{
border: 0;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
HTML
<div class="card-group">
<img class="card-img-top card borders" src="https://source.unsplash.com/random/200x200">
<img class="card-img-top card borders" src="https://source.unsplash.com/random/200x200">
<img class="card-img-top card borders" src="https://source.unsplash.com/random/200x200">
</div>
<div class="card-group">
<img class="card-img-top card borders" src="https://source.unsplash.com/random/200x200">
<img class="card-img-top card borders" src="https://source.unsplash.com/random/200x200">
<img class="card-img-top card borders" src="https://source.unsplash.com/random/200x200">
</div>
Preview Screenshort
You could also consider doing your gallery with plain CSS. Witch makes the HTML so much nicer:
.gallery {
width: 600px;
display: grid;
grid-template-columns: auto auto auto;
grid-template-rows: auto auto;
grid-gap: 0;
}
.gallery > * {
width: 200px;
}
<div class="gallery">
<img src="https://source.unsplash.com/random/200x200?1" alt="">
<img src="https://source.unsplash.com/random/200x200?2" alt="">
<img src="https://source.unsplash.com/random/200x200?3" alt="">
<img src="https://source.unsplash.com/random/200x200?4" alt="">
<img src="https://source.unsplash.com/random/200x200?5" alt="">
<img src="https://source.unsplash.com/random/200x200?6" alt="">
</div>
Try this way:
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card-group flex-wrap">
<div class="card border-0">
<img class="card-img-top" src="https://cdn.pixabay.com/photo/2020/10/03/14/54/rock-formations-5623735__340.jpg" alt="Card image cap">
</div>
<div class="card border-0">
<img class="card-img-top" src="https://cdn.pixabay.com/photo/2020/10/03/14/54/rock-formations-5623735__340.jpg" alt="Card image cap">
</div>
<div class="card border-0">
<img class="card-img-top" src="https://cdn.pixabay.com/photo/2020/10/03/14/54/rock-formations-5623735__340.jpg" alt="Card image cap">
</div>
</div>
<div class="card-group flex-wrap">
<div class="card border-0">
<img class="card-img-top" src="https://cdn.pixabay.com/photo/2020/10/03/14/54/rock-formations-5623735__340.jpg" alt="Card image cap">
</div>
<div class="card border-0">
<img class="card-img-top" src="https://cdn.pixabay.com/photo/2020/10/03/14/54/rock-formations-5623735__340.jpg" alt="Card image cap">
</div>
<div class="card border-0">
<!-- this is empty space -->
</div>
</div>

How to manage bootstrap Mansory col height?

I don't understand how to change the height of my column in a way to have three elements in my first and second column. I'm new to design element so a little help would be very helpful for me
Mock-up that I want to create:
My code looks like this:
<div class="card-columns pl-5 pr-5">
<!-- col 1-->
<div class="mb-3">
<h1>Lorem Ipsum</h1>
</div>
<div class="card">
<img class="card-img-top" src="assets/Familly-card-hide.png" alt="Card image cap">
</div>
<div class="card">
<img class="card-img-top" src="assets/Twiter-card-hide.png" alt="Card image cap">
</div>
<!-- col 2-->
<div class="card">
<img class="card-img-top" src="assets/Twiter-card-hide.png" alt="Card image cap">
</div>
<div class="card">
<img class="card-img-top" src="assets/Dog-card-hide.png" alt="Card image cap">
</div>
<div>
<span>Lorem ipsum ipsum<img src="assets/Circle-arow-btm.png" alt=""
style="padding-left: 10px; transform: rotate(270deg);"> </span>
</div>
<!-- col 3-->
<div class="card" style="width: 300px;height: auto;">
<img class="card-img-top" src="assets/Fried-card-hide.png" alt="Card image cap">
</div>
<div class="card" style="width: 300px;height: auto;">
<img class="card-img-top" src="assets/work-card-hide.png" alt="Card image cap">
</div>
</div>
I hope you can help me to understand how Masonry work in a way to help me with this code.
Bootstrap Masonry using .card-columns is not so flexible solution for this case. Better to use simple Bootstrap grid system. I made three columns, which are .col-sm-6, .col-sm-4 and .col-sm-2. As you probably now, BS grid contains of 12 columns, so we have 6+4+2=12 columns here. In my example the sizes of the blocks will depend on the proportions of image inside. .img-fluid class makes images max-width: 100%; so thew will not tear the parent block.
This example made by using only Bootstrap 4 classed, for instance. If you want to add some specific setting - you will need to write additional CSS classes and their properties.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
<div class="container mt-5">
<div class="row">
<!-- col 1-->
<div class="col-sm-6 px-1">
<div class="mb-2">
<h1>Lorem Ipsum</h1>
</div>
<div class="mb-2">
<img class="img-fluid" src="https://via.placeholder.com/1000x600">
</div>
<div class="mb-2 text-right">
<img class="img-fluid w-75" src="https://via.placeholder.com/1000x600">
</div>
</div>
<!-- col 2-->
<div class="col-sm-4 mt-1 px-1">
<div class="mb-2">
<img class="img-fluid" src="https://via.placeholder.com/1000x600">
</div>
<div class="mb-2">
<img class="img-fluid" src="https://via.placeholder.com/1000x1000">
</div>
<div class="text-right">
<span>Lorem ipsum ipsum<img src="https://via.placeholder.com/36x36" alt=""style="margin-left: 10px; transform: rotate(270deg);"> </span>
</div>
</div>
<!-- col 3-->
<div class="col-sm-2 px-1">
<div class="mb-2 mt-4">
<img class="img-fluid" src="https://via.placeholder.com/1000x1300">
</div>
<div class="mb-2">
<img class="img-fluid" src="https://via.placeholder.com/1000x1100">
</div>
</div>
</div>
</div>
More about Bootstrap technologies I used in this exapmple:
Grid https://getbootstrap.com/docs/4.0/layout/grid/
Spacing https://getbootstrap.com/docs/4.0/utilities/spacing/
image-fluid https://getbootstrap.com/docs/4.0/content/images/
Sizing https://getbootstrap.com/docs/4.0/utilities/sizing/

Images not displayed properly when using img-fluid class

When adding the bootstrap class "img-fluid" to my images they don't appear, when inspecting the element it sets the image at 0x0 pixels.
The html:
<div class="col">
<div class="row no-gutters">
<div class="col">
<img src="assets/images/placeholder.png" class="img-fluid"
alt="placeholder">
</div>
<div class="col">
<img src="assets/images/placeholder.png" class="img-fluid" alt="placeholder">
</div>
<div class="w-100"></div>
<div class="col">
<img src="assets/images/placeholder.png" class="img-fluid" alt="placeholder">
</div>
<div class="col">
<img src="assets/images/placeholder.png" class="img-fluid" alt="placeholder">
</div>
</div>
</div>
The images are supposed to be the same size as the parent(div.col) and be responsive to the parent.
Here's an answer based on my comment.
Because you have a .col element as a child of a flex parent, the flex-basis is set to 0. That basically equates to width: 0.
To get around that, set the flex-basis to auto on the .col that are children of the .row element.
.row>.col {
flex-basis: auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="col">
<div class="row no-gutters">
<div class="col">
<img src="https://placehold.it/100x100" class="img-fluid" alt="placeholder">
</div>
<div class="col">
<img src="https://placehold.it/100x100" class="img-fluid" alt="placeholder">
</div>
<div class="w-100"></div>
<div class="col">
<img src="https://placehold.it/100x100" class="img-fluid" alt="placeholder">
</div>
<div class="col">
<img src="https://placehold.it/100x100" class="img-fluid" alt="placeholder">
</div>
</div>
</div>

Bootstrap: responsive image alignment

I have a code like below - columns with full-width images.
<div class="col-3">
<img src="..." class="w-100" />
<div>Name</div>
</div>
As images can be of different size, it's not guaranteed that content below images will be displayed in one line (image's height is supposed to differ, depending on image's width).
In other words, when an image within .col is 200 px width, should be 200 px height, etc. (width = height).
Example 1 with css:
#import url('https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css');
.img-prev {
height: 200px;
overflow: hidden;
}
.img-prev img {
max-height: 100%;
width: auto;
max-width: inherit;
margin: auto;
display: block;
}
<div class="container">
<div class="row">
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538356913997-b04286765811?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=39e83600a508d87be2ea636e02f30f95&auto=format&fit=crop&w=600&q=60" alt="" class="img-fluid">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538333785596-1cc0b5e03551?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=c942fc493dad02801a99aa191414a535&auto=format&fit=crop&w=600&q=60" alt="" class="img-fluid">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538379585867-44bf200a3d25?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=29782178fb2019f60bee12c2fcfacff4&auto=format&fit=crop&w=600&q=60" alt="" class="img-fluid">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538367735494-5e6cc0ff555a?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3066861f0cf81849de20853676dee717&auto=format&fit=crop&w=600&q=60" alt="" class="img-fluid">
</div>
</div>
</div>
</div>
Example 2 html:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538356913997-b04286765811?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=39e83600a508d87be2ea636e02f30f95&auto=format&fit=crop&w=600&q=60" alt="" height="200" width="150">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538333785596-1cc0b5e03551?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=c942fc493dad02801a99aa191414a535&auto=format&fit=crop&w=600&q=60" alt="" height="200" width="150">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538379585867-44bf200a3d25?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=29782178fb2019f60bee12c2fcfacff4&auto=format&fit=crop&w=600&q=60" alt="" height="200" width="150">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538367735494-5e6cc0ff555a?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3066861f0cf81849de20853676dee717&auto=format&fit=crop&w=600&q=60" alt="" height="200" width="150">
</div>
</div>
</div>
</div>
Example 3 with jq:
var imgs = $('.img-prev>img');
imgs.each(function(){
var item = $(this).closest('.img-prev');
item.css({
'background-image': 'url(' + $(this).attr('src') + ')',
'background-position': 'center center',
'-webkit-background-size': 'cover',
'background-size': 'cover',
});
$(this).hide();
});
.img-prev {
height: 200px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<div class="container">
<div class="row img-list">
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538356913997-b04286765811?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=39e83600a508d87be2ea636e02f30f95&auto=format&fit=crop&w=600&q=60" alt="">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538333785596-1cc0b5e03551?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=c942fc493dad02801a99aa191414a535&auto=format&fit=crop&w=600&q=60" alt="">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538379585867-44bf200a3d25?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=29782178fb2019f60bee12c2fcfacff4&auto=format&fit=crop&w=600&q=60" alt="">
</div>
</div>
<div class="col-4 mb-2">
<div class="img-prev">
<img src="https://images.unsplash.com/photo-1538367735494-5e6cc0ff555a?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3066861f0cf81849de20853676dee717&auto=format&fit=crop&w=600&q=60" alt="">
</div>
</div>
</div>
</div>
try this one,
<div class="container">
<img src="loginBackground.jpg" class="img-fluid" alt="Cinque Terre" width="200" height="200">
</div>