CSS dynamic image size in bootstrap - html

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>

Related

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>

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 4 - Grid 1 and 2 Change position

Example what i need:
Image:
My code is this:
<div class="container">
<div class="row">
<div class="col-3">
<img src="image1.png" class="img-thumbnail float-left" alt="Novi Sad">
</div>
<div class="col-6">
<img src="image1.png" class="img-thumbnail mx-auto d-block" alt="Belgrade">
</div>
<div class="col-3">
<img src="image1.png" class="img-thumbnail float-right" alt="Niš">
</div>
</div>
<!-- THIS IS PROBLEM -->
<div class="row">
<!-- THIS COLUMN NEED TO BE BELOW 1 COLUMN / LEFT SIDE -->
<div class="col-3">
<img src="image1.png" class="img-thumbnail float-left" alt="Novi Sad">
</div>
<!-- THIS COLUMN NEED TO BE BELOW 3 COLUMN / RIGHT SIDE -->
<div class="col-3 ml-auto">
<img src="image1.png" class="img-thumbnail float-right" alt="Novi Sad">
</div>
</div>
<!-- END PROBLEM -->
<div class="row">
<div class="col-4">
<img src="image1.png" class="img-thumbnail float-left" alt="Novi Sad">
</div>
<div class="col-4">
<img src="image1.png" class="img-thumbnail mx-auto d-block" alt="Novi Sad">
</div>
<div class="col-4">
<img src="image1.png" class="img-thumbnail float-right" alt="Novi Sad">
</div>
</div>
</div>
I can't figure where is the problem. If someone know where problem is, write solution. Thanks
PS. Sorry for my Bad english
Thanks All..
Edit: I've updated my answer to include background images, and to mimic the original layout better. Thanks to AndreiGheorghiu for help answering this more accurately.
.grid{
display: grid;
grid-gap:10px;
height:500px;
grid-template-columns: 1fr .2fr 1fr .2fr 1fr;
grid-template-areas: "topLeft large large large topRight"
"midLeft large large large midRight"
"bottomLeft bottomLeft bottomCenter bottomRight bottomRight";
}
.pic{
background: url(https://placeimg.com/400/400/any) no-repeat center/cover;
font: 700 20px sans-serif;
color:white;
display:flex;
align-items:center;
justify-content:center;
text-shadow:2px 2px 5px #000;
}
.pic-1{
grid-area: topLeft;
}
.pic-2{
grid-area: large;
}
.pic-3{
grid-area: topRight;
}
.pic-4{
grid-area: midLeft;
}
.pic-5{
grid-area: midRight;
}
.pic-6{
grid-area: bottomLeft;
}
.pic-7{
grid-area: bottomCenter;
}
.pic-8{
grid-area: bottomRight;
}
<div class="grid">
<div class="pic pic-1">Foxwoods</div>
<div class="pic pic-2">New York City</div>
<div class="pic pic-3">Las Vegas</div>
<div class="pic pic-4">Philadelphia</div>
<div class="pic pic-5">San Francisco</div>
<div class="pic pic-6">Miami</div>
<div class="pic pic-7">Boston</div>
<div class="pic pic-8">Washington D.C.</div>
</div>
You can try this code,
Note: Included responsive classes. Run this code as a full page
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail" alt="Novi Sad">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail mt-md-4" alt="Novi Sad">
</div>
<div class="col-md-6">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail" alt="Belgrade">
</div>
<div class="col">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail" alt="Niš">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail mt-md-4" alt="Novi Sad">
</div>
</div>
<div class="row mt-md-4">
<div class="col-md-4">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail" alt="Novi Sad">
</div>
<div class="col-md-4">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail" alt="Novi Sad">
</div>
<div class="col-md-4">
<img src="https://via.placeholder.com/800x800" class="img-fluid img-thumbnail" alt="Novi Sad">
</div>
</div>
</div>
You need to put the small images in the same col. And display them as blocks: display:block; width: 100%; height: auto;.
Here's how I'd personally do it. I'm using .no-gutter class on .rows and white borders on images:
[my-gallery]:not(#_) {
padding: 8px;
}
[my-gallery] img {
display: block;
background-color: #f5f5f5;
width: 100%;
height: auto;
border: 8px solid white;
}
[my-gallery] .strethed img {
height: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid" my-gallery>
<div class="row no-gutters">
<div class="col">
<img src="https://picsum.photos/300/200/?random" alt="Novi Sad">
<img src="https://picsum.photos/303/202/?random" alt="Novi Sad">
</div>
<div class="col-6 strethed">
<img src="https://picsum.photos/306/204/?random" alt="Belgrade">
</div>
<div class="col">
<img src="https://picsum.photos/309/206/?random" alt="Niš">
<img src="https://picsum.photos/312/208/?random" alt="Novi Sad">
</div>
</div>
<div class="row no-gutters" >
<div class="col">
<img src="https://picsum.photos/315/210/?random" alt="Novi Sad">
</div>
<div class="col">
<img src="https://picsum.photos/318/212/?random" alt="Novi Sad">
</div>
<div class="col">
<img src="https://picsum.photos/321/214/?random" alt="Novi Sad">
</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>

bootstrap grid system to format picture

i was using bootstrap grid system to format picture like:
this is my code :
<div class="row">
<div class="col-md-4">
<div class="well"><img src="..." class="img-responsive"">
<br/>
<br/>
<br/>
<br/>
<br/>
</div>
</div>
<div class="col-md-8">
<div class="row">
<div class="col-md-6">
<div class="well"><img src="..." class="img-responsive" alt="Responsive image"></div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="well"><img src="..." class="img-responsive" alt="Responsive image"></div>
</div>
</div>
</div>
</div>
I want the picture to be responsive so I put class="img-responsive" on the <img>
<img src="..." class="img-responsive" alt="Responsive image">
but end up like the right picture. if i dont use class="img-responsive" the image will show on full resulution. how i fix this? (please give me a easy explanation because i'm a noob :) )
You can apply CSS Flexbox rules on .row & .col-*-*. I've used some custom classes (all the .col-*-* has a class called .item). Like:
.row {
display: flex;
}
.item {
display: flex;
flex-direction: column;
}
.item.left {
flex-direction: row;
}
Have a look at the snippet below:
.row {
display: flex;
}
.item {
display: flex;
flex-direction: column;
}
.item.left {
flex-direction: row;
}
.item.left .well {
width: 100%;
align-self: stretch;
}
.item.left .well img {
height: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="item left col-sm-4">
<div class="well"><img src="http://placehold.it/200x200" class="img-responsive">
</div>
</div>
<div class="item col-sm-8">
<div class="row">
<div class="col-sm-6">
<div class="well"><img src="http://placehold.it/100x100" class="img-responsive" alt="Responsive image"></div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="well"><img src="http://placehold.it/100x100" class="img-responsive" alt="Responsive image"></div>
</div>
</div>
</div>
</div>
Hope this helps!
You should use following HTML structure as you need left and right columns having equal width:
<div class="container">
<div class="row">
<div class="col-xs-6">
// content....
</div>
<div class="col-xs-6">
// content....
</div>
</div>
</div>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="well"><img src="..." class="img-responsive">
<br>
<br>
<br>
<br><br>
</div>
</div>
<div class="col-xs-6">
<div class="well"><img src="..." class="img-responsive" alt="Responsive image"></div>
<div class="well"><img src="..." class="img-responsive" alt="Responsive image"></div>
</div>
</div>
</div>