Stretch Image to Fit 100% of Div Height and Width - html

I have a div with the following CSS
#mydiv{
top: 50px;
left: 50px;
width: 200px;
height: 200px;
}
and my HTML looks like this
<div id = "mydiv">
<img src = "folder/file.jpg" width = "200px" height = "200px">
</div>
I'd like my web image to always be the same size (in a 1:1 aspect ration) no matter what the resolution of the actual image is. If my actual image files are square (with 1:1 ratio) then this isn't a problem. But if the actual image files are not square then the displayed web image do stretch to 100% of both the div's height and width (in this case 200px).
How do I get different image sizes to fit to my DIV?

You're mixing notations. It should be:
<img src="folder/file.jpg" width="200" height="200">
(note, no px). Or:
<img src="folder/file.jpg" style="width: 200px; height: 200px;">
(using the style attribute) The style attribute could be replaced with the following CSS:
#mydiv img {
width: 200px;
height: 200px;
}
or
#mydiv img {
width: 100%;
height: 100%;
}

Instead of setting absolute widths and heights, you can use percentages:
#mydiv img {
height: 100%;
width: 100%;
}

Or you can put in the CSS,
<style>
div#img {
background-image: url(“file.png");
color:yellow (this part doesn't matter;
height:100%;
width:100%;
}
</style>

will the height attribute stretch the image beyond its native resolution? If I have a image with a height of say 420 pixels, I can't get css to stretch the image beyond the native resolution to fill the height of the viewport.
I am getting pretty close results with:
.rightdiv img {
max-width: 25vw;
min-height: 100vh;
}
the 100vh is getting pretty close, with just a few pixels left over at the bottom for some reason.

Related

How to crop image in HTML as a percentage of it's height?

In my HTML code, I have a PNG image that I resized using CSS:
.adjust-image {
border:1px solid #021a40;
display: block;
width: auto;
max-width: 100%;
max-height: 1000px;
}
.img-container {
position: relative;
}
<div class= "img-container">
<img class = "adjust-image noselect fade-out" src="{{ s.photo.url }}">
</div>
Using the image's current size (with max-height), how can I crop the image as a percentage, instead of using px for height?
For instance, how can I get CSS written so that I can just designate something like: height: 34%?
The height property can only use a percentage if its parent has a fixed height. What you can do however is place the image as a background image. Then you can set the height to 0, and as long as the width of the image is 100%, then you can use padding-bottom of a percentage since that references the width of its parent. Weird I know. So an example:
.img {
width: 100%;
height: 0;
padding-bottom: 34%;
background-size: cover;
}
Of course, this may not be the best semantically, but it is a classic way to use aspect ratio in CSS. And once aspect-ratio is sufficiently supported, that will be a great option.

How do I scale this image to fit my header?

My header only has a height of 108px
This is how big my header is
But the image/logo I want to place is clearly bigger
When I remove the height limit in my CSS, this is how it looks like
how do I rescale my image to fit my header? I'm new to html and css and trying to learn to code, thank you in advance!
this is my html and css
header.top {
background - color: #157bea;
margin: 10px,10px, 0px, 10px;
height: 108px;
}
img.logowhite {
width: 100%;
height: 100%;
object-fit: 100%;
}
<header class = top>
<div class = logowhite>
<img src = images/logowhite.png>
</div>
</header>
Set the width CSS attribute to auto value as follows and set the height with the px value.
On your CSS, your header has got the fixed height 108px so it will be fine to define the height in pixel.
And currently, img tag is inside .logwhite selector so it is needed to replace img.logowhite to .logowhite img.
.logowhite img {
width: auto;
height: 108px;
}

Image should be responsive and should be filled on the entire screen

I have 2 div's named first and second and I have set the width and height of them as 100%
.first{
width : 100%;
height : 100%;
}
.second{
width : 100%;
height : 100%;
}
now I would like to add an image in each div. These images should fill in the entire div.
<img src="someimage.png" width="100%" height="100%"/>
My problem is the image should not be stretched it should be filled the entire screen. I have used img img-responsive classes to achieve this. The image is now getting filled without stretching but when resized it is getting resized uniformly and the height of it is also getting decreased hence the image's height is now not getting filled 100%. Is there any way to achieve width and height of an image to cover the entire screen without stretching and decreasing the height?
Check this out, and you should use width: 100% beside min-height: 100% but i recommend you to use background-image with background-size: cover
.first{
width : 100%;
height : 100%;
}
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.first img {
width: 100%;
min-height: 100%;
}
<div class="first">
<img alt="" src="https://placeholdit.imgix.net/~text?txtsize=33&txt=500%C3%97500&w=500&h=500"/>
</div>
jsFiddle
I use imgLiquid this is a jQuery Plugin to resize images to fit in a container.
https://github.com/karacas/imgLiquid
It's super easy to use and light weight.

Make the img tag width and height 100% inside overflow hidden div while maintaining the aspect ratio

I need to make the img tag width and height 100% inside overflow hidden div while maintaining the aspect ratio.
What I reached for is putting the image within overflow hidden div And the image is max-width 100% and auto height.
<div id="foo">
<img src="http://www.engineering.com/Portals/0/BlogFiles/swertel/heart-cloud.jpg" />
</div>
but the problem i'm facing is not go height 100%
Look the code in action http://fiddle.jshell.net/TARwL/
And get close look at the div#cover is 100% width and height is perfect look and i would like to see my code do the same
I can't use the background-size:cover method because beside is not working in older browsers, I can't click right and save the image and this is important to me
I rethought and I found eligible solution for me, I don't know if will suit anyone else !!
The Image will be background size cover and at the same time I will add the image inside the same div with 100% width and height and 0 opacity
So the image will show like cover and anyone can click on the same area and use the image like normal (copy link, download, etc.)
HTML
<div style="background-image:url(http://www.engineering.com/Portals/0/BlogFiles/swertel/heart-cloud.jpg)">
<img src="http://www.engineering.com/Portals/0/BlogFiles/swertel/heart-cloud.jpg" />
</div>
CSS
div{
width: 300px;
height: 300px;
display:inline-block;
vertical-align: top;
background-size:cover;
background-position:50% 50%;
}
div img{
width: 100%;
height: 100%;
opacity: 0;
/* OLD IE */
zoom: 1;
filter: alpha(opacity=0);
}
Code In Action http://jsfiddle.net/Jim_Toth/mVtJc/1/
I think you'll have to use a Script for this one. (unless you want to use a centered background image)
Working Fidlle
[try it with any image you want, with different aspects ratios]
JQuery
var img = $("#foo > img");
var ratio = img.width() / img.height();
var limit = (100*ratio)+"%";
var margin = ((1-ratio)*50)+"%";
if( ratio > 1)
{
img.css({"width": limit, "margin-left": margin});
}
else
{
ratio = 1 / ratio;
img.css({"height": limit, "margin-top": margin});
}
Edit:
this Fiddle support multiple images at once (use the foo class)
Try this: (Note this will only work if you use images with the same aspect ratios)
#foo img {
width:133.33%;
margin-left: -16.66%; /* crop img to the left: 33.33 /2 = 16.66 */
}
FIDDLE
Explanation:
Your image is 1024px wide X 768px high. So width to height ratio= 1.333.
However your overflow:hidden div has a raio 1X1 - so the image will be distorted at 100%.
So in order to display the image to ratio - you need to increase the width by 133%.
Then, in order to center or 'crop' the image to fit the div - use margin.
Another proposal: jsFiddle
I really did not understand why it shouldn't be possible to use a background-iamge!?
So you can use the example code as long as the width and height of the containing div remains the same and also the aspect ration of the image stays at 4:3.
If any of the values changes you have to adapt at least the value for left (the calculation can easily be done with Javascript).
Not using a background-image make the whole thing very "fragile" ..., and from a semantically point of view it is also "not ideal".
Would be better to use a server side technique to crop the image to the desired/ needed size.
I think it should be like:
.image-container {
width: 169px; // May be auto
height: 169px;
position: relative;
overflow: hidden;
.image-wrap {
position: absolute;
left: 50%;
transform: translateX(-50%) translateY(0);
transition: all .2s ease-in-out; //Run on IE
height: 100%; // Height full frame
img.scale {
height: 100%;
max-height: 100%;
max-width: none !important; // To make sure that width of the image can be larger than its container.
}
}
}
HTML:
<div class="image-container">
<div class="image-wrap"><img class="scale" src="your image path" /></div>
</div>
In modern browsers it is possible to use the property object-fit: cover
<style>
#foo {
width: 200px;
height: 200px;
overflow: hidden;
}
#foo img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
<div id="foo">
<img src="https://picsum.photos/300/200" />
</div>
Example: https://jsfiddle.net/davox/z5a728jm/7/
Source:
https://developer.mozilla.org/en-US/docs/Learn/CSS/Howto/Fill_a_box_with_an_image

