How Do I Stretch A Background Image To Cover The Entire HTML ...

    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 How do I stretch a background image to cover the entire HTML element? Ask Question Asked 16 years, 1 month ago Modified 1 year, 1 month ago Viewed 413k times 103

I'm trying to get a background image of a HTML element (body, div, etc.) to stretch its entire width and height.

Not having much luck. Is it even possible or do I have to do it some other way besides it being a background image?

My current css is:

body { background-position: left top; background-image: url(_images/home.jpg); background-repeat: no-repeat; }

Edit: I'm not keen on maintaining the CSS in Gabriel's suggestion so I'm changing the layout of the page instead. But that seems like the best answer so I'm marking it as such.

Share Improve this question Follow edited Dec 18, 2022 at 23:04 starball's user avatar starball 47.2k28 gold badges175 silver badges825 bronze badges asked Oct 25, 2008 at 3:41 Fung's user avatar FungFung 7,7308 gold badges55 silver badges69 bronze badges 2
  • Of course, you could use JavaScript to do this dynamically, but that would be probably worse than the CSS hacks suggested by gabriel1836's link. – Jason Bunting Commented Oct 25, 2008 at 4:42
  • To preserve the aspect ratio of the image you should use "background-size: cover;" or "background-size: contain;". I've built a polyfill that implements those values in IE8: github.com/louisremi/background-size-polyfill – Louis-Rémi Commented Dec 6, 2012 at 14:32
Add a comment |

14 Answers 14

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 217 <style> { margin: 0; padding: 0; } html { background: url('images/yourimage.jpg') no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } </style> Share Improve this answer Follow edited Mar 17, 2017 at 9:55 Hakan Fıstık's user avatar Hakan Fıstık 19.2k16 gold badges119 silver badges144 bronze badges answered Mar 16, 2011 at 20:21 Nathan's user avatar NathanNathan 2,1941 gold badge13 silver badges2 bronze badges 12
  • This also works as style tags within html elements or in css files. – Nathan Commented Mar 16, 2011 at 20:29
  • 3 this is the best solution, ever. – Andrei Drynov Commented Apr 20, 2012 at 10:13
  • 1 What's the difference between cover vs 100% 100% ? – Pacerier Commented May 22, 2012 at 14:13
  • 4 100% 100% does not keep the aspect ratio of the original image. ‘cover’ scales the image, while preserving its intrinsic aspect ratio (if any), to the smallest size such that both its width and its height can completely cover the background positioning area. You can also use ‘contain’ to scale the image, while preserving its intrinsic aspect ratio (if any), to the largest size such that both its width and its height can fit inside the background positioning area. – Mike737 Commented Jun 22, 2012 at 4:11
  • "-webkit-background-size: cover;" is not a valid CSS3 property. – VoidKing Commented Apr 23, 2013 at 15:55
| Show 7 more comments 16

Use the background-size property: http://www.w3.org/TR/css3-background/#the-background-size

Share Improve this answer Follow edited Jan 31, 2012 at 11:32 answered Jul 9, 2009 at 12:52 Kornel's user avatar KornelKornel 99.9k38 gold badges232 silver badges317 bronze badges 1
  • 1 Awesome, this is the correct answer. It probably doesn't work on IE, but I'm only targeting webkit for my project, so this is perfect :) – aehlke Commented Mar 24, 2011 at 2:37
Add a comment | 9

In short you can try this....

<div data-role="page" style="background:url('backgrnd.png'); background-repeat: no-repeat; background-size: 100% 100%;" >

Where I have used few css and js...

<link rel="stylesheet" href="css/jquery.mobile-1.0.1.min.css" /> <script src="js/jquery-1.7.1.min.js"></script> <script src="js/jquery.mobile-1.0.1.min.js"></script>

And it is working fine for me.

Share Improve this answer Follow answered May 7, 2013 at 10:44 Tamal Samui's user avatar Tamal SamuiTamal Samui 7381 gold badge10 silver badges14 bronze badges 1
  • For those who do NOT want to keep the aspect ratio! – BillyNair Commented Feb 23, 2016 at 15:52
