19

I have an image, and I want the width to fill up the browser window, no matter the size of the window.

How do I do this in HTML and CSS?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Billjk
  • 10,387
  • 23
  • 54
  • 73
  • possible duplicate of [CSS or jQuery scalable background image with NO white space underneath when img height is less than window height](http://stackoverflow.com/questions/5072750/css-or-jquery-scalable-background-image-with-no-white-space-underneath-when-img) – Joseph Mar 16 '12 at 02:54

5 Answers5

17

You can add a div with width an height of 100%, also set image's width and height are 100%

<div id="wrapper">
    <img src="https://picsum.photos/200/200">
</div>​​​​​​​​​​

CSS:

#wrapper, img{
    width:100%;
    height: 100%;
}

jsfiddle

sbagdat
  • 830
  • 11
  • 25
8
<img src="filename.jpg" alt="alt text" width="100%" />

This assumes that the image's container is as wide as the browser window.

Brian Willis
  • 22,768
  • 9
  • 46
  • 50
2

You add the following <meta> element in the head of your HTML document:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Then you add

max-width:100%;
height:auto;

as a CSS rule for both the image and the container of the image. (It can also work for the image without having a container.)

And you add

body {
    margin: 0;
}

to get rid of the margin around the image and/or container. This is how your image will fill the whole width of the screen, no matter what size the screen is.

tituszban
  • 4,797
  • 2
  • 19
  • 30
DianaHeit
  • 31
  • 3
1
<div class="fixpixel">
    <img src="ImagePath" />
</div>​​​​​​​​​​

CSS file

.fixpixel img{
height: auto;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
 }
Sabarish R
  • 71
  • 1
  • 7
0

The margin of the body element is set to a default of 8px, which is the space you are seeing which prevent the image to stretch the full width and height of the screen.

You can include

body {margin: 0px;}

to prevent this behavior, which I guess was put in place to prevent unstyled pages to present the content pushing to the edge of the page. It was discussed here.

SLop
  • 31
  • 6