How To Set A Background Image In HTML: 13 Steps (with Pictures)

Skip to ContentQuizzes
  • Home
  • Random
  • Browse Articles
  • Quizzes & Games
  • All QuizzesHot
  • Love Quizzes
  • Personality Quizzes
  • Fun Games
  • Dating Simulator
  • Learn Something New
  • Forums
  • Courses
  • Happiness Hub
  • Explore More
  • Support wikiHow
  • About wikiHow
  • Log in / Sign up
Terms of Use wikiHow is where trusted research and expert knowledge come together. Learn why people trust wikiHow Add a Background Image to a Website: Step-by-Step Tutorial PDF download Download Article Add a fixed or tiled background image to a webpage with this user-friendly guide Written by Nicole Levine, MFA

Last Updated: March 17, 2025 Fact Checked

PDF download Download Article
  • Set a Web Page Background
  • |
  • Background Tricks & Examples
  • |
  • Video
  • |
  • Q&A
  • |
  • Tips
|Show more |Show less X

This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources. This article has been viewed 2,300,475 times. Learn more...

Want to learn the best way to add a background image to your website? Adding an image as your page's background is a little different than inserting an image into other areas of your website. Instead of using plain HTML code, you'll need to use CSS (Cascading Style Sheets). Fortunately, it's easy to set a background image on your website. This wikiHow article will walk you through setting your web page's background using HTML and CSS using simple examples. We'll also give you some more advanced tips and tricks for resizing and aligning your background image.

Coding an HTML Background Image

Use the CSS property background-image: url("my_background.png"); to set your background image. Replace "my_background.png" with the location of your image, whether it's the full URL or the relative path on your web server. Make it cover the page with background-repeat: norepeat; and background-size: cover;.

Steps

Section 1 of 2:

Set a Web Page Background

PDF download Download Article
  1. Step 1 Open your HTML document. 1 Open your HTML document. You can use any text editor, such as Notepad++ or TextEdit, to edit your HTML file.
    • If you haven't created your webpage yet, check out our guide to creating a simple website in HTML.
    • If you prefer, you can also add your CSS code to a file that's separate from your HTML document. This involves creating a styles.css file for your CSS code, and linking it to your HTML document using the <link> tag. We'll focus this guide on adding the CSS directly to your HTML file, but check out How to Create CSS to learn how to create separate style sheets.
  2. Step 2 Type <style> in the head of the HTML document. 2 Type <style> in the head of the HTML document. This is the opening tag for cascading style sheets (CSS), which should go between the <head> and </head> tags.
    • Alternatively, you can create your CSS on a separate CSS document and link it to your HTML document.
    Advertisement
  3. Step 3 Type body { on the next line. 3 Type body { on the next line. This is the opening of the CSS code that will style the body of your HMTL document.[1]
  4. Step 4 Type the code to set a background image. 4 Type the code to set a background image. background-image: url("filename.jpg");. Replace filename.jpg with the path to the background image you'd like to use.[2]
    • If the background image is in the same folder as your HTML file, you can just use the file name of the image. For example, background-image: url("my_background.png");.
    • If the image is on the web and not in the same folder as your HTML file, you'll need to specify the path to the image. This can be the full URL to the image. For example, background-image: url("https://www.wikihow.com/images/my_background.png");.
    • You can also use the path to the image on your web server. For example, if you are editing the HTML file www.wikihow.com/mypage.html and your background image is at www.wikihow.com/images/my_background.png, you can set your background using background-image: url("/images/my_background.png");.
  5. Step 5 Choose whether the background repeats or stays a single image. 5 Choose whether the background repeats or stays a single image. By default, if the background image is smaller than the page, it will automatically repeat itself. If you want the background image to only appear once, type background-repeat: no-repeat; on the next line.[3]
    • If you do want the background to repeat, such as when you have a small image that you want to tile like wallpaper, you can replace no-repeat with other options:
      • background-repeat: repeat;: Repeats the image horizontally and vertically, like a traditional wallpaper effect.
      • background-repeat: repeat-x;: Repeats the image horizontally.
      • background-repeat: repeat-y;: Repeats the image vertically.
      • background-repeat: space repeat;: Repeats the image so it runs vertically along the left and right sides of the page, with empty space at the center.
      • background-repeat: space;: Repeats the image on all four corners of the page with space between the images.
  6. Step 6 Make the background cover the entire page. 6 Make the background cover the entire page. If you chose not to repeat the background using background-repeat: no-repeat;, you'll want to make sure the background image covers the entire page, even if it's smaller than the frame. Otherwise, you'll see a bit of the page's background color around the edges of the background image. To do this, type or paste this code onto the next line: background-size: cover;.[4]
  7. Step 7 Choose how the background behaves when scrolling. 7 Choose how the background behaves when scrolling. If you want to keep the background fixed in place as the user scrolls through the page, you can anchor the background image to the viewport using the code background-attachment: fixed; on the next line.[5]
    • Alternatively, if you want the background to scroll along with other HTML elements like paragraphs and images, you can use background-attachment: local;.[6]
  8. Step 8 Type } at the end of the "Body" section of your CSS. 8 Type } at the end of the "Body" section of your CSS. If you want to include other CSS code that affects the body of your document, such as setting font colors or centering your content, include them now. Then, type "}" below your last line of CSS to close the body section.
  9. Step 9 Type </style> at the end of your CSS. 9 Type </style> at the end of your CSS. Add this line before the </head> tag. This tag closes your CSS.
  10. Step 10 Save the HTML file. 10 Save the HTML file. When you are finished with everything, click File and then Save to save your work. Your entire HTML document should look something like this:
      <!DOCTYPE html> <html> <head> <title>Page Title</title> <style> body { background-image: url("/images/my_background.png"); background-repeat: no-repeat; background-size: cover; background-attachment: fixed; } </style> </head> <body> </body> </html>
  11. Advertisement
