How to keep aspect ratio of downsized images? - html

i'm building an website and came across this problem: i have a image with 345x300.
In my div, she gets the widths of 82 x 118, staying like this:
I need the image to have 82 x 82 width, and when i set it gets flattern.
What can i do to fix this problem? Sorry for my bad english.
If needed, this is the website i'm working: https://2018.escambofotografico.com.br/ (the problem i'm talking its in the end of the page)
Thanks everyone!

You can also do this using the object-fit css property.
I made a little example for you on how it works.
$(function(){
$('.list-group button').click(function(e) {
e.preventDefault()
$that = $(this);
$that.parent().find('button').removeClass('active');
$that.addClass('active');
$("img").removeClass();
$("img").addClass($that.prop('id'));
});
})
img {
height: 182px;
width: 182px;
}
.fill {
object-fit: fill;
}
.contain {
object-fit: contain;
}
.cover {
object-fit: cover;
}
.none {
object-fit: none;
}
.scale-down {
object-fit: scale-down;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<div class="row">
<div class="col-6">
<div class="list-group">
<button type="button" class="list-group-item list-group-item-action" id="fill">object-fit: fill;</button>
<button type="button" class="list-group-item list-group-item-action" id="cover">object-fit: cover;</button>
<button type="button" class="list-group-item list-group-item-action" id="contain">object-fit: contain;</button>
<button type="button" class="list-group-item list-group-item-action" id="none">object-fit: none;</button>
<button type="button" class="list-group-item list-group-item-action" id="scale-down">object-fit: scale-down;</button>
</div>
</div>
<div class="col-6">
<img src="http://coveractionspremium.com/images/SoftwareBoxCD-model6v2-coveractions1.jpg" alt="">
</div>
</div>
</div>
Here is a Codepen you can play around with:
https://codepen.io/gurgen/pen/jKmXXW

When you set the dimensions of an html image tag, those are the dimensions that will display. If you take a rectangular image and display it as a square, you're going to get some distortion. The solution is to either edit your images with some padding to make them the exact size (and shape) you want, or else display them at the same aspect ratio that the images start at.
Also, note that your image is much larger than you are displaying. If bandwith for you server or customers is a concern, you're better off to resize the image to the size you want to display. The lightroom image is approximately 42Kb, but you might be able to resize it at 82x82 and send a ~6Kb file instead. For a mobile or low bandwith customer, that savings multiplied by 12 images is 432Kb. Your server has to serve up those wasted bytes to every customer, which you may be paying for.
Looking at some of your other images, you're scaling them up. Modern browsers will try to scale images smoothly, but you'll always lose some display quality doing this. Your best bet is to size images at the exact dimensions you want to display when you can.

To avoid stretching images in a fixed size container, you can set the max-height and max-width on the image tag to 100%. Once the height or width fills the container, it won't stretch the image.
So, if you want your image in a 82x82px container, you can do this for each image:
<div class="thumbnail">
<img src="...">
</div>
With the following CSS:
.thumbnail {
height: 82px;
width: 82px;
}
.thumbnail > img {
max-height: 100%;
max-width: 100%;
}

Related

Adjust Bootstrap image resizing

I am trying to make a card div with two images inside, an up arrow and a down arrow.
When in full screen the images look okay and are the full size but due to Bootstraps responsive design, when i reduce the page resolution using the developer console in chrome, the images get smaller and smaller until they cannot be seen on a small device, such as a mobile.
How can i limit how small the images will go?
Thanks.
<div class='col-11 col-xs-11 col-md-5 col-lg-5' style='background-color:#dfdfdf;border-radius:5px;margin-right:10px;margin-bottom:10px;'>
<div class='col-2 col-xs-2 col-md-2 col-lg-2'>
<h4 class='card-header' style='text-align:center;height:100%;'>
<img style='height:50%;'src='uparrow' onmouseover=this.src='differentuparrow' onmouseout=this.src='uparrow' border='0'/>
<br><br>
<img style='height:50%; src='downarrow' onmouseover=this.src='differentdownarrow' onmouseout=this.src='downarrow' border='0'/>
</h4>
</div>
<div class='col-8 col-xs-8 col-md-8 col-lg-8'>
<h4 class='card-header' style=''><u>Title</u></h4>
<div class='card-body' style=''>
<p class='card-title'>Description</p>
<p class='card-text' style='display: inline-block; bottom:0;'>Points: 0<br>Replies: 0</p>
</div>
</div>
<div class='col-2 col-xs-2 col-md-2 col-lg-2' style='float:right;'>
<i class='fa fa-pencil fa-5' aria-hidden='true'> Edit</i>
<br> <i class='fa fa-trash fa-5' aria-hidden='true'> Delete</i>
</div>
</div>
https://jsfiddle.net/5a87h3ky/
I had to remove the original sources for the images due to confidentiality
You can set the minimum size of the image using the min-width: px; attribute.
This will prevent the image from getting any smaller than the given size.
There are several ways of controlling the size of images with CSS or in HTML. To control your arrows you can use min-height and max-height and also set min-width and max-width.
For example take image_arrow_up.jpg, you can set it's size with:
<head>
<style>
div {
max-height: 600px;
min-height: 400px;
max-width: 400px;
min-width: 200px;
}
</style>
</head>
Another way would be to use percentages, although this is not as concrete as min and max:
<!DOCTYPE html>
<html>
<head>
<style>
html, body {
height: 100%;
}
img.one {
height: auto;
width: auto;
}
img.two {
height: 50%;
width: 50%;
}
</style>
</head>
<body>
Another option is to use CSS with the image-set function, or with scrset + sizes:
.img {
background-image: url(examples/images/image-384.jpg);
background-image:
-webkit-image-set(
url(examples/images/image-384.jpg) 1x,
url(examples/images/image-768.jpg) 2x,
);
background-image:
image-set(
url(examples/images/image-384.jpg) 1x,
url(examples/images/image-768.jpg) 2x,
);
}
It also helps to make sure the original image is sized appropriately. For example make the original arrow image sized for a "regular" desktop monitor rather than an oversized arrow that may shrink in unexpected ways. Or you can use an image management service, such as Cloudinary or 3Scale to create responsive quality images.

Scale panel down appropriately when viewing on smaller screen

I have an application that is querying the Spotify API which returns some names and images.
I am listing all of these out on my page inside cards/panels like so:
<div class="col-md-4" v-if="type == 'tracks'" v-for="(track, index) in tracks">
<div class="card">
<div class="header bg-red">
<h2 class="nameHeading">
#{{ track.name }} <small>#{{ track.artists[0].name }}</small>
</h2>
<p class="header-dropdown m-r--5">#{{ index + 1 }}</p>
</div>
<div class="body">
<i :style="{ 'background-image': `url(${track.album.images[1].url})` }" :alt="track.name" class="bg-image"></i>
</div>
</div>
</div>
The images returned can be of varying sizes and dimensions and so my bg-image class helps render these consistently on the page:
.bg-image {
background-size: cover;
display: block;
width: 100%;
height: 325px;
background-position: center;
}
This works perfectly on desktops:
However the cards/panels get stretched when viewing on smaller screens:
Is there a way I can prevent this from happening and just scale the image down to keep it consistent with what you see on the desktop and to keep the panel in roughly the same shape?
My CSS skills are really quite basic, and so I have no clue where to start when it comes to something like this.
can you try this
.bg-image {
width:100%;
float:left;
background:url('http://media02.hongkiat.com/ww-flower-wallpapers/dandelion.jpg');
height:600px;
background-size:100% auto;
}
Setting background-size: 100% was definitely a step in the right direction. The only issue was that it then meant the image repeated above and below to fill in the rest of the height set by the .bg-image class.
Setting background-repeat: no-repeat solved this, but still left the blank space above and below.
Using some jquery $('.bg-image').css('height', $('.bg-image').width()); I was able to resize the height of the <i> tag appropriately - the issue I ran in to was that because I am using Vue, the cards weren't visible in the DOM straight after my API request.
To allow v-for to render these and then run the jQuery, I simply added this into my Vue method:
this.$nextTick(function()
{
$('.bg-image').css('height', $('.bg-image').width());
});
This now resizes appropriately on desktop and mobile devices.

Bootstrap responsive image issue

I'm new to this whole Bootstrap scene.
Trying to put an image as a navbar brand and have it resized when the windows does
<a class="navbar-brand" href="#"><img alt="Brand" src="images/logo.png" class="img-responsive" /></a>
I've also tried img-fluid and adding style='width:100%', but nothing works and its size is always static.
Put on the image the max-width:100%, shall work
Try this,
a {
display: block;
width: /* depending on how much space you wanted for your navbar-brand */
}
img {
max-width: 100%;
}
With images you'd want to add:
.img-responsive {
height: auto; // the height will be in the images correct aspect ratio.
max-width: 100%; //Keep the image width the same as it's parents container.
}
Your code is working well.
I've tried same code here. Please check.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<a class="navbar-brand" href="#"><img alt="Brand" src="http://img.logospectrum.com/dec/dummy-logo.jpg" class="img-responsive" /></a>

HTML: how to upscale an image with <img> and keep the aspect ratio?

Currently I am using img with no other constraint than max-width and max-height and I like the fact that when I use a bigger image my browser will downscale the image to fit it completely in its area (button, div, whatever) while keeping the image aspect ratio.
But for the case where the image is smaller I would like the browser to upscale the image (so that it takes the whole width OR height) while keeping the aspect ratio.
I tried playing with width:100% or/and height:100% but of course these will stretch the image, which is not what I want.
Here is an example.
The first image is big and will downscale properly.
The second image is small and doesn't upscale... I don't know how to upscale it properly.
That is my question!
I would like a pure CSS solution, no javascript if possible.
Note: I dont know what the image exact size is and I want to downscale if its too big and upscale if it's too small.
<!DOCTYPE html>
<html>
<body>
<div>
<button type="button" style="width:256px; height:256px">
<img src="http://www.google.com/doodle4google/images/d4g_logo_global.jpg" alt="Smiley face" style="max-width:100%; max-height:100%">
</button>
<button type="button" style="width:256px; height:256px">
<img src="http://static.autodesk.net/etc/designs/v2171/autodesk/adsk-design/images/autodesk_header_logo_140x23.png" style="max-width:100%; max-height:100%">
</button>
</div>
</body>
</html>
No need to do max-width and max-height Just use width:100%; instead.
The image will automatically maintain it's aspect ratio if you only define the width.
Here is a fiddle showing it in action: http://jsfiddle.net/Ea26r/
HTML:
<div>
<button type="button">
<img src="..." alt="Smiley face">
</button>
<button type="button">
<img src="..." >
</button>
</div>
CSS:
img {
width: 100%;
}
EDIT
Ok, so after some back and forth in the comments I think I have a better grasp of what you're looking for. Rather than using <img> tags inside the buttons, we can just use CSS to apply a background-image to the button then use background-size: contain; to scale the photo regardless of it's orientation. Something like this:
HTML
<button id="button1" type="button" style="width:256px; height:32px">
</button>
CSS
#button1 {
background-image: url('http://www.url-to-image-here.com');
background-size: contain;
background-repeat: no-repeat;
}
Now, keep in mind that IE8 and below do not support background-size:contain; so this solution is not perfect, but if you are not concerned about support for older browsers, then this should get you what you need. Here is a fiddle: http://jsfiddle.net/68VRk/