Is it possible to render an image in two sizes with correct proportion

I have a website on which the products have just one pic associated with them. The dimension of pictures are generally +200 X 200+. At one place, i want to show the image in 100X100 and at other in 75X75.
Is it possible to show the images properly in same proportion or not. Right now, the images look fatty or long or thin at some places.
If you set ONLY the height OR width of an image, the other dimension gets resized proportionally.
So, if your image container is let's say 100X100 px, you can style the image like this:
div.imageContainer100X100px img {
max-width: 100px;
max-height: 100px;
}
Or for 75X75 px:
div.imageContainer75X75px img {
max-width: 75px;
max-height: 75px;
}
I assume that you have used IMG tag to display the same image at different places in different sizes. To display image in correct proportion, set one of the dimensions i.e; width or height the other will be adjusted automatically: Example:
<img src="mypic.png" width="75px" />
In the above code the image tag will automatically adjust the height for correct proportion.
hope this helps
In my experience you have to set the image width to 100% of the parent to get consistent results with image resizing. Just setting the max-width doesn't guaranty that the image will fill the parent. This will.
.container {
overflow:hidden;
}
.container img {
width: 100%;
height: auto;
}
.container100 {
width: 100px;
height: 100px;
}
.container75 {
width: 75px;
height: 75px;
}
<div class="container container100">
<img src="path-to-img" alt="appropriate alt text">
</div>