Add a comment | 6

Not sure that stretching a background image is possible. If you find that it's not possible, or not reliable in all of your target browsers, you could try using a stretched img tag with z-index set lower, and position set to absolute so that other content appears on top of it.

Let us know what you end up doing.

Edit: What I suggested is basically what's in gabriel's link. So try that :)

Share Improve this answer Follow answered Oct 25, 2008 at 3:50 Travis Collins's user avatar Travis CollinsTravis Collins 4,0224 gold badges33 silver badges45 bronze badges 1
  • 1 I saw this technique in action at least a decade ago. I can't imagine it wouldn't still work now. – eyelidlessness Commented Oct 25, 2008 at 3:51
Add a comment | 6

To expand on @PhiLho answer, you can center a very large image (or any size image) on a page with:

{ background-image: url(_images/home.jpg); background-repeat:no-repeat; background-position:center; }

Or you could use a smaller image with a background color that matches the background of the image (if it is a solid color). This may or may not suit your purposes.

{ background-color: green; background-image: url(_images/home.jpg); background-repeat:no-repeat; background-position:center; } Share Improve this answer Follow answered Oct 27, 2008 at 19:09 Traingamer's user avatar TraingamerTraingamer 1,4238 silver badges9 bronze badges Add a comment | 5

If you need to stretch your background image while resizing the screen and you don't need compatibility with older browser versions this will do the work:

body { background-image: url('../images/image.jpg'); background-repeat: no-repeat; background-size: cover; } Share Improve this answer Follow answered Dec 22, 2016 at 16:48 leocborges's user avatar leocborgesleocborges 4,8375 gold badges35 silver badges39 bronze badges Add a comment | 3

If you have a large landscape image, this example here resizes the background in portrait mode, so that it displays on top, leaving blank on the bottom:

html, body { margin: 0; padding: 0; min-height: 100%; } body { background-image: url('myimage.jpg'); background-position-x: center; background-position-y: bottom; background-repeat: no-repeat; background-attachment: scroll; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } @media screen and (orientation:portrait) { body { background-position-y: top; -webkit-background-size: contain; -moz-background-size: contain; -o-background-size: contain; background-size: contain; } } Share Improve this answer Follow answered Jun 12, 2015 at 19:50 live-love's user avatar live-lovelive-love 52.1k26 gold badges250 silver badges214 bronze badges Add a comment | 3

The following code I use mostly for achieving the asked effect:

body { background-image: url('../images/bg.jpg'); background-repeat: no-repeat; background-size: 100%; } Share Improve this answer Follow edited Jul 15, 2015 at 22:27 Enamul Hassan's user avatar Enamul Hassan 5,44523 gold badges43 silver badges58 bronze badges answered Jul 9, 2015 at 14:33 Badar's user avatar BadarBadar 1,4601 gold badge15 silver badges20 bronze badges 2
  • 4 You need background-size: 100% 100%; – Sablefoste Commented Aug 10, 2015 at 20:09
  • I don't know what browser you use, But what I have told works for me in Firefox and Chrome. – Badar Commented Sep 2, 2015 at 18:23
