Image with max-width, and set height - html

I have a lightbox of photos and each photo is assigned a height of 85% of the viewport. When the window width is too small, the image is cut off, so they have a max-width of 100%;
.modal-image {
height: 85vh;
max-width: 90%;
}
However, when the max-width kicks in, the image is distorted because the height remains 85vh while the width changes with the screen width.
How could I keep the images at height 85vh except for when the max-width kicks in, at which point the height should be dynamic.
Thanks for the help

You might have a chance using a container for your image, so you could set the max-width in the container and the height on the image like so:
div {
max-width: 90%;
}
img {
height: 85vh;
object-fit: contain;
}
<div> <img src="https://pixar-community-production.s3-us-west-1.amazonaws.com/News/Effects_MTL_2018/rfm_rewrite.jpg" /> </div>
Using a container with a max-width allow us to have the right width, then using heighton the image allow the image to dynamically calculate its width without loosing its ratio.

You have to look in which screen resolution, the window "becomes" too small and create a css rule specific for every case. For example:
<style>
#media (max-width: 768px) {
.modal-image {
height: 85vh;
max-width: 90%;
}
}
#media (max-width: 1200px) {
.modal-image {
height: 85vh;
max-width: 90%;
}
}
</style>
Greetings!

Update: found an alternative solution
simply gave the image a container of width 85vh and height 85vh, and used object-fit: fill; so that the images scale properly.
<div class="container">
<img src="https://www.aussiespecialist.com/content/asp/en/sales-resources/image-and-video-galleries/jcr:content/mainParsys/hero/image.adapt.1663.medium.jpg" class="modal-image">
</div>
<style>
.container {
width: 100%;
height: 100vh;
padding: 100px;
object-fit: contain;
}
.modal-image {
width: 100%;
height: 100%;
}
</style>

Related

Automatically resize images (that have a predefined size) with browser size using CSS

In order to make images flexibles I can use the following css code:
img {
max-width: 100%;
height: auto;
width: auto\9;
}
Although, this doesnt let me to define a specific size for an image when the browser is on the full size.
Is there any way to do something like this :
img {
max-width: 100%;
height: 150px;
height: auto;
width: auto\9;
}
Maybe you want to use CSS Media Queries
img {
max-width: 100%;
height: auto;
width: auto\9;
}
#media (min-width: 1280px) {
img {
max-width: 100%;
height: 150px;
height: auto;
width: auto\9;
}
}
By setting min-width and/or max-width you can have special rules applied only with those window sized.
What you are trying to achieve for flexible width and height can be achived using viewport height and viewport width unit of css.
Instead of using '%' or 'px' as units to your width and height, try 'vh' fir height and 'vw' for width. For an example a 50vh height is roughly 50% of browser height, so it is highly flexible with browser changes.
img { max-width: 100%; max-height: 150px; height: 70vh; width: 60vw; }
do you already tried to use object-fit?
Is a good alternative to fix image sizes, just passing:
img {
object-fit: cover;
width: 100%;
height: 100%;
}

how to allow image width to exceed the width of the div that it is in

I have a div that is 1140px wide and am image sitting within it that I need to fit the screen size, Is it possible to make the image width size larger than the div width that it is sitting in?
<div className="row" style="width: 1140px; margin-left: auto; margin-right: auto">
<img className="aimg" src="~/images/background.gif" style="height: 325px" />
</div>
CSS as follows:
.aimg {
padding: 0;
display: block;
margin: 0 auto;
height: auto;
width: 100%;
}
As it stands, the div width is limiting the image from fitting the screen width
set position: absolute; width: 100vw; height: 100vh; to you img styles and it should work :)
"vw" stands for viewWidth and "vh", accordingly, for viewHeight.
https://www.w3schools.com/cssref/css_units.asp
Below codepen is based on:
https://css-tricks.com/full-width-containers-limited-width-parents/
Codepen itself:
https://codepen.io/anon/pen/vzawqv
you need css classes for container and image, the following example suppose to the div width is fixed width and set with 480px.
.div_image {
max-height:480px;
height: 480px;
}
.div_image img{
height: 100%;
width: 100%;
}

How can I resize all photos to the same size?

