Stretch An Image To Fill Width Of Browser Window - Html - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Stretch an image to fill width of browser window Ask Question Asked 12 years, 9 months ago Modified 2 years, 10 months ago Viewed 77k times 20

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?

Share Improve this question Follow edited May 15, 2015 at 13:06 Eric Leschinski's user avatar Eric Leschinski 154k96 gold badges421 silver badges335 bronze badges asked Mar 16, 2012 at 2:50 Billjk's user avatar BilljkBilljk 10.7k23 gold badges55 silver badges73 bronze badges 1
  • possible duplicate of CSS or jQuery scalable background image with NO white space underneath when img height is less than window height – Joseph Commented Mar 16, 2012 at 2:54
Add a comment |

5 Answers 5

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 18

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

Share Improve this answer Follow edited Feb 19, 2022 at 20:38 answered Mar 16, 2012 at 2:57 sbagdat's user avatar sbagdatsbagdat 85011 silver badges25 bronze badges Add a comment | 8 <img src="filename.jpg" alt="alt text" width="100%" />

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

Share Improve this answer Follow answered Mar 16, 2012 at 2:53 Brian Willis's user avatar Brian WillisBrian Willis 23.8k9 gold badges48 silver badges50 bronze badges 4
  • 1 this is not valid for HTML 5. In HTML5, value must be in pixels. – sbagdat Commented Mar 16, 2012 at 3:03
  • @sbagdat Interesting. I didn't know that. Any idea why percent was removed as an option? – Brian Willis Commented Mar 16, 2012 at 3:06
  • probably when images are getting bigger, they are losing quality. here is the w3c link: w3schools.com/html5/att_img_width.asp – sbagdat Commented Mar 16, 2012 at 3:11
  • svg (vectorial) images don't lose quality. It works for me in browsers/devices I tried – dstonek Commented Jul 10, 2018 at 20:53
Add a comment | 2 <div class="fixpixel"> <img src="ImagePath" /> </div>​​​​​​​​​​

CSS file

.fixpixel img{ height: auto; background-position: center; background-repeat: no-repeat; background-size: cover; } Share Improve this answer Follow edited Sep 1, 2017 at 9:32 answered Sep 1, 2017 at 9:22 Sabarish R's user avatar Sabarish RSabarish R 812 silver badges7 bronze badges Add a comment | 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.

Share Improve this answer Follow edited Sep 9, 2019 at 4:32 tituszban's user avatar tituszban 5,1522 gold badges22 silver badges32 bronze badges answered Sep 8, 2019 at 21:38 DianaHeit's user avatar DianaHeitDianaHeit 313 bronze badges Add a comment | 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.

Share Improve this answer Follow answered Sep 22, 2018 at 12:58 SLop's user avatar SLopSLop 316 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • How AI apps are like Google Search
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries

Linked

37 Why is there a default margin on the <body> element? 5 CSS or jQuery scalable background image with NO white space underneath when img height is less than window height -6 How to make image fit to window width 1 How do you stretch an image with CSS? 4 Stretching image 2 stretch image to 100% of viewport 0 Cant get image to stretch full width 0 Can't seem to stretch an image to 100% of the webpage 3 css - how to stretch a background image across the entire window 0 How to have div fill width and height of browser window and not have the image be stretched 1 How to stretch image to fill div using JavaScript 2 How to stretch a background-image to 100% width 0 How to stretch the image responsive to fill entire screen

Hot Network Questions

  • Does theory ladenness mean I have to throw out science ... and my senses?
  • tabularray error
  • Why did Herod want to know the time of appearance of the Star of Bethlehem?
  • How does FM preemphasis interact with maximum deviation?
  • QGIS labeling: Why do we need a primary key for the auxiliary storage?
  • Why don't bicycles have the rear sprocket OUTSIDE of the frame spacing? (Single speed)
  • Is a rational decision (or claim) incompatible with determinism?
  • If God is good, why does "Acts of God" refer to bad things?
  • Are there any languages without adpositions?
  • What HDD copy strategy is best between manually syncing and rewriting from scratch?
  • Image of the entire function
  • What Battery Powered Part Is This?
  • \MakeLowercase in \section
  • What is the best way to prevent this ground rod from being a trip hazard
  • Tikz vphantom/hphantom for whole node/scope
  • Why does the engine prefer a5 (pass pawn) over axb5 (pass pawn+win a pawn)?
  • Initialize std::map
  • Cannot solve this naive exponential equation with base coefficient > 1
  • 3 phase asynchronous motors in tandem
  • How to Modify 7447 IC Output to Improve 6 and 9 Display on a 7-Segment
  • Escape braces in C# interpolated raw string literal
  • How to swim while carrying fins (i.e., when the fins aren't positioned on my feet)?
  • Hardy's ratings of mathematicians
  • Should I include my legal name on my CV if my preferred name is not reflected on my diplomas? -- Humanities
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-html

Từ khóa » Html Image Fill Page