I have a series of photos of different qualities and sizes. Some vertically, some are horizontal. On the details page, I’ll display each photo at a specific size. How can I control the photos so that they do not stretch and do not scratch their resolution?
I use Bootstrap 4 and Mvc.Core
For generic CSS to work on any image with any aspect ratio the usual method is to set one dimension (height or width) then set the other to auto.
I believe Bootstrap has a class for this, img-responsive or similar. That sets the width to 100% (the containing element defines the width) and the height is auto to maintain aspect.
I don’t recall whether that class uses width: 100% or max-width: 100%, but in my own CSS I tend to have:-
Because I want all images to be responsive by default without the need for a class and use max-width over width because that stops the images blowing up beyond their native size when in a larger parent element.
It could be done the other way, fixing the height and having auto width, if for example you want rows of images of the same height.
There are also methods to do this with flex. Perhaps Bootstrap 4 has bult-in functions for this too, but I don’t know.
Yes, I do the same, but when the image size is large, the image height on the screen will be very high
And when the photo width is low, a lot of photos will be taken. And if you set a specific height for a photo, the resolution will be lost
I am looking for a solution that I can control such things
One can post a variety of images with different dimensions (& aspect ratios) at the same height (widths will vary), or at the same width (heights will vary). One can set the height or width of each image separately by assigning a unique className to each image. What other choice would you like to see?.. Please describe how would it look (show us an example of how the images would look on a page) and how would you control it?
My problem is exactly what you wrote. As I can get any photo size, how do I set the page size to display the images properly?
In fact, imagine that Instagram gets a lot of photos in different sizes. What approach does this solve?
What size should I display in the page view of how horizontally and vertically, how and where the horizontal images are displayed.
While each and every portrait and horizontally have different sizes and if they are located in the same frame, they may cause the photo to stretch or not part of the photo …
I have one very simple but apparently very hard request…
With your photo/image editor or using simple code, please create a make-believe page with boxes that represent “properly displayed” images on a page. No image will be “stretched”, cropped, or distorted out of shape. Just draw boxes, each one representing an image. You can assume that the images will be reduced in size to fit within the boxes “properly”.
If you cannot show us how your mind sees “properly sized” images on a page, we cannot help you create it. At this time, your requirements for a web page of “properly sized” images are a fantasy that we cannot see. You don’t have to show us any code, but you must be able to show us how you imagine a page of “properly sized” images might look. We’ll take it from there. If a solution is possible, we’re pretty clever about figuring out how to do it.
Please realize that we are not mind readers or fortune tellers.
For example, this photo is at 640x764, and this is seen in the image that is not good at all
my view code this
<div class="container-fluid p-5">
<h1 class=" image-title">@Model.Title</h1>
<div class="col-md-8 p-0">
<img class="img-fluid" title="@Model.Title" alt="@Model.ImageName"
test test test test test test test test test test test test test test test test
<div class="col-md-2 ">
<div class="row ">
Do you mean that for arguments sake you want to display all your images at a specific size of say 300px x 300px (or whatever fixed/percentage size you have in mind)?
Obviously the quality of the images are out of the control of CSS but you can make the image fit into the 300 x 300 space by using object-fit:cover in the css. That will maintain the general aspect ratio of the image while making it fit into a square box fully. It is the same method as using background-size:cover for background images.
Of course that means the image will automatically be cropped to fit unless its aspect ratio exactly mimics the 300 x 300 square box. It does this by increasing both dimensions of the image uniformly until the box is filled completely. If for example you had a very wide image but it was only 100px tall then the height would be increased 3 times to fill the box but in return the image width must also be increased 3 times to maintain the correct aspect ratio for that image. The wider part of the image is then cropped so that only 300px of it is seen in the 300 x 300 box. This could result in important detail not being visible in the new box that you made for it. In the end this is purely a mathematical question as you obviously can’t make an image a different shape but have it look exactly the same.