Fit Website Background Image To Screen Size - 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 Fit website background image to screen size Ask Question Asked 11 years ago Modified 1 year ago Viewed 442k times 35

I'm just starting on a website and I already encounter a small problem where I can't find a specific solution to.

I wanted to make my website background fit any screen size in width, height does not matter.

This is the link to my image:

../IMAGES/background.jpg

EDIT

I have no idea what's wrong, but for some reason the body doesn't even want to get the background image. It just displays a plain white background.

body {background-image:url(../IMAGES/background.jpg);} Share Improve this question Follow edited Aug 11, 2017 at 14:00 Vadim Kotov's user avatar Vadim Kotov 8,2548 gold badges50 silver badges63 bronze badges asked Nov 12, 2013 at 9:12 Tim's user avatar TimTim 4011 gold badge5 silver badges7 bronze badges 2
  • 3 There are so many question like yours. Please use the search function! And the image path is your local path. – Michael Schmidt Commented Nov 12, 2013 at 9:15
  • 1 possible duplicate of Stretch and scale a CSS image in the background - with CSS only – Oliver Matthews Commented Nov 12, 2013 at 9:15
Add a comment |

15 Answers 15

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

you can do this with this plugin http://dev.andreaseberhard.de/jquery/superbgimage/

or

background-image:url(../IMAGES/background.jpg); background-repeat:no-repeat; background-size:cover;

with no need the prefixes of browsers. it's all ready suporterd in both of browsers

Share Improve this answer Follow edited Oct 28, 2023 at 0:47 answered Nov 12, 2013 at 9:25 Victorino's user avatar VictorinoVictorino 1,64312 silver badges22 bronze badges 1
  • This solution doesn't work in case of mobile screens stackoverflow.com/questions/72852187/… – vikramvi Commented Jul 4, 2022 at 5:41
Add a comment | 28

Try this ,

background: url(../IMAGES/background.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;

For more information , follow this Perfect Full Page Background Image !!

Share Improve this answer Follow answered Nov 12, 2013 at 9:18 zey's user avatar zeyzey 6,09514 gold badges63 silver badges112 bronze badges 2
  • It's a little bit old thread, but It actually worked good for me, but be sure to place this CSS in the html {} and NOT in the body {}. – Marco Commented Oct 18, 2015 at 18:24
  • This is perfect for all kinds of devices. Keeping image center performs the magic – Yogi Commented Mar 10, 2017 at 7:51
Add a comment | 22

You can try with

.appBackground { position: relative; background-image: url(".../img/background.jpg"); background-repeat:no-repeat; background-size:100% 100vh; }

works for me :)

Share Improve this answer Follow answered Jun 20, 2016 at 9:52 Carlos Jaraiz's user avatar Carlos JaraizCarlos Jaraiz 2212 silver badges3 bronze badges 2
  • 2 This one is the only correct solution. The key to success is "100vh". – Farshad Mohajeri Commented Jun 21, 2018 at 7:42
  • finally an answer that doesn't just say "use background-size: cover", this works. – Stef Commented Nov 20, 2021 at 19:46
Add a comment | 5

Try this, I hope it will help:

position: fixed; top: 0; width: 100%; height: 100%; background-size: cover; background-image: url('background.jpg'); Share Improve this answer Follow answered Dec 5, 2018 at 18:54 Sara Popa's user avatar Sara PopaSara Popa 1632 silver badges6 bronze badges Add a comment | 3 body{ background-image: url(".../img/background.jpg"); background-size: 100vw 100vh; background-repeat: no-repeat; } Share Improve this answer Follow answered Mar 24, 2021 at 8:58 coderX's user avatar coderXcoderX 2135 silver badges18 bronze badges 1
  • 1 Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Ran Marciano Commented Mar 24, 2021 at 19:36
Add a comment | 2

Try this:

background: url(../IMAGES/background.jpg) no-repeat; background-size: auto auto; Share Improve this answer Follow edited Apr 23, 2016 at 8:00 Jai Kumar Rajput's user avatar Jai Kumar Rajput 4,1873 gold badges40 silver badges66 bronze badges answered Nov 12, 2013 at 9:52 Rohit's user avatar RohitRohit 6776 silver badges25 bronze badges Add a comment | 2

