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
. Imagemax-width:100%
andheight:auto
works in IE7,
but not in IE8 (yes, another weird IE bug). To fix this, you need to
addwidth: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
Infinite Scrolling with React Js
Angularjs - Placeholder for Empty Result from Filter
What Cross-Browser Issues Have You Faced
Bootstrap 3: How to Make Head of Dropdown Link Clickable in Navbar
How to Do Anything About "Repaints on Scroll" Warning in Chrome for "Overflow:Scroll" Div
Css3 Keyframe Animations: End and Stay on the Last Frame
Chrome Extension Inject Sidebar into Page
Bootstrap 3 Accordion Collapse Does Not Work on Iphone
How to Set a JavaScript Onclick Event to a Class with CSS
Prevent Select2 from Flipping the Dropdown Upward
JavaScript - Smooth Parallax Scrolling with Mouse Wheel
Can JavaScript Change the Value of @Page CSS
Fast Image Loading Methods, Low to High Res with Multiple Backgrounds - JavaScript Solution
JavaScript Interface Not Working with Android 4.2
Automatically Resize Text Area Based on Content
Setting a Whole Style String to an Element from JavaScript (Not Individual Style Parameters)