I have this site:link
but i have the following problem:problem
What I want to do is:
1. the two images must have a width of 33.3333% , but the middle image must
remain as it is
another of my problems is when I resize the browser,the middle image is much higher.
CSS
.inline-block{
display: inline-block;
vertical-align: top;
}
.left-img,.right-img{
width: 33.3333%;
}
.left-img{
background: url("/wp-content/themes/wp_bagel/assets/img/img-01.png");
background-size: cover;
}
.ctr-img{
background: url("/wp-content/themes/wp_bagel/assets/img/img-02.png");
background-size: cover;
}
.right-img{
background: url("/wp-content/themes/wp_bagel/assets/img/img-03.png");
background-size: cover;
}
HTML
<div class="left-img inline-block">
<img src="/wp-content/themes/wp_bagel/assets/img/img-01.png" alt="Mountain View" style="visibility: hidden;">
</div>
<div class="ctr-img inline-block">
<img src="/wp-content/themes/wp_bagel/assets/img/img-02.png" alt="Mountain View" style="visibility: hidden;">
</div>
<div class="right-img inline-block">
<img src="/wp-content/themes/wp_bagel/assets/img/img-03.png" alt="Mountain View" style="visibility: hidden;">
</div>
This will not be possible with just HTML / CSS. You will have to use Javascript. For your layout, I would suggest taking a look at Masonry.
What I understand from your question is that you're trying to make all the three images of the same size, and this isn't happening when you're resizing the browser.
Well that, I think, is because you're specifying the width of left and the right images as 33% of screen and you're not setting the width of middle image. Try adding the ctr-image class also to that CSS block. i.e.,
.left-img,.ctr-img,.right-img{
width: 33.3333%;
}
instead of just
.left-img,.right-img{
width: 33.3333%;
}
click here to website
https://www.sitepoint.com/web-foundations/resize-images-css/
you get an idea
use like that:
#media screen and (orientation: portrait) {
img.ri { max-width: 90%; }
}
#media screen and (orientation: landscape) {
img.ri { max-height: 90%; }
}
I suggest you to use framework with grid system (Bootstrap par exemple). With three images, you need to set .col-md-4, .col-sm-4, .col-xs-4 to each image and then you have a perfect display.
To make all three images have the same height, if u want, u can fix the height. If not, the following solution works well:
.image-container {
position: relative;
padding: 0 0 56.25% 0;
}
.image-container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Change 56.25% to the value that is best for you and then you will have what you want.
This is an exemple with your images: Codepen
You have a { max-width: 100%; } in your main.css file, remove it, and add {width: 100%}...
img {
border: 0;
max-width: 100%; /* Remove this line */
width: 100%; /* Add this line */
height: auto;
}
Then, You must make all 3 images in same height! Resize your images to same height... for example if your minimum height is 177px (first image), the second image must resize to (184x177) and the last image must be (235x177).
Finally, add { width: 33%; } for the center image-div
.ctr-img {
width: 33%;
}

Chrome with height 100% images randomly doesn't keep aspect ratio and stretches images.

the issue is easy to explain...
I have a container div with a 90% height and a contained image with height:100% to fill its container.
Here is the simple css:
.carousel-item .img {
height: 90%;
text-align: center;
}
.carousel-item .img img {
height: 100%;
max-height: 384px;
max-width: 497px;
}
The problem is that just Chrome randomly (but it happens the most of the time) stretches the width image like I set width property to 100% but I didn't because I want to keep its aspect ratio.
I was wondering if it's a Chrome bug, or there is a workaround for that.
I link two screenshots:
http://oi61.tinypic.com/ehxlbm.jpg <--- as is.
http://oi62.tinypic.com/2qcl8gl.jpg <--- to be.
Thanks for helping.
.carousel-item .img img {
height: 100%;
width: auto;
max-height: 100%;
max-width: 497px;
}

Responsive banner image

I am developing a responsive website which has a large banner image which spans edge to edge. The problem is that its supposed to be a static height regardless of the screen size. So when I set the image to width:100% it looks great in full size, but as soon as I start to shrink the screen the image gets shorter then the width that it needs to be. How can I set this up so that the image height appears to never change and when the screen size is smaller then the image size it zooms in and crops the edges?
http://bit.ly/1qxmZir
HTML
<div id="hd-img">
<img src="http://72.52.242.20/~camacoll/wp-content/uploads/2014/05/inner-hd.jpg">
</div>
CSS
#hd-img {
max-height: 352px;
}
img {
width: 100%;
height: auto;
left: 100%;
margin-left: -200%;
position: relative;
}
One way you could do it is like this.
/* CSS */
#hd-img {
height: auto;
overflow: hidden;
}
#hd-img img {
/* Optional max width: 600px; */
display: block;
margin: 0 auto;
}
Try this:
background-image:url(../images/header-bannerbg.png);background-position:fixed;background-repeat:no-repeat;height:580px;-webkit-background-size: 100% 100%;-moz-background-size: 100% 100%;-o-background-size: 100% 100%;background-size: 100% 100%;