Section 2 of 2:

Background Tricks & Examples

PDF download Download Article
  1. Step 1 Add a background image to any HTML tag. 1 Add a background image to any HTML tag. Background images can be set for any HTML element, including paragraphs, headers, and more. In this example, we'll set a background image for the <p> tag, which makes it so all content in paragraph tags inherits the background image. In your CSS file, or within the <style></style> section of your HTML document, add a new CSS selector for the element you want to give a background image. In this example, we'll add a small background image at the center of the paragraph behind the text: <p> tag: <head> <style> p { background-image: url("/images/my_background.png"); background-position: left; } </style> </head>
  2. Step 2 Use the background-size property to resize background images. 2 Use the background-size property to resize background images. You can use CSS to change the size of your background in three ways: using the keywords cover or contain, by specifying a width value alone, or by specifying width and height values together.
    • background-size: cover; Scales the background image to cover the entire container without stretching or shrinking the image. The container can be the entire web page or any other element. This is most common when setting a background image for an entire web page.
    • background-size: contain;: This option scales the image to a smaller size to fit a container.[7] This option is useful if your background image is much larger than the container you're using it in. If the image is smaller, it will tile.
    • background-size: background-size: 150px 200px;: This option makes the background image 150px wide and 200px tall. The first measurement is width, and the second is height. However, length is optional—if you only enter the width, the height will automatically scale without stretching or warping.
      • You can also specify length and width in percentages.
  3. Step 3 Use the background-position property to align a background. 3 Use the background-position property to align a background. This property will help you position your background image relative to the container, which could be the page (if used in the body tag), or any other container. You can specify two values for background-position—the first is always the horizontal value. If you don't specify a vertical value, the default will always be 50%.[8] Some examples:
    • background-position: center;: Centers the background image.
    • background-position: top;: Aligns to the top of the element.
    • background-position: bottom;: Aligns to the bottom of the element.
    • background-position: left;: Aligns to the left side of the element.
    • background-position: right;: Aligns to the right side of the element.
    • background-position: right top;: Aligns to the right side and the top of the element.
    • background-position: center bottom;: Aligns to the center and the bottom of the element.
    • background-position: top 20px right 10px;: Aligns to the top with a 20px offset, and the right with a 10px offset. You can also specify the pixels as percentages.
    • background-position: 20% 60%;: Positions the background 20% horizontally from the left side, and 60% vertically from the top.
  4. Step 4 Set a background color. 4 Set a background color. Even if you're adding a background image to your page, it's always a good idea to set a background color as well. If the background image doesn't load, the user will see the background color you select. To do this, add the background-color: #FF33F0;, replacing FF33F0 with your preferred HTML color hex code.
  5. Advertisement

Community Q&A

Search Add New Question
  • Question Hi! When I upload a background image I'm ending up with repeated copies. I just want a single image as background. wikiHow Staff Editor wikiHow Staff Editor Staff Answer This answer was written by one of our trained team of researchers who validated it for accuracy and comprehensiveness. wikiHow Staff Editor wikiHow Staff Editor Staff Answer Use "background-repeat: no-repeat;" followed by "background-size: cover;" in your CSS to make sure the background doesn't repeat and covers the entire page. Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 2 Helpful 3
  • Question How do I apply a background image from a folder? Community Answer Community Answer Make sure that the folder is in the same directory as the HTML, then just type the path. If there was an image called "image1.png" inside a folder named "folder," it would look like "/folder/image1.png.". Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 96 Helpful 120
  • Question How do I add a photo to an HTML document for the background? Community Answer Community Answer By using the background-img=" " tag, we can insert an image in HTML. You can add a colored background with the style attribute; for example, body style="background:yellow". Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 56 Helpful 49
