Bootstrap 4 - How to keep a sidebar same height as responsive square? - html

I have the following setup:
I have an image box as a responsive square using a pseudo element :after and padding-bottom: 100%;
The issue:
I want to have a sidebar next to my box, which can contain any number of small thumbnail images. However, in default Bootstrap behavior, the smaller of the boxes gets adjusted to the bigger one. Instead, I am trying to get the sidebar to always be the same height as the main images. If it is bigger, the content should scroll vertically instead.
I have tried to achieve this using a wrapper around my sidebar, however the box still grows to its size instead.
My question:
How can I ensure the sidebar only gets the maximum height of my square box next to it?
https://jsfiddle.net/Sirence/rzx6t8ey/
.row {
margin-top: 20px;
}
.row>div {
border: solid 1px #6c757d;
padding: 10px;
}
img {
display: block;
border: 1px solid red;
}
.sidebar>div:not(:last-child) img {
margin-bottom: 10px;
}
.main img {
width: 100%;
height: 100%;
object-fit: cover;
}
.main .wrapper {
border: 1px solid green;
position: relative;
}
.main .wrapper:after {
content: "\00a0";
display: block;
padding-bottom: 100%;
line-height: 0;
}
.main .holder {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
}
.sidebar-wrapper .sidebar {
height: 100%;
overflow-y: scroll;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-1 sidebar">
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
</div>
<div class="col-2 main">
<div class="wrapper">
<div class="holder">
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-1 sidebar">
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
</div>
<div class="col-2 main">
<div class="wrapper">
<div class="holder">
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-1 sidebar-wrapper">
<div class="sidebar">
<div class="thumbnail">
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
<div>
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
</div>
</div>
<div class="col-2 main">
<div class="wrapper">
<div class="holder">
<img class="thumbnail" src="http://via.placeholder.com/20x20" class="img-fluid" />
</div>
</div>
</div>
</div>
</div>

This is definitely possible with only pure CSS. Alright, the premise of this is simple. You want for your row to always take the height of the responsive image and not the long list of thumbnails as you mentioned.
We need to tell the browser to only look at the height of the image and ignore the thumbnail list, and to do that we remove the thumbnail list from the document flow with position absolute.
I replicated your bootstrap layout, albeit more simply, and implemented what I just mentioned. I set the sidebar positioning to relative with vertical overflow to scroll. I styled it with a fixed flex width with no grow or shrink and then placed another absolute positioned container div to house the thumbnails.
I then added a second column where the main image will be. All that was left was to add the img-fluid bootstrap style to make the image responsive and voila!
Working fiddle here: Click Here
And this is the code:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<style>
.container-overrides {
background: #ccc;
margin: 20px auto;
max-width: 500px !important;
}
.sidebar {
position: relative;
overflow-y: scroll;
flex: 0 0 50px;
}
.sidebar .image-container {
position: absolute;
left: 0;
right: 0;
top: 10px;
bottom: 10px;
}
.sidebar img {
display: block;
margin: 0 auto 10px;
}
.sidebar img:last-of-type {
margin: 0 auto;
}
.main{
background:#ccc;
}
.main img {
height: auto;
border: 1px solid red;
}
</style>
<div class="container p-0 container-overrides">
<div class="row no-gutters">
<!-- Responsive Sidebar -->
<div class="sidebar bg-dark p-2">
<div class="image-container">
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
<img src="http://via.placeholder.com/20x20" />
</div>
</div>
<!-- Main Image Container -->
<div class="col p-2 main">
<img src="https://ih1.redbubble.net/image.393347411.1344/flat,800x800,070,f.jpg" class="img-fluid" />
</div>
</div>
</div>

Related

CSS dynamic image size in bootstrap

I got a horizontal scrolling page with bootstrap 5. All images have the same height, but different widths. My goal is that the images always use the full width of the page, so when the browser window is resized the images should shrink too.
What am I missing to achieve this? Your help is highly appreciated.
.container-fluid{
/* Vertical Scroll */
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
height: 100%;
}
.flex-nowrap {
-webkit-flex-wrap: nowrap!important;
-ms-flex-wrap: nowrap!important;
flex-wrap: nowrap!important;
}
.row {
flex-direction: column;
}
.col > img {
min-height: 100px;
max-height: 300px;
height: 300px;
padding-right:10px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid ">
<div class="row flex-row d-flex flex-nowrap">
<div class="col">
<img src="https://via.placeholder.com/450x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/500x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/550x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/333x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/450x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/500x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/550x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/333x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/450x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/500x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/550x600.png" alt="">
</div>
<div class="col">
<img src="https://via.placeholder.com/333x600.png" alt="">
</div>
</div>
</div>
If you want the children of .flex-row to have their widths determined by the image, you need to replace .col with .w-auto.flex-shrink-1.flex-grow-1 and add .img-fluid to your images. You have some errors in your stylesheet as well. Take a look at the snippet:
EDIT
If you are looking for vertical responsiveness (not what people typically mean) then use the unit vh on the height setting and don't use .flex-shrink-1. This will make the images a percentage of the viewport height while maintaining aspect ratio. Take a look at the updated snippet using height: 30vh; and min-height: 100px;:
.container-fluid {
/* Vertical Scroll */
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
height: 100%;
}
.flex-nowrap {
-webkit-flex-wrap: nowrap !important;
-ms-flex-wrap: nowrap !important;
flex-wrap: nowrap !important;
}
/* .row {
flex-direction: column;
} */
.w-auto>img {
min-height: 100px;
/* max-height: 300px; */
height: 30vh;
padding-right: auto;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid ">
<div class="row flex-row d-flex flex-nowrap">
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/450x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/500x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/550x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/333x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/450x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/500x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/550x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/333x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/450x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/500x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/550x600.png" class="img-fluid" alt="">
</div>
<div class="w-auto flex-grow-1">
<img src="https://via.placeholder.com/333x600.png" class="img-fluid" alt="">
</div>
</div>
</div>

How do I create a grid-styled layout without grid or flexbox?

I want to create a grid styled layout without using grid or flexbox. I am assuming I need float for this. One of the issues is that the images aren't' filling the entire space. Any advice on how to do this?
.container {
max-width: 90%;
margin: 0 auto;
}
.grid-item {
width: 25%;
float: left;
}
.grid-item img {
object-fit: cover;
width: 100%;
height: auto;
}
<div class="container">
<div class="grid">
<div class="grid-item phone">
<img src="https://picsum.photos/500/300" alt="" />
</div>
<div class="grid-item camera">
<img src="https://picsum.photos/400/200" alt="" />
</div>
<div class="grid-item watch">
<img src="https://picsum.photos/500/300" alt="" />
</div>
<div class="grid-item camera">
<img src="https://picsum.photos/400/200" alt="" />
</div>
<div class="grid-item watch">
<img src="https://picsum.photos/500/300" alt="" />
</div>
<div class="grid-item camera">
<img src="https://picsum.photos/400/200" alt="" />
</div>
<div class="grid-item watch">
<img src="https://picsum.photos/500/300" alt="" />
</div>
</div>
</div>
I suggest using the columns rule, specifying the number of columns and a width of 1/4 of the total width for responsiveness:
columns: 4 25vh;
Thus, the template with the image is now responsive without media queries.
.container {
max-width: 90%;
margin: 0 auto;
}
.grid {
columns: 4 25vh;
}
.grid-item {
/*width: 25%;
float: left;*/
}
.grid-item img {
object-fit: cover;
width: 100%;
height: auto;
}
<div class="container">
<div class="grid">
<div class="grid-item phone">
<img src="https://picsum.photos/500/300" alt="" />
</div>
<div class="grid-item camera">
<img src="https://picsum.photos/400/200" alt="" />
</div>
<div class="grid-item watch">
<img src="https://picsum.photos/500/300" alt="" />
</div>
<div class="grid-item camera">
<img src="https://picsum.photos/400/200" alt="" />
</div>
<div class="grid-item watch">
<img src="https://picsum.photos/500/300" alt="" />
</div>
<div class="grid-item camera">
<img src="https://picsum.photos/400/200" alt="" />
</div>
<div class="grid-item watch">
<img src="https://picsum.photos/500/300" alt="" />
</div>
</div>
</div>

bootstrap 4 - arrange images in particular?

I created a simple page in my Laravel and in that page, I have three sections which is mainly navigation, content and footer. I got the navigation and footer layout completed. In the content section, I wish to use bootstrap to place my images layout like this:
Image:
This is just a general idea but I need it to be responsive so it fits the mobile layout as well.
What I have tried is to use Masonry for CSS but it does not fit the mobile layout responsively. I tried using bootstrap 4 but the alignment between IMG3 and IMG4/5 are too far apart and placing margin does not align them near enough.
My code:
#section('content')
<div class="container">
<div class="row">
<div class="col-sm">
<img src="{{asset('/images/Home_Bed.jpg')}}" class="img-fluid custom-position-1" alt="">
</div>
<div class="col-sm col-8">
<img src="//via.placeholder.com/350x150" class="img-fluid custom-position-2" alt="">
</div>
<div class="col-sm custom-position-3">
<img src="{{asset('/images/Home_Kitchen.jpg')}}" class="img-fluid custom-position-3" alt="">
</div>
</div>
<div class="row align-items-end">
<div class="col col-8">
<img src="{{asset('/images/Shop_Page.jpg')}}" class=" img-fluid custom-position-4" alt="">
</div>
<div class="col">
<img src="{{asset('/images/Home_Sofa.jpg')}}" class="img-fluid custom-position-5" alt="">
</div>
</div>
</div>
#endsection
.row {
margin-bottom: 30px;
}
.custom-position-1{
position: relative;
bottom: -160px;
left: 50px;
height:100px;
object-fit: cover;
}
.custom-position-2{
position:relative;
right:100px;
object-fit:cover;
}
.custom-position-3{
position: relative;
bottom: -160px;
left: 50px;
height:100px;
object-fit: cover;
}
.custom-position-4{
position: relative;
height: 400px;
width:800px;
object-fit: cover;
}
.custom-position-5{
position: relative;
height:300px;
left:50px;
top: 100px;
object-fit: cover;
}
Does anyone have any idea how to make it good?
Are you looking for a solution like this?
If you want to have some spacing use margin classes.
img {
width: 100%;
height: 100%;
}
[class*="col-"],
.flex-grow-0 {
border: 1px solid black;
}
.img-fluid {
padding: 5px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div class="container">
<div class="row m-0">
<div class="col-8 p-0">
<div class="row m-0">
<div class="col-6 p-0">
<img src="http://placehold.it/300x300" class="img-fluid" alt="" />
</div>
<div class="col-6 p-0">
<img src="http://placehold.it/300x300" class="img-fluid" alt="" />
</div>
<div class="col-12 p-0">
<img src="http://placehold.it/300x300" class="img-fluid" alt="" />
</div>
</div>
</div>
<div class="col-4 p-0 d-flex">
<div class="d-flex flex-column">
<div class="flex-grow-0 mt-auto">
<img src="http://placehold.it/300x500" class="img-fluid" alt="" />
</div>
<div class="flex-grow-0">
<img src="http://placehold.it/400x400" class="img-fluid" alt="" />
</div>
</div>
</div>
</div>
</div>

divide three images equally and image name too appear their recpective image

Actually the image is 450*450pixels (both height and width is 15.88cm)
so i want to adjust them in equally to screen please see my screen shot and as you see the name also came in the bottom of each image the name should not so much big and small too it just fit with the image
img {
padding float: left;
border-radius: 150%;
}
<h1>ACTOR</h1>
<img src="img/chadwick.jpg" alt="chadwick" height="15%" width="15%">
<img src="img/michael.jpg" alt="michel" height="15%" width="15%">
<img src="img/lupita.jpg" alt="lupita" height="15%" width="15%">
Bootstrap 3 solution
HTML structure
<div class="row">
<div class="col-...">
<img ...>
<h5>...</h5>
</div>
</div>
Added padding-left and padding-right to each column to make space. Added margin-top to <h5> (Could be something else).
Example
.my-row>div {
padding: 0 7vw;
}
.my-row h5 {
margin-top: 15px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row my-row">
<div class="col-xs-4 text-center">
<img src="http://placehold.it/450x450" alt="" class="img-responsive img-circle">
<h5>Name 1</h5>
</div>
<div class="col-xs-4 text-center">
<img src="http://placehold.it/450x450" alt="" class="img-responsive img-circle">
<h5>Name 2</h5>
</div>
<div class="col-xs-4 text-center">
<img src="http://placehold.it/450x450" alt="" class="img-responsive img-circle">
<h5>Name 3</h5>
</div>
</div>
Bootstrap 4 solution
<div class="row text-center">
<div class="col">
<div><img src="https://via.placeholder.com/450x450" alt="chadwick"></div>
<div>chadwick</div>
</div>
<div class="col">
<div><img src="https://via.placeholder.com/450x450" alt="michel"></div>
<div>michel</div>
</div>
<div class="col">
<div><img src="https://via.placeholder.com/450x450" alt="lupita"></div>
<div>lupita</div>
</div>
</div>

Get the images straight to the div width

I have a image gallery which is not straighting/fullfilling the full width in a row
html
<div class="col-md-6">
<figure class="gallery">
<img src="images/blog_1.png" alt="img">
<img src="images/blog_1.png" alt="img">
<img src="images/blog_1.png" alt="img">
<img src="images/blog_1.png" alt="img">
<img src="images/blog_1.png" alt="img">
<img src="images/blog_1.png" alt="img">
</figure>
</div>
css
.gallery{
display:table;
}
.gallery img{
display: inline-block;
padding: 2px;
background: transparent;
}
display:table-cell is also not working . There is 6 images here
.gallery{
display:table;
white-space: nowrap;
}
.gallery img{
display: inline-block;
padding: 2px;
background: transparent;
}
<div class="col-md-6">
<figure class="gallery">
<img src="http://placehold.it/100x100" alt="img" />
<img src="http://placehold.it/100x100" alt="img" />
<img src="http://placehold.it/100x100" alt="img" />
<img src="http://placehold.it/100x100" alt="img" />
<img src="http://placehold.it/100x100" alt="img" />
<img src="http://placehold.it/100x100" alt="img" />
</figure>
</div>
From what I understood (since you have not mentioned the image size.)
.gallery{
}
.gallery > div{
padding:2px;
}
.gallery img{
background: transparent;
width:100%;
}
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="col-md-12">
<div class="gallery row">
<div class="col-xs-2">
<img src="http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg" alt="img"/>
</div>
<div class="col-xs-2">
<img src="http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg" alt="img"/>
</div>
<div class="col-xs-2">
<img src="http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg" alt="img"/>
</div>
<div class="col-xs-2">
<img src="http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg" alt="img"/>
</div>
<div class="col-xs-2">
<img src="http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg" alt="img"/>
</div>
<div class="col-xs-2">
<img src="http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg" alt="img"/>
</div>
</div>
</div>
Add Below css
.gallery{
display:table;
margin:0;
}
DEMO