Add a comment | 2 background: url(images/bg.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; Share Improve this answer Follow answered Mar 4, 2014 at 8:46 Behnam's user avatar BehnamBehnam 6,4391 gold badge43 silver badges36 bronze badges Add a comment | 2

It works for me

.page-bg { background: url("res://background"); background-position: center center; background-repeat: no-repeat; background-size: 100% 100%; } Share Improve this answer Follow answered May 28, 2020 at 16:21 msk_sureshkumar's user avatar msk_sureshkumarmsk_sureshkumar 4435 silver badges10 bronze badges Add a comment | 1

Here is an example from my code:

.content{ background-image: url("SOMEURL"); background-position:center; background-size: cover; text-align: center; /* Center-aligns text horizontally */ justify-content: center; /* Center content horizontally */ }

Share Improve this answer Follow answered Oct 14, 2023 at 22:17 Metehan Akça's user avatar Metehan AkçaMetehan Akça 111 bronze badge 1
  • 1 Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Bot Commented Oct 17, 2023 at 11:12
Add a comment | 0

You cannot in pure CSS. Having an image covering the whole page behind all other components is probably your best bet (looks like that's the solution given above). Anyway, chances are it will look awful anyway. I would try either an image big enough to cover most screen resolutions (say up to 1600x1200, above it is scarcer), to limit the width of the page, or just to use an image that tile.

Share Improve this answer Follow answered Oct 25, 2008 at 14:15 PhiLho's user avatar PhiLhoPhiLho 41.1k6 gold badges99 silver badges136 bronze badges Add a comment | 0

image{

background-size: cover; background-repeat: no-repeat; background-position: center; padding: 0 3em 0 3em; margin: -1.5em -0.5em -0.5em -1em; width: absolute; max-width: 100%; Share Improve this answer Follow answered Feb 21, 2020 at 23:19 Ebele Nwaelene's user avatar Ebele NwaeleneEbele Nwaelene 1 1
  • 2 Welcome to SO! To write a better answer, add some reasoning as to why it works and show the code in action. You can also read about writing a good answer. – displacedtexan Commented Feb 21, 2020 at 23:25
Add a comment | 0

Simply make a div to be the direct child of body (with the class name bg for example), encompassing all other elements in the body, and add this to the CSS file:

.bg { background-image: url('_images/home.jpg');//Put your appropriate image URL here background-size: 100% 100%; //You need to put 100% twice here to stretch width and height }

Refer to this link: https://www.w3schools.com/css/css_rwd_images.asp Scroll down to the part that says:

  1. If the background-size property is set to "100% 100%", the background image will stretch to cover the entire content area

There it shows the 'img_flowers.jpg' stretching to the size of the screen or browser regardless of how you resize it.

Share Improve this answer Follow answered Apr 28, 2020 at 22:16 Salah Ayman's user avatar Salah AymanSalah Ayman 32 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
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

3 Background image at 100% of window (and resizes with it) 2 Jasper to html background image stretch 0 CSS3 - Multiple backgrounds that covering the whole site 0 HTML: background-image not displaying 0 How to extend CSS style in a HTML so it appears throughout the whole page when viewed in broswer? -2 How to have a backround image on HTML 192 Stretch background image css? 2 stretching background image 4 Have background-image "stretch to fit" the background of my page 140 How to stretch the background image to fill a div 0 Stretch background image to page 3 css - how to stretch a background image across the entire window 5 CSS stretch or fit background image 1 Stretch a part of background image 1 How To Make a Background Image not Stretch in HTML and show as a whole? 2 How to stretch a background-image to 100% width

Hot Network Questions

  • Why don't routers answer ARP requests for IP addresses they can handle even if they aren't assigned that IP address themselves?
  • why can I not fund my regtest named wallet?
  • Are periodic functions such as sine and cosine defined on surreal numbers?
  • Nomenclature for Swap Spreads
  • What is the Calvinist/Reformed solution to the Problem of Hell?
  • Condition IF in Tikz
  • Why「记」for shop names?
  • Strange Brackets in String Writing
  • Solana Playground - Mainnet-Beta
  • Do referees get to see each other's reports?
  • How to avoid wasting reader investment with a thematically disappointing ending?
  • How SMA (or RF) connector's impedance is measured and rated?
  • What should I do with a package that is delivered to my address but the name is wrong?
  • Commencement and repeal dates in UK Acts of Parliament - meaning of "From and after"
  • Can a contradiction exist in the antecedent of a sequent?
  • Was it really possible to damage my VGA card by programming it in assembly through its latches registers?
  • Why did General Groves mention the baby delivery count here?
  • Is every automorphism of a cone diagonalisable?
  • Quantitative definition of information without set cardinality or probability?
  • Does a touch spell have advantage when delivered by an invisible Familiar?
  • Are Quantum Algorithms better than classical algorithms for all problems?
  • I want to align Mathematics and class X vertically
  • How many colors do we need?
  • Why would you not issue orders to a facility?
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 Body Background Image Stretch To Fit