See more answers Ask a Question 200 characters left Include your email address to get a message when this question is answered. Submit Advertisement

Video

Tips

  • If the background image doesn't show up, you've likely uploaded the image to a location that's different than what you specified in background-image. If the image is not in the same folder as your HTML file, you will need to specify the path to the image rather than just the image name. For example, background-image: url("/images/my_background.png"); instead of background-image: url("my_background.png");. Thanks Helpful 3 Not Helpful 2
Submit a Tip All tip submissions are carefully reviewed before being published Name Please provide your name and last initial Submit Thanks for submitting a tip for review! Advertisement

You Might Also Like

Set Background Color in HTML3 Methods to Set a Background Color with HTML & CSS Create a Simple CSS Stylesheet Using NotepadHow toCreate a Simple CSS Stylesheet Using Notepad Insert Images with HTMLHow toInsert Images with HTML Resize iFrames in HTMLHow toResize iFrames in HTML Add Background Graphics to PowerpointHow toAdd Background Graphics to Powerpoint Set Image Width and Height Using HTMLHow to Set Image Width and Height Using HTML Create Redirects in HTMLHow toCreate Redirects in HTML Insert a Background Image on One Page in Word4 Ways to Insert a Background Image on One Page in Word Change Text Color in HTMLHow to Change Text Color in HTML and CSS Upload Your Own Fonts to HTML Using CSSHow toUpload Your Own Fonts to HTML Using CSS Create a Simple Web Page with HTMLHow to Create a Basic HTML Website: A Step-by-Step Guide Create a Background in InDesignHow to Create a Background in InDesign Change the Button Color in HTMLHow toChange the Button Color in HTML Embed PicturesHow to Embed Images in Documents and Websites Advertisement

References

  1. https://www.geeksforgeeks.org/how-to-set-background-image-in-html/
  2. https://www.freecodecamp.org/news/html-background-image-how-to-add-wallpaper-images-to-your-website/
  3. https://developer.mozilla.org/en-US/docs/Web/CSS/background-repeat
  4. https://www.freecodecamp.org/news/html-background-image-how-to-add-wallpaper-images-to-your-website/
  5. https://www.freecodecamp.org/news/html-background-image-how-to-add-wallpaper-images-to-your-website/
  6. https://developer.mozilla.org/en-US/docs/Web/CSS/background-attachment
  7. https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
  8. https://developer.mozilla.org/en-US/docs/Web/CSS/background-position

About This Article

Nicole Levine, MFA Written by: Nicole Levine, MFA wikiHow Technology Writer This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 2,300,475 times. How helpful is this? Co-authors: 44 Updated: March 17, 2025 Views: 2,300,475 Categories: HTML In other languages Spanish Italian Portuguese Russian German Indonesian French Dutch Thai Arabic Korean Vietnamese Japanese Hindi Chinese Turkish Persian
  • Print
  • Send fan mail to authors
Thanks to all authors for creating a page that has been read 2,300,475 times.

Is this article up to date?

Yes No Advertisement Cookies make wikiHow better. By continuing to use our site, you agree to our cookie policy. Nicole Levine, MFA Written by: Nicole Levine, MFA wikiHow Technology Writer Click a star to vote Co-authors: 44 Updated: March 17, 2025 Views: 2,300,475

Quizzes & Games

What Makeup Should I Wear QuizWhat Makeup Should I Wear QuizTake QuizBedroom Aesthetic QuizBedroom Aesthetic QuizTake QuizInterior Design Style QuizInterior Design Style QuizTake QuizWhat Should I Wear Today QuizWhat Should I Wear Today QuizTake QuizGuess the Flag QuizGuess the Flag QuizTake QuizWhat Animal Would Go On My House Crest QuizWhat Animal Would Go On My House Crest QuizTake Quiz

You Might Also Like

Set Background Color in HTML3 Methods to Set a Background Color with HTML & CSSCreate a Simple CSS Stylesheet Using NotepadHow toCreate a Simple CSS Stylesheet Using NotepadInsert Images with HTMLHow toInsert Images with HTMLResize iFrames in HTMLHow toResize iFrames in HTML

Featured Articles