.. I found the above solutions didn't work for me (on current versions of firefox and safari at least).

In my case I'm actually trying to do it with an img tag, not background-image, though it should also work for background-image if you use z-height:

<img src='$url' style='position:absolute; top,left:0px; width,max-height:100%; border:0;' >

This scales the image to be 'fullscreen' (probably breaking the aspect ratio) which was what I wanted to do but had a hard-time finding.

It may also work for background-image though I gave up on trying that kind of solution after cover/contain didn't work for me.

I found contain behaviour didn't seem to match the documentation I could find anywhere - I understood the documentation to say contain should make the largest dimension get contained within the screen (maintained aspect). I found contain always made my image tiny (original image was large).

Contain was with some hacks closer to what I wanted than cover, which seems to be that the aspect is maintained but image is scaled to make the smallest-dimension match the screen - i.e. always make the image as big as it can until one of the dimensions would go offscreen...

I tried a bunch of different things, starting over included, but found height was essentially always ignored and would overflow. (I've been trying to scale a non-widescreen image to be fullscreen on both, broken-aspect is ok for me). Basically, the above is what worked for me, hope it helps someone.

Share Improve this answer Follow edited Nov 23, 2016 at 4:13 answered Oct 29, 2014 at 0:08 pacifist's user avatar pacifistpacifist 7224 silver badges13 bronze badges 0 Add a comment | 2

This worked for me:

body { background-image:url(../IMAGES/background.jpg); background-position: center center; background-repeat: no-repeat; background-attachment: fixed; background-size: cover; } Share Improve this answer Follow answered Jul 5, 2020 at 4:00 code001's user avatar code001code001 211 bronze badge 1
  • 2 Welcome to Stack Overflow. When answering a six year old question with an accepted answer and twelve existing answers it is very important to point out what new aspect of the question your answer addresses. Code only answers can generally be improved by explaining how and why they work. – Jason Aller Commented Jul 5, 2020 at 4:41
Add a comment | 0

Try this,

.appBg { background-image: url(".../img/background.jpg"); background-repeat:no-repeat; -webkit-background-size: 100% auto; -moz-background-size: 100% auto; -o-background-size: 100% auto; background-size: 100% auto ; }

This one works for me

Share Improve this answer Follow answered Jun 30, 2017 at 8:52 Udara Herath's user avatar Udara HerathUdara Herath 8853 gold badges19 silver badges41 bronze badges Add a comment | 0

Background image fix to screens with browser compatibility css

.full-screen { height: 100%; width: 100%; background-image: url(../images/banner.jpg); background-size: cover; background-position: center; //for browser compatibility -moz-background-size: cover; -webkit-background-size: cover; -o-background-size: cover; } Share Improve this answer Follow answered Jul 27, 2018 at 12:58 Muhammad Bilal's user avatar Muhammad BilalMuhammad Bilal 2,96828 silver badges53 bronze badges Add a comment | 0

Although there are answers to this your questions but because I was once a victim of this problem and after few search online i was unable to solve it but my fellow hub mate helped me and i feel i should share. Examples explained below.

Folders: web-projects/project1/imgs-journey.png

background-image:url(../imgs/journey.png); background-repeat:no-repeat; background-size:cover;

My major points is the dots there if you noticed my journey.png is located inside an imgs folder of another folder so you're to add the dot according to the numbers folders where your image is stored. In my case my journey.png image is saved in two folders that's why two dot is used, so i think this may be the problem of background images not showing sometimes in our codes. Thanks.

Share Improve this answer Follow edited Apr 1, 2019 at 20:56 Sebastian Brosch's user avatar Sebastian Brosch 43.4k15 gold badges76 silver badges89 bronze badges answered Sep 5, 2018 at 14:51 Stanzuck's user avatar StanzuckStanzuck 375 bronze badges Add a comment | 0 width: 100%; background-image: url("images/bluedraw.jpg"); background-size: cover; Share Improve this answer Follow answered Jun 27, 2020 at 5:43 Kalon Acharjee's user avatar Kalon AcharjeeKalon Acharjee 1 0 Add a comment | 0

Similar to some of the answers above but I wouldn't set in the body, it is less flexible that way. If I were you, I would create a container and set the background properties specific to it. This allows the image to scale with the page AND enables it to fully take up the background even in situations where the page size differs drastically from the image proportions, see images at the very bottom as an example.

To create such a container, you can do something similar to the following:

In your html page:

<div class='background-div'></div>

In your style sheet:

.background-div { top: 0; right: 0; bottom: 0; left: 0; position: fixed; background-image: url(../images/background.jpg); background-size: cover; background-position: center; }

The CSS block does the following:

  1. Zero out any positioning, ie sets image to start at the top most left corner.
  2. We set position to fixed in order remove the element from the normal document flow.
  3. Set image location. You should change it your respective folder.
  4. We set background to cover which will make the image fit the screen.
  5. Lastly, we center the image.

background style applied to <div> container background style applied to div container

background style applied to <body> container [![background style applied to body element][2]][2](bordered)

Share Improve this answer Follow answered Apr 29, 2023 at 16:22 N.Atanasov's user avatar N.AtanasovN.Atanasov 3812 silver badges6 bronze badges Add a comment | -1

You can do it like what I did with my website:

background-repeat: no-repeat; background-size: cover; Share Improve this answer Follow edited May 17, 2016 at 20:37 Michał Perłakowski's user avatar Michał Perłakowski 92.1k30 gold badges163 silver badges185 bronze badges answered May 17, 2016 at 18:14 Ya ag's user avatar Ya agYa ag 1 Add a comment | -1 background: url("../image/b21.jpg") no-repeat center center fixed; background-size: 100% 100%; height: 100%; position: absolute; width: 100%; Share Improve this answer Follow edited Jul 28, 2016 at 12:03 demonplus's user avatar demonplus 5,79112 gold badges53 silver badges69 bronze badges answered Jul 28, 2016 at 10:19 Ravi dangar's user avatar Ravi dangarRavi dangar 11 bronze badge 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

4 How do I make my background-image fit the entire screen CSS 0 CSS Background image is not working properly 939 Stretch and scale a CSS image in the background - with CSS only 0 How can I make my background image fit to phone size? 0 Image background not fitting screen -1 How to make background image fit browser window size 0 Scale background img to the user's browser size 8 Responsive web design : "How to resize a background image according to browser window size using CSS"? 0 Fitting image size across screen resolutions 0 Responsive background image resolution 0 HTML and CSS background image scaling to fit screens 0 How to scale background-image to fit responsive div 4 Fit div size to background image 1 How to make background image scale to 100% of height and width even if screen size is not big enough 2 How to scale a background image in a div responsively to the screen size 0 Fit background image to the whole screen no matter what size screen is 0 How can I make a background-image adjust to screen size?

Hot Network Questions

  • Does the canonical distribution assign probability to microstates or macrostates?
  • Are Quantum Algorithms better than classical algorithms for all problems?
  • Short story about a man living In an apartment who's curious about his neighbor who turns out to be a monster or demon
  • Is my evaluation for this multiple linear regression correct?
  • What is the Calvinist/Reformed solution to the Problem of Hell?
  • 1980s or 90s space cartoon with a space prince and princess
  • What is the basis for the view of Oneness in Theravada?
  • Darlington-driven PNP vs. MOSFET
  • Installing MacTex on new machine -- where do my .cls and .sty files go?
  • Does the twin paradox hold in a universe that's empty except for the twins?
  • Why does 写真に収めとこ mean take a picture? And what is the purpose of とこ in this case?
  • How to DSolve[{-0.8*y'[x]*y''[x]/(1 + (y'[x])^2)^1.4 == 0.77781490, y[0] == 0, y[1] == 1}, y[x], x]
  • Naive proof that subgroup of free group is free, what's wrong?
  • How can I solve a multi-delay differential equation?
  • Does the earliest known use of an "average" occur after the invention of calculus?
  • Misplaced \noalign while using tabularray
  • Accused of violating NDA on thesis
  • C# basic calculator
  • How does the Warlock's invocation Gaze of Two Minds interact with a creature with the Invisible Condition?
  • Advantages of information criteria over cross-validation
  • How can I tell if commercial packaging is suitable for Sous Vide cooking?
  • Is partial correctness decidable?
  • How to draw this matrix with stairs and submatrices?
  • Can a storage device completely erase itself while performing the erase?
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 Size To Fit Screen