Display image inside the box without stretching it - html

I developed an image gallery.
My problem is that I am using 100% height and sometimes the image stretches, ending up losing quality.
Is there a way to place the image in the center of the box without stretching it and in the center if it is not tall enough to fill the box completely?
Demo
My code
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="row">
<div class="drop dropp">
<div class="abc">
<ngb-carousel style="outline: none;" id="carousel" #carousel *ngIf="data" data-interval="false"
data-ride="carousel" data-pause="hover" (slide)="change($event)">
<ng-template *ngFor="let imgIdx of data; let i = index" [id]="i" ngbSlide>
<div class="picsum-img-wrapper">
<img [src]="imgIdx.image" style="border-radius: 8px; object-fit: fill;" class="img-responsive">
</div>
</ng-template>
</ngb-carousel>
</div>
</div>
<div class="row">
<div class="Upcard" *ngFor="let imgIdx of belowImageData; let i = index">
<img class="img-responsive" [src]="imgIdx.image" style="width: 100%; object-fit: fill; height: 100%; border-radius: 8px;">
</div>
</div>
</div>
</div>
**My Problem **
As you can see in the image, it fills the whole box but is deformed :( I want it not to lose quality and if it doesn't have enough height, stay centered on the box. I want the box to have a fixed height.

If you don't care about the old browsers you can use object-fit property on the img directly.
for example:
object-fit: cover;
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
Otherwise you can add an empty div, give it the full size of the box, and use the image as a background of that div. Then use background-size property.
Hope it helps.

I have tested the code, you can try adding the below code in css.edit and take a look
.img-responsive {
object-fit: contain; height:200px!important;
}
or to need all images to fit properly you have to remove the div global height 100 percentage. and set the height specific for each div. so you don't need height parameter in .img-responsive class.

Related

How to set beckground image full div without using height

I have first div generated dynamically and second div I have placed it.
First div image is taking 200X200 by dynamically. Same size image i want to set it through css in second div.
But when I tried to set it and it's not taking full height 200 of the image.
If i mention min-height it's not coming properly for all resolutions. I don't want to specify min-height for the media queries.
How to set background image height automatically?
First Div structure
<div class="product-layout col-lg-3 col-md-3 col-sm-6 col-xs-6">
<div class="product-thumb transition">
<div class="image"><img src="../image/cache/catalog/savouries/karasevu-200x200.jpg" alt="Karasevu" title="Karasevu" class="img-responsive"></div>
<div class="caption">
<h4>Karasevu</h4>
<p class="price"> $0.00 </p>
</div>
<div class="button-group home-btn">
<button class="producthome-btn" type="button" onClick="cart.add('71');"><i class="fa fa-shopping-cart cart-icon"></i> <span class="">Add to Cart</span></button>
</div>
</div>
</div>
Second Div structure
<div class="more-link product-layout col-lg-3 col-md-3 col-sm-6 col-xs-12">
<div class="a-more-link"> VIEW MORE </div>
</div>
Use the background-size property.
.someClass {
background-image: url(someImage.jpg);
background-size: cover;
}
First method use background-size to 100%
.classname {
background-image: url(img.png);
background-size: 100%;
}
Second method use background-size to cover
.classname {
background-image: url(img.png);
background-size: cover;
}
Try using background-size:cover and you can center it by using background-position:center
Set your body element to 100% like this:
body{
height: 100%;
}
then use inherit or set your bg height to 100% like this:
.yourParentDiv{
height: 100%;
// your other code here
}

Bootstrap container always has 0 height

I am trying to execute some simple container code to center an image, I cannot find it why my Container has a height of 0.
#test {
background-image: url('https://via.placeholder.com/150');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
<div class="container">
<div class="row">
<div class="col">
<div id="test">
</div>
</div>
</div>
</div>
Why does the container have a height of 0, and hence my image not appearing?
By default, block elements get their heights from their content. Content meaning what goes between the opening and closing tag of the element in the HTML. A background image would not be content, but can be thought of more like decoration.
You can set the height manually with css to whatever you like. Keep in mind though that an empty element is unsemantic code.
Adding a height property could fix this as mentioned by you in comments.
Another alternative that would solve the problem with varying image sizes could be to use an img tag inside #test element.
<div id="test" style="background-image: url(/my-image.jpg);">
<img src="/my-image.jpg" style="visibility: hidden;" />
</div>
This way the div takes up the height based on the image size.
<div class="text-center d-flex" style="height: 100vh">
<div class="container m-auto">
<div class="row">
<div class="col-8 mx-auto">
<h1> YOU CAN DO IT </h1>
</div>
<div class="col-8 mx-auto">
<h1> I should be centered </h1>
</div>
</div>
</div>
</div>
Using margin:auto to vertically-align a div
As the second answer from the linked stackoverflow question over, it was because my parent was not of type flex.

Resizing <img> in Bootstrap carousel to parent element in CSS

I have a dynamic page that has a carousel. I have no control of the images, because they are uploaded by the user. There are some guidelines, but I can't be sure that they are gonna follow them.
So, in my development tests, I have three images: a 100x100, a 2800x1500 and a 600x400
I want the carousel be in the middle of the page, and be responsive. I have no problem with that if I have more or less consistent images.
...
<div class="carousel-item active">
<div class="carousel-img">
<img src="img1.png">
</div>
</div>
<div class="carousel-item">
<div class="carousel-img">
<img src="img2.png">
</div>
</div>
<div class="carousel-item">
<div class="carousel-img">
<img src="img3.png">
</div>
</div>
...
The CSS for "carousel-img" currently (I have tried lots of combinations, and nothing seems to work) is:
.carousel-img {
height: 400px;
width: 100%;
overflow: hidden;
}
.carousel-img img {
height: 100%;
width: 100%;
object-fit: cover;
}
Now, the very large image, works as intended. It resizes not using more than 400px vertically. The problem is with the other two images. The smallest one, resizes from 100px to 400px vertically and up to around 400px vertically, it doesn't fills up horizontally, despite the 100%.
So how can I tell CSS to resize the images (without changing aspect ratio) to use 100% of the parent container. Let me try to explain better: if the carousel is inside a <div class="col-md-6">, it should use all the width and up to 400px.
Edit
Please accept my apologies to make everyone waste their time. There's no error in my original code. But there was a previous DIV before the carousel that read:
<div class="row justify-content-center">
I thought this might be no problem since it is intended to center the following DIVs, I don't know why but this justify-content-center prevented the images to resize appropriately, when I removed it, the images inside the carousel started working as intended.
Thank you very much to all who offered to help, today I learned something different.
Find a working snippet in this jsfiddle – this is using the carousel with indicators provided in the documentation and the image sizes you are considering.
The only changes you have to make is to remove your carousel-img div from your HTML, and change the .carousel-img rulesets in CSS to be applied to carousel-item instead:
...
<div class="carousel-item active">
<img src="img1.png">
</div>
<div class="carousel-item">
<img src="img2.png">
</div>
<div class="carousel-item">
<div class="carousel-img">
<img src="img3.png">
</div>
</div>
...
And the CSS:
.carousel-item {
height: 400px;
width: 100%;
overflow: hidden;
}
.carousel-item img {
height: 100%;
width: 100%;
object-fit: cover;
}
Have also removed the active class from all your items except the first as that would cause problems for Bootstrap JS to iterate through the slides.
You would have already got a first from me for using object-fit: cover from the start!

When I set my background-size to anything other than cover the text inside my div overfllows whenever the image resizes. How do I prevent that?

I want my background image to be responsive so changed the size to 100% and auto but now the texts that I have inside my section is overflowing out of the image. This wouldn't happen if my image size was cover. How do prevent the text to overflow from the image.
.section1{
height: 600px;
background: url("img/Rectangle 1.jpg") no-repeat center;
background-size: cover;
}
html
<section class="section1">
<div class="container info">
<div class="row">
<div class="col-lg-12 " align="center">
Make Future Visible <span>™</span>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12 info2" align="center">
Real-time predictive analytics for refining equipment eliminate accidents<br>
and fires, increases refinery uptime, decreases downtime and drastically<br>
reduces maintenance costs.
</div>
</div>
</div>
</section>
If you set the background position css to top center it will make the rectangle image start from the top of <section> element.
If you want the image 100% width, centred, and responsive, then a rectangle image will not work on a mobile.
See demo here https://jsfiddle.net/joshmoto/tfesu0cx/1/

How to fit an image inside a Bootstrap 3 div?

I am using a div tag in code as shown below:
<div class="col-sm-6 col-md-6 col-lg-4">
<img src="images/dummy_image.png" class="img-responsive">
</div>
The user can upload any kind of image and have it displayed here. I want to fit the image inside of the div, meaning specifically that I need to cover the full div using that image as it's resized.
I am using Bootstrap 3.3.7. Please advise.
Just add you images width to 100%.
But as you are saying user will upload various kind of images, so can use object-fit property.
Add the CSS like this:
.fit-image{
width: 100%;
object-fit: cover;
height: 300px; /* only if you want fixed height */
}
<div class="col-sm-6 col-md-6 col-lg-4">
<img src="images/dummy_image.png" class="img-responsive fit-image">
</div>
You will find the details about object-fit and object-position here : https://css-tricks.com/on-object-fit-and-object-position/
For Bootstrap 4
<div class="col-sm-6 col-md-6 col-lg-4">
<img src="images/dummy_image.png" class="img-fluid">
</div>
bootstrap.css
.img-fluid {
max-width: 100%;
height: auto
}
It is safe to assume that by fitting you mean covering all of the width. This is because
You typically do not know the height just by using col-sm-6 or col-md-6 or col-lg-4.
There is a huge probability of loss in aspect ratio of the image if you try to resize it according to your own will.
Use <img src = "images/dummy_image.png" class = "img-responsive" width = "100%" /> for fitting the column. This will fit your image width-wise into the column and will automatically modify the height (keeping the aspect ratio in mind), so you do not have to worry about image getting illogically resized.