bootstrap align img same line [closed] - html

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I would like to have the IMG aligned on the same line using bootstrap how to do it? I am quite junior.
Is it necessary to define col-12 below the row?
img {
width: 15rem;
height: 15rem;
margin: 10px 0;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>

Contain two different sized images on the same row (Option 1)
The Bootstrap way... Adding .img-fluid class to the images applies max-width: 100%; and height: auto; so that they scale with the parent element.
img {
height: 15rem;
}
.row {
background: #e3e3e3; /* let's visualise the row */
}
.row div[class^='col'] {
padding: 10px;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img class="img-fluid" src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img class="img-fluid" src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>
Contain two different sized images on the same row (Option 2)
Adding object-fit: contain; allows the images to fill their container. We'll also need to remove the .img-fluid class
img {
height: 100%;
width: 100%;
object-fit: contain;
}
.row {
background: #e3e3e3; /* let's visualise the row */
}
.row div[class^='col'] {
padding: 10px;
height: 15rem;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>
Images fill their container
Changing to object-fit: cover; allows the images to fill their container.
img {
height: 100%;
width: 100%;
object-fit: cover;
}
.row {
background: #e3e3e3; /* let's visualise the row */
}
.row div[class^='col'] {
padding: 10px;
height: 15rem;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row" *ngFor="let img of lstImg">
<div class="col-sm-3" *ngIf="img != 'tImg'; else elseBlock">
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
<div class="col-sm-6" #elseBlock>
<img src="https://thumbs.dreamstime.com/z/tv-test-image-card-rainbow-multi-color-bars-geometric-signals-retro-hardware-s-minimal-pop-art-print-suitable-89603635.jpg" alt="img">
</div>
</div>
</div>
More info on the CSS object-fit Property here.

Related

How would I achieve this layout?

I need it to be responsive.. I've tried setting larger image as a div background but it's not responsive.. I end up with the two right images large than the large image... I'm not sure what to do.. I'm using bootstrap, please see my code and any advice would be appreciated.
Also, the two images on the right will display:none on mobile.. so it's not mobile I'm worried about, its from ipad and up that I need it to be responsive and all stay aligned
.large-image {
div {
position: relative;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
width: 100%;
}
}
.large-image-two {
div {
height: 50vw;
#include breakpoint(up, xl) {
height: 43vw;
}
}
#include breakpoint(up, xl) {
max-height: 1100px;
}
}
.tile {
display: none;
#include breakpoint(up, lg) {
display: block;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<section class="image-tile">
<div class="container-fluid padding-sides padding-large pt-0">
<div class="row">
<div class="col-xxl-10 col-lg-9 col-12 large-image large-image-two">
<div class="full-height" style="background-image: url(https://i.picsum.photos/id/847/1370/700.jpg?hmac=kz_PXEVjCOQtig90AKAetNcWNNoJdWMjRMYhOLc6r7w)"></div>
</div>
<div class="col-xxl-2 col-lg-3 col-12 tile">
<div class="row d-flex flex-column full-height">
<div class="top-image col-8">
<img class="full-width" src="https://i.picsum.photos/id/682/250/400.jpg?hmac=ub0-x7bHa0z6PHozDd9A5baHp22hyWuq0NZuhHYmmBE">
</div>
<div class="col-4">
<img class="full-width" src="https://i.picsum.photos/id/545/250/270.jpg?hmac=lfBUvuMl6_uUp1lJI6e4A7KBiB4vzkocSlai_L0fUyE">
</div>
</div>
</div>
</div>
</div>
</section>

img-fluid not correctly resizing image height

I am working with Bootstrap 4 and am trying to have a grid of 4 images, 2 on top and 2 on bottom. I am using the img-fluid class but the image resizes based on the width only making the image height too large and it is getting cut off. I tried setting max-height: 100% but that didn't work.
What's going wrong?
.images-container {
height: 95vh;
}
.nav-button {
width: 100%;
height: 50%;
margin: auto;
font-size: 5em;
color: black;
}
.footer-container {
text-align: center;
}
.bottom-nav-box {
font-size: 1.5em;
margin: 0;
}
.sim-image {
max-height: 100%;
}
i {
margin: auto;
}
body {
height: 100%;
}
html {
height: 100%;
}
footer {
height: 5vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-sm-10 h-100 center-container">
<div class="row images-container">
<div class="row top-images-row h-50">
<div class="w-50 d-inline-block image-div">
<img src="https://lorempixel.com/875/656/" class="sim-image" id="simulatorImageTopLeft">
</div>
<div class="w-50 d-inline-block image-div" id="simulatorImageTopRight">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageTopRight">
</div>
</div>
<div class="row bottom-images-row h-50">
<div class="col image-div center-block text-center">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomLeft">
</div>
<div class="col image-div center-block text-center">
<div class="row">
<div class="col center-block text-center">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomRight">
</div>
<div class="col center-block text-center">
<img src="https://lorempixel.com/875/656/" class="img-fluid sim-image" id="simulatorImageBottomRight">
</div>
</div>
</div>
</div>
</div>
</div>
Not 100% sure I'm interpreting the question correctly, but if you want a 2×2 grid of images then you should just be able to do it relatively quick like this;
html:
<div class="panel">
<img src="blah" />
<img src="blah" />
<img src="blah" />
<img src="blah" />
</div>
css:
.panel {
width = whateversizeyouwant;
height = whateversizeyouwant;
display: flex;
flex-wrap: wrap; //the flex stuff should make it display 2×2
}
.panel > img {
width: 50%;
height: 50%;
}
That should work for any set of 4 images.
If you want them to stay the same aspect ratios then under .panel > img set height: auto. Keep in mind this won't give you a perfect square, and the size of the panel div will impact how the images can be sized.

image scale proportionally within specified column bootstrap 3

I'm trying to get image fit with bootstrap column in a row proportionally within specified height 300px, i have tried so many ways but unfortunately its not fit with column width (my 1st image dimension is 262*380 and 2nd image dimension 546*380 and im using img-responsive class)
I need my image should be like this Expected
but im getting like this Actual
My HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="grid">
<img src="https://static5.thumbtackstatic.com/_assets/images/release/pages/jobs/submodules/gallery/images/lunch-5502ae26.jpg" class="img-responsive" alt="">
</div>
</div>
<div class="col-md-8">
<div class="grid">
<img src="https://static7.thumbtackstatic.com/_assets/images/release/pages/jobs/submodules/gallery/images/lounge-0cd3d802.jpg" class="img-responsive" alt="">
</div>
</div>
</div>
My CSS
.grid {
height: 300px;
border-right: 4px;
}
.grid img {
height: 100%;
}
Try this HTML:
<div class="container">
<div class="row">
<div class="col-md-4 bg-cover grid" style="background-image: url(https://static5.thumbtackstatic.com/_assets/images/release/pages/jobs/submodules/gallery/images/lunch-5502ae26.jpg)">
</div>
<div class="col-md-8 bg-cover grid" style="background-image: url(https://static7.thumbtackstatic.com/_assets/images/release/pages/jobs/submodules/gallery/images/lounge-0cd3d802.jpg)">
</div>
</div>
</div>
with this CSS:
.grid {
height: 300px;
}
.grid img {
height: 100%;
}
.bg-cover {
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
height: 300px
}

Two small align images beside a large image

I'm struggling on how I will code to create a two vertical images and is it possible to lessen the height of the larger image without lessen the width? because I need to fit it on col-md-8 any thoughts about this?
this is the image I need to make.
Click here
HTML and CSS code:
.img-big{ height: 100%;width: 100%; }
<div class="row col-md-8">
<img class="row img-big img-responsive" src="/assets/icons/people-crowd-child-kid-large.jpg"></div>
</div
the above code is what I've used to make the bigger image beside image 2 and 3. the dimension of the large image is 900x767
You can use the flexbox property to achieve what you want and set the image as background.
body, html {
margin: 0;
padding: 0;
color: white;
}
.container {
height: 767px;
display: flex;
}
.left {
background-image: url('http://i.imgur.com/AzeiaRY.jpg');
background-size: cover;
flex: 1;
}
.right {
display: flex;
flex-direction: column;
flex: 0 0 50%;
}
.one {
background-image: url('http://i.imgur.com/AzeiaRY.jpg');
background-size: cover;
flex: 50%;
}
.two {
background-image: url('http://i.imgur.com/AzeiaRY.jpg');
background-size: cover;
flex: 50%;
}
<div class="container">
<div class="left">Left image</div>
<div class="right">
<div class="one">First image</div>
<div class="two">Second image</div>
</div>
</div>
In Bootstrap 5, you can use d-grid gap-x. For example:
HTML
.content {
background-color: transparent;}
.content .left, .content .right {
float: left;}
.full-width-band-hd {
margin-top: -35px;}
.txt-yellow {
color: var(--bs-yellow);}
<section class="container-lg">
<div class="content row">
<h2 class="txt-yellow full-width-band-hd">Head</h2>
<!-- Left Side -->
<h6 class="text-yellow">H6 head</h6>
<h3 class="text-yellow">H3 head</h3>
</div>
<!-- style in content row will become a class later-->
<div class="content row" style="height: 642px;">
<div class="col-md-4 left d-grid gap-3">
<div class="">
<img src="image1" width="100%" height="auto" alt="fp1">
</div>
<div class="">
<img src="image2" width="100%" height="auto" alt="fp2">
</div>
</div>
<!-- End of Left Side -->
<div class="col-md-8 left">
<div class="">
<img src="image3" width="100%" height="auto" alt="fp3">
</div>
</div>
<!-- End of col-md-8 -->
</div><!-- content row -->
</section>
You should format your code like below:
<div class="row">
<div class="col-md-8">
<img class="img-big img-responsive" src="https://en.apkshki.com/storage/5/icon_5dcfce7f86906_5_w256.png"></div>
</div>
<div class="col-md-4">
<img src="https://en.apkshki.com/storage/5/icon_5dcfce7f86906_5_w256.png"></div>
<img src="https://en.apkshki.com/storage/5/icon_5dcfce7f86906_5_w256.png"></div>
</div>
</div>
As you can see the two images at the bottom in col-md-4 if you spread the width 100% the next image will drop below.
You shouldnt really have a class with both a row and a col-md in the class name. (See http://getbootstrap.com/css/)
With regards to reducing the height and not the width are you not able to crop the image down on Paint or Photoshop and upload the image with the correct height?

Bootstrap 3 with Less - How I manage to center the container horizontally and vertically

I'm trying to build a webpage with the bootstrap 3 framework and the less tool.
For one special page I'd like to center the container horizontally and vertically. I know there are allready some questions out there, but for me they do not work.
So this are the solutions i've tried following:
HTML:
<div class="container hvcenter" > <!-- Container -->
<div class="row"> <!-- Bootstrap Row -->
<div class="col-sm-4">
<img src="img/logo.png" alt="logo" class="img-responsive">
</div>
<div class="col-sm-offset-1 col-sm-7">
<p>Textblock ... </p>
</div>
</div>
</div>
CSS:
.container {
max-width: 85%;
padding-top: 3em;
padding-bottom: 3em;
background-color: silver;
border-radius: #border-radius-large * 6;
}
.hvcenter {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
This effects thath the div is centered horizontally, but not vertically.
An other try was to build in one mor class for vertical alignment and to use the builtin solution for horizontal alignment (.center-block):
HTML:
<div class="container center-block vcenter" > <!-- Container -->
<div class="row"> <!-- Bootstrap Row -->
<div class="col-sm-4">
<img src="img/logo.png" alt="logo" class="img-responsive">
</div>
<div class="col-sm-offset-1 col-sm-7">
<p>Textblock ... </p>
</div>
</div>
</div>
CSS:
.container {
max-width: 85%;
padding-top: 3em;
padding-bottom: 3em;
background-color: silver;
border-radius: #border-radius-large * 6;
}
.vcenter {
display: inline-block;
vertical-alignment: middle;
float: none;
}
I'm happy about every answer. Thanks for spending time on this.
.container {
max-width: 85%;
padding-top: 0;
background-color: silver;
border-radius: #border-radius-large * 6;
}
.vcenter {
display: flex;
align-items: center;
height:10em;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<div class="container center-block" > <!-- Container -->
<div class="row"> <!-- Bootstrap Row -->
<div class="col-xs-4">
<img src="img/logo.png" alt="logo" class="img-responsive">
</div>
<div class="col-xs-offset-1 col-xs-7 vcenter">
<p>Textblock ... </p>
</div>
</div>
</div>
You should try flex. Check out this link:
https://jsbin.com/pezesaquza/edit?html,css,output