Full full screen ratio

How can I display an image that fills the entire screen (HTML or Javascript) but doesn't stretch or distort, it maintains aspect ratio.

So it will be centered and either:

You have 100% height and width x% (where "x" is less than or equal to 100%)

or

Are y% high and 100% wide (where "y" is less than or equal to 100%)

For something like a full screen web image viewer.

+3


source to share


3 answers


Use this:

body, html {
    height: 100%;
    margin: 0;
    padding: 0;
}
div {  
    height: 100%;
}

img {
max-width: 100%;
max-height: 100%;
}

      



JSFIDDLE HERE

+1


source


Just set one of width or height, and the other will be set automatically according to the aspect ratio of the image.



+1


source


html {
  margin: 0;
  padding: 0;
}
.container {
  max-width: 100%;
}
.img-class {
  max-width: 100%;
  height: auto;
  position: relative;
}
      

Run codeHide result


You can set the container div to be exactly positioned on the screen, using absolute position if necessary.

+1


source







All Articles