Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image in full screen with img tag

Tags:

I am using the img tag for my slider images. I need the image to be full width and height and centered inside its container.

My problem is, when I resize the window width, my image becomes small and its height doesn't follow the window height. I need the same behaviour as background-size: cover but with the image tag.

background: url(../images/slider/002.jpg) center center; background-size: cover; 

If I make the image a background, everything works fine, but the slider will not. I need to use the <img> tag. Here is my example.

like image 392
Anju Aravind Avatar asked Jul 09 '14 09:07

Anju Aravind


People also ask

How do I make an image full screen?

Press "F11" to view the photo in fullscreen.

How do I put a background image on a whole page in HTML?

We'll use the html element (better than body as it's always at least the height of the browser window). We set a fixed and centered background on it, then adjust it's size using background-size set to the cover keyword.

How do I make an image fit the screen in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.


1 Answers

There are several ways to make an image cover a div with the image tag, the simplest is to use the object-fit property like this :

html,body{     margin:0;     height:100%; } img{   display:block;   width:100%;    height:100%;   object-fit: cover; }
<img src="http://i.imgur.com/5NK0H1e.jpg" alt="">

The browser support is good for object-fit (see canIuse) but if you need to support more browsers (like IE), you can use this technique to center an image fullscreen image with the <img> tag :

  • vertical and horizontal centering with absolute positioning, negative top, bottom, left and right values combined with margin:auto;
  • image always covers viewport with 100% min-height and min-width

DEMO :

html,body{     margin:0;     height:100%;     overflow:hidden; } img{     min-height:100%;     min-width:100%;     height:auto;     width:auto;     position:absolute;     top:-100%; bottom:-100%;     left:-100%; right:-100%;     margin:auto; }
<img src="http://i.imgur.com/5NK0H1e.jpg" alt="">
like image 196
web-tiki Avatar answered Sep 30 '22 19:09

web-tiki