210+ Shower Thoughts: Funny, Deep, Weird & Mind-Blowing210+ Shower Thoughts: Funny, Deep, Weird & Mind-Blowing Meet Gay People Without a Dating AppHow to Meet Gay People Without a Dating AppCute, Thoughtful & Romantic Messages to Wish Her a Good Day at WorkCute, Thoughtful & Romantic Messages to Wish Her a Good Day at WorkWhat Movie Should I Stream QuizWhat Movie Should I Stream QuizWhat Does the Term "Femboy" Mean?What Does the Term "Femboy" Mean?40+ Fun & Exciting Group Game Ideas to Play with 20+ People40+ Fun & Exciting Group Game Ideas to Play with 20+ People

Trending Articles

Will I Ever Find Love QuizWill I Ever Find Love QuizCan We Guess How Tall You Are QuizCan We Guess How Tall You Are QuizHow Sexual Am I QuizHow Sexual Am I QuizMental Illness TestMental Illness TestAm I Funny QuizAm I Funny QuizWhat 16 of the Most Common Hugs MeanWhat 16 of the Most Common Hugs Mean

Featured Articles

11 Things to Do If You’re Connected to Wi-Fi But Have No Internet11 Things to Do If You’re Connected to Wi-Fi But Have No InternetThe Ultimate List of Funny Nicknames for Your CoworkersThe Ultimate List of Funny Nicknames for Your CoworkersWhich Five Nights at Freddy's Character Am I QuizWhich Five Nights at Freddy's Character Am I QuizBest Haircuts for Men with Round Faces (& How to Style Them)Best Haircuts for Men with Round Faces (& How to Style Them)What Is the Chubby Body Type? Plus, How It Compares to Other Body TypesWhat Is the Chubby Body Type? Plus, How It Compares to Other Body TypesDo I Have an Innocent Face QuizDo I Have an Innocent Face Quiz

Featured Articles

Reinvent YourselfHow toReinvent YourselfYe Olde List of Medieval Insults: 40+ Churlish SayingsYe Olde List of Medieval Insults: 40+ Churlish SayingsWhat Are the Rarest Eye Colors?What Are the Rarest Eye Colors?Everything You Need to Know About the “My Roman Empire” TikTok TrendEverything You Need to Know About the “My Roman Empire” TikTok TrendWhat Sport Should I Play QuizWhat Sport Should I Play Quiz80+ Best Ways to Slide Into Your Crush’s Instagram DMs80+ Best Ways to Slide Into Your Crush’s Instagram DMs

Watch Articles

 Marinate Chicken: 4 Recipes and a Step-By-Step GuideHow to Marinate Chicken: 4 Recipes and a Step-By-Step GuideClean Shower TileHow toClean Shower TileDo Oblique CrunchesHow toDo Oblique CrunchesSimple Ways to Riffle Shuffle and Bridge Playing CardsSimple Ways to Riffle Shuffle and Bridge Playing CardsMake Black CoffeeHow toMake Black CoffeePlant a Seed in a PotHow toPlant a Seed in a Pot

Trending Articles

What Type of Person Do I Attract QuizWhat Type of Person Do I Attract QuizAccurate IQ Checker Quiz: How Smart Am I?Accurate IQ Checker Quiz: How Smart Am I?What's My Eye Type QuizWhat's My Eye Type QuizHow Many Secret Admirers Do I Have QuizHow Many Secret Admirers Do I Have QuizSpotify: All The Ways to View Your Spotify Wrapped SlideshowSpotify: All The Ways to View Your Spotify Wrapped SlideshowDo I Attract the Male Gaze or the Female Gaze QuizDo I Attract the Male Gaze or the Female Gaze Quiz

Quizzes & Games

Engagement Ring QuizEngagement Ring QuizTake QuizWhat Tattoo Matches My Personality QuizWhat Tattoo Matches My Personality QuizTake QuizWhat's My Hair Type QuizWhat's My Hair Type QuizTake QuizPhotographic Memory TestPhotographic Memory TestTake QuizYour Bedroom Personality AnalyzerYour Bedroom Personality AnalyzerAnalyzeWhat Do I Look Like QuizWhat Do I Look Like QuizTake Quiz wikiHow
  • Categories
  • Computers and Electronics
  • Internet
  • Website and Blog Creation
  • Markup Languages
  • HTML
wikiHow Newsletter You're all set! Helpful how-tos delivered toyour inbox every week! Sign me up! By signing up you are agreeing to receive emails according to our privacy policy.
  • Home
  • About wikiHow
  • Experts
  • Jobs
  • Contact Us
  • Site Map
  • Terms of Use
  • Privacy Policy
  • Do Not Sell or Share My Info
  • Not Selling Info
  • Contribute

Follow Us

×

Keep up with the latest tech with wikiHow's free Tech Help Newsletter

Subscribe You're all set! X --1206

Tag » Add Background Image To Section Html