Auto Image Resize Based on Browser Window Dimensions

How to resize an image to fit in the browser window?

Update 2018-04-11

Here's a Javascript-less, CSS-only solution. The image will dynamically be centered and resized to fit the window.

<html>
<head>
<style>
* {
margin: 0;
padding: 0;
}
.imgbox {
display: grid;
height: 100%;
}
.center-fit {
max-width: 100%;
max-height: 100vh;
margin: auto;
}
</style>
</head>
<body>
<div class="imgbox">
<img class="center-fit" src='pic.png'>
</div>
</body>
</html>

The [other, old] solution, using JQuery, sets the height of the image container (body in the example below) so that the max-height property on the image works as expected. The image will also automatically resize when the client window is resized.

<!DOCTYPE html>
<html>
<head>
<style>
* {
padding: 0;
margin: 0;
}
.fit { /* set relative picture size */
max-width: 100%;
max-height: 100%;
}
.center {
display: block;
margin: auto;
}
</style>
</head>
<body>

<img class="center fit" src="pic.jpg" >

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" language="JavaScript">
function set_body_height() { // set body height = window height
$('body').height($(window).height());
}
$(document).ready(function() {
$(window).bind('resize', set_body_height);
set_body_height();
});
</script>

</body>
</html>

Note: User gutierrezalex packaged a very similar solution as a JQuery plugin on this page.

Automatically resize images with browser size using CSS

To make the images flexible, simply add max-width:100% and
height:auto. Image max-width:100% and height:auto works in IE7,
but not in IE8 (yes, another weird IE bug). To fix this, you need to
add width:auto\9 for IE8.

source:
http://webdesignerwall.com/tutorials/responsive-design-with-css3-media-queries

for example :

img {
max-width: 100%;
height: auto;
width: auto\9; /* ie8 */
}

and then any images you add simply using the img tag will be flexible

JSFiddle example here. No JavaScript required. Works in latest versions of Chrome, Firefox and IE (which is all I've tested).

How to make an image auto resize to fit browser window with relation to height

It is not clear what you tried so far and we don't see any code. I'll try to answer anyway and perhaps it'll help you.

First of all, when you working with responsive layouts, always try to size your elements with viewport height and width. This helps you keep your content relative to the browser size - no matter if resized nor how large the screen is.

This code might help you insert a responsive image to your site:

div {
width:80vw;
height:80vh;
}
img {
max-width:100%;
height:auto;
max-height:100%;
}

Working example here

In this example is div sized 80% of both window's height and width, so it never exceeds the viewport. Max. measures of img are 100% of the div and height:auto; secures that it preserves its aspect ratio. Image then fits a div to the max allowed. You can comfortably center the image by setting display:table-cell; vertical-align:middle; text-align:center; to the DIV.

Another solution would be:

 background-image:url(' ');
background-size:contain;
background-repeat:no-repeat;
background-position:center;

Check it out here

Fluidly resizing all images based on browser window size

Try to use a set of media queries and do the math, see if it helps. I use the font-size: 0; trick on the container to remove the the white space between the inline-blocks.

https://jsfiddle.net/Lnxd0yra/2/

body {  margin: 0;}.container {  font-size: 0;}.item {  font-size: 16px;  display: inline-block;  width: 312px;}.item img {  width: 100%;  height: auto;  display: block;}@media (max-width: 1248px) { /*312x4*/  .item {    width: 25%;  }}@media (max-width: 936px) { /*312x3*/  .item {    width: 33.3333%;  }}@media (max-width: 624px) { /*312x2*/  .item {    width: 50%;  }}@media (max-width: 312px) { /*312x1*/  .item {    width: 100%;  }}
<div class="container">  <div class="item"><img src="https://unsplash.it/200"></div>  <div class="item"><img src="https://unsplash.it/200"></div>  <div class="item"><img src="https://unsplash.it/200"></div>  <div class="item"><img src="https://unsplash.it/200"></div>  <div class="item"><img src="https://unsplash.it/200"></div>  <div class="item"><img src="https://unsplash.it/200"></div></div>

Resizing an image based on browser size

There is no height attribute for divs recognized by the browser so

$(".widescreen").attr("height", "480px");

is adding height="480px" tothe code but that doesn't tell it to render differently.

I think you want

$(".widescreen").css("height", "480px");


Related Topics



Leave a reply



Submit