HTML Background Images
HTML Background Images
HTML CSS
Menu
ADVERTISEMENT
Log in
Example
Add a background image on a HTML element:
Try it Yourself »
You can also specify the background image in the <style> element, in the <head>
section:
Example
Specify the background image in the <style> element:
Dark mode
D k d
Dark code
<style>
HTML CSS
p {
background-image: url('img_girl.jpg');
ADVERTISEMENT
}
</style>
Try it Yourself »
Example
Add a background image for the entire page:
<style>
body {
background-image: url('img_girl.jpg');
}
</style>
Try it Yourself »
Background Repeat
If the background image is smaller than the element, the image will repeat itself,
horizontally and vertically, until it reaches the end of the element:
Dark mode
D k d
Dark code
HTML CSS
ADVERTISEMENT
Example
<style>
body {
background-image: url('example_img_girl.jpg');
}
</style>
Try it Yourself »
To avoid the background image from repeating itself, set the background-repeat
property to no-repeat .
Example
<style>
body {
background-image: url('example_img_girl.jpg');
background-repeat: no-repeat;
}
</style>
Try it Yourself »
ADVERTISEMENT
Dark mode
D k d
Dark code
HTML CSS Ads by
Send feedback Why this ad?
ADVERTISEMENT
Background Cover
If you want the background image to cover the entire element, you can set the
background-size property to cover.
Also, to make sure the entire element is always covered, set the background-
attachment property to fixed:
This way, the background image will cover the entire element, with no stretching (the
image will keep its original proportions):
Example
<style>
body {
background-image: url('img_girl.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
</style>
Try it Yourself »
Background Stretch
Dark mode
D k d
Dark code
If you want the background image to stretch to fit the entire element, you can set the
HTML
background-size
CSS
property to 100% 100% :
ADVERTISEMENT
Try resizing the browser window, and you will see that the image will stretch, but
always cover the entire element.
Example
<style>
body {
background-image: url('img_girl.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
}
</style>
Try it Yourself »
To learn more about CSS background properties, study our CSS Background Tutorial.
Dark mode
D k d
Dark code
❮ Previous
HTML CSS ❯
Next
ADVERTISEMENT
ADVERTISEMENT
COLOR PICKER
Get certified
by completing
a HTML
course today!
Dark mode
D k d
Dark code
HTML CSS w3
school
s
ADVERTISEMENT
2
CE
02
TI 2
R
FI .
ED
Get started
ADVERTISEMENT
ADVERTISEMENT
Dark mode
D k d
Dark code
HTML CSS
ADVERTISEMENT
Report Error
Spaces
Upgrade
Newsletter
Get Certified
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference Dark mode
D k d
HTML Colors Dark code
HTML CSS Java Reference
Angular Reference
jQuery Reference
ADVERTISEMENT
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.