HTML/CSS image overlay

Implementing a "play video" function on a web site. Each video content item can have a different image. Each of these images will have the same width, but potentially differing heights (they are resized on upload to maintain aspect ratio to meet standard width requirements).
The plan was to display another transparent "play button" image over top of the content image using markup like this:
<div class="media">
<a class="videoLink" href="#" style="background-image: url(http://cloud.github.com/downloads/malsup/cycle/beach2.jpg);" >
<img src="PlayButton.png" alt="Click to Play" height="200" width="300" />
</a>
</div>
This is very similar to how channel 9 does it on their home page. This, however, appears to assume any image is of standard height and width. Are there alternative ways of tackling this?
Forgot to mention originally. We have a predefined width that things will fit into, however, each image may have a different height. For example, the same markup needs to be used to support the following images:
W x H
400 x 200
400 X 300
400 X 400
The Play button needs to be centered in each image.
Instead of the inner element being an <img>, you could make it a <div>, styled with the playbutton as the background image, positioned in the center.
<div class="media">
<a class="videoLink" href="#" style="background-image: url(http://cloud.github.com/downloads/malsup/cycle/beach2.jpg);" >
<div style='background:url(PlayButton.png) center center;' alt="Click to Play" height="200" width="300" />
</a>
</div>
You'll still need to know the size of the thumbnail image, as you'll still need to supply height and width for the div - since you're displaying the thumbnail as a background image, you won't be able to have the box scale to the right size automatically. But at least now your code can set the values for height and width without worrying about the shape of the play button getting distorted.
(note: the play button as a background image should probably be in a separate stylesheet rather than being declared inline as per my example; I did it like that to demonstrate how it differs from your original code, rather than to show best practice)
Need some your CSS to make sure things work, but this may help you:
.media {
display: table;
}
.media img {
display: table-cell;
vertical-align: middle;
display: block;
margin: 0 auto;
}
If not, please add you CSS so I can Fiddle it and make it happen.
I'd do it like this.
<div class="media">
<a class="videoLink" href="#"></a>
<img class="thumbnail" src="http://cloud.github.com/downloads/malsup/cycle/beach2.jpg"/>
</div>
Separate the thumbnail image from the link. We want the link to appear on top of the image, and the image to stretch the height of the <div class="media">.
The CSS:
.media {
position: relative;
}
.videoLink {
display: block;
height: 100%;
width: 100%;
background-image: url(PlayButton.png);
background-position: center center;
position: absolute;
z-index: 2;
}