- CSS Background Image Size Tutorial – How to Code a Full Page Background Image
- Cover Viewport with Image
- Magic of ‘Background-Size’ Property
- How to Fine Tune an Image Position and Avoid Tiling
- How to Fix an Image in Place When Scrolling
- Optional: How to Add a Media Query for Mobile
- Want to See More Web Development Tips and Knowledge?
- How to Make HTML Background Image Full Screen?
- Pre-requisites
- What are we Creating?
- How to Make Full-Screen Responsive Background Image with CSS
- 1. Fill the Entire Viewport with the background-size Property
- 2. Use Media Queries to Provide a Smaller Background Image for Mobile Devices
- HTML Background Image Full Screen Without CSS
- Conclusion
- Responsive Full-Screen Background Image (Simple Example)
- TLDR – QUICK SLIDES
- TABLE OF CONTENTS
- DOWNLOAD & NOTES
- QUICK NOTES
- EXAMPLE CODE DOWNLOAD
- RESPONSIVE BACKGROUND DEMO
- CITY LIFE
- RESPONSIVE BACKGROUND IMAGE
- PART 1) THE HTML
- PART 2) THE CSS
- EXTRA BITS & LINKS
- LINKS & SUMMARY
- TUTORIAL VIDEO
- INFOGRAPHIC CHEAT SHEET
- THE END
CSS Background Image Size Tutorial – How to Code a Full Page Background Image
This tutorial will show you a simple way to code a full page background image using CSS. And you’ll also learn how to make that image responsive to your users’ screen size.
Making a background image fully stretch out to cover the entire browser viewport is a common task in web design. Fortunately, this task can be taken care of with a few lines of CSS.
Cover Viewport with Image
First, we will want to make sure our page actually covers the entire viewport:
Inside html , we will use the background-image property to set the image:
background-image: url(image.jpg); /*replace image.jpg with path to your image*/
Magic of ‘Background-Size’ Property
The magic happens with the background-size property:
cover tells the browser to make sure the image always covers the entire container, in this case html . The browser will cover the container even if it has to stretch the image or cut a little bit off the edges.
Because the browser may stretch the image, you should use a background image that has high enough resolution. Otherwise the image may appear pixelated.
If you care about having all of the image appear in the background, then you will want to make sure the image is relatively close in aspect ratio compared to the screen size.
How to Fine Tune an Image Position and Avoid Tiling
The browser can also choose to display your background image as tiles depending on its size. To prevent this from happening, use no-repeat :
background-repeat: no-repeat;
To keep things pretty, we will keep our image always centered:
background-position: center center;
This will center the image both horizontally and vertically at all times.
We have now produced a fully responsive image that will always cover the entire background:
How to Fix an Image in Place When Scrolling
Now, just in case you want to add text on top of the background image and that text overflows the current viewport, you may wish to make sure your image stay in the background when the user scrolls down to see the rest of the text:
background-attachment: fixed;
You can include all of the background properties described above using short notation:
background: url(image.jpg) center center cover no-repeat fixed;
Optional: How to Add a Media Query for Mobile
To add some icing on the cake, you may wish to add a media query for smaller screens:
@media only screen and (max-width: 767px) < html < background-image: url(smaller-image.jpg); >>
You can use Photoshop or some other image editing software to downsize your original image to improve page load speed on mobile internet connections.
So now that you know the magic of creating a responsive, full page image background, it is time to go create some beautiful websites!
Want to See More Web Development Tips and Knowledge?
How to Make HTML Background Image Full Screen?
It is often more visually pleasing and interesting to add images to the background of certain parts of a website than to change only the color of the background. Depending on the available space, the image can be left at its natural size, stretched, or constrained. This article describes how to set an image as the background for an element using the background-size property in CSS.
Pre-requisites
- HTML for the structure of our image.
- CSS for styling and making our background image responsive.
What are we Creating?
Here we’ll learn how to stretch a background image to cover the entire browser viewport. The CSS background-size property will be used to accomplish this, and JavaScript is not required. Background images are often stretched out to cover the entire browser viewport by web designers as it looks enticing to the users who are visiting our websites. It is pretty easy to accomplish this task by using a few lines of CSS in our projects. The final output of what we would create is shown below :
How to Make Full-Screen Responsive Background Image with CSS
To make our images responsive, we plan to do the following:
1. Fill the Entire Viewport with the background-size Property
It is possible to set the CSS background-size property to cover. Using this value, the browser will automatically and proportionally scale the background image’s width and height so that they are either equal to or greater than the view port’s width and height.
2. Use Media Queries to Provide a Smaller Background Image for Mobile Devices
A scaled-down version of the background image file will be served to speed up the page load on small screens with a media query. Choosing this option is optional.
What are the benefits of serving a smaller background image on a mobile device? In the demo, I used an image of about 5 9 7 6 ∗ 3 5 7 0 5976*3570 5 9 7 6 ∗ 3 5 7 0 px.
With this size, we’ll be covered on the vast majority of widescreen computer monitors currently available. However, it will require us to serve up a 1 . 5 M B 1.5MB 1 . 5 M B file.
It is never a good idea for a background photo to take up such a large amount of data, but it is especially bad for mobile Internet. Further, the image dimension is too large on small-screen devices.
We can make the images responsive by using the following approach:
Here is all the markup you need:
We will assign the image to be used as a background to the body element so that the image always covers the entire viewport of our browsers.
Now our background image covers the entire viewport of the browser, and we’re going to assign the image to the body element. Nevertheless, this technique works on any element at the block level (such as a div or a form).
The background image will always scale so that it covers the whole block-level container if the container’s width and height are fluid.
Here’s where the magic happens. Whenever this property/value pair is set, the browser scales the background image to conform to an element’s width and height. Here we’re using the body element as an example.
When a browser sees a background image that is smaller than the body element’s dimensions, it will programmatically enlarge the image. This happens on high-resolution screens with small background images.
It is well known that when images are scaled above their natural dimensions, the image quality degrades (pixelation occurs).
Image quality is negatively affected when images are scaled beyond their natural dimensions.
Consider that when selecting the image for your project, As the demo uses 5 9 7 6 ∗ 3 5 7 0 5976*3570 5 9 7 6 ∗ 3 5 7 0 px photos for larger screens, it’ll be a while before there are problems.
Depending on the size of your background image, the browser can display it as tiles.
Using no-repeat will prevent this from happening:
We will keep our image centered on keeping things looking nice:
In this way, the image will be vertically and horizontally centered at all times.
The next problem is when the height of the content is greater than the height of the visible viewport. In this case, there will be a scroll bar. The background image must remain in place during scrolling down. Otherwise, either the image runs out at the bottom of the background or moves when the user scrolls down (which can be very distracting).
This can be accomplished by setting the background-attachment property to fixed.
Using short notation, all of the background properties described above can be included :
Now we have achieved a fully responsive image that will always cover the entire background and look enticing to the users who visit our website. The code to achieve it is shown below :
The output with the usage of the media query is shown below :
If your mobile connection is slow, you can downsize your original image using Photoshop or another image-editing software.
One of the major disadvantages of using the media query is that, when the browser window is resized, for example, from 1 2 0 0 p x 1200px 1 2 0 0 p x to 6 4 0 p x 640px 6 4 0 p x (or vice versa), the background image will momentarily flicker while it loads.
HTML Background Image Full Screen Without CSS
A variety of methods can be used to set the HTML background image to full screen. In this example, the image will cover the entire screen. This can be done with or without CSS. As you can see in the example below, when we zoom in and out, there is no change in the background image, which is all done using the HTML attributes without adding any CSS.
- The first property used here is a background image, which sets an image as the background by using the URL.
- Background-repeat is the next attribute used to keep our image from repeating.
- Background size is one of the most important attributes used here because it fixes the image in the background so that it does not change when the webpage is zoomed in and out. The cover is given as the value.
Conclusion
- The CSS background-size property has been used to make the HTML background image full-screen.
- An element’s background image is controlled by the background-size CSS property. Depending on the available space, the image can be left at its natural size, stretched, or constrained.
- It is also possible to add an image without using CSS using the background-image, background-repeat, and background-size attributes.
Responsive Full-Screen Background Image (Simple Example)
Welcome to a tutorial on how to create a responsive full-screen background image with HTML and CSS. Need to add a background image that will look nice on both big and small screens?
A simple way to create a responsive full-screen background image is to set a background image that covers the entire window – body < width: 100vw; min-height: 100vh; background: url("IMAGE"); background-size: cover; >.
That covers the quick basics, but read on for a few more examples!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming .
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
RESPONSIVE BACKGROUND DEMO
CITY LIFE
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
RESPONSIVE BACKGROUND IMAGE
All right, let us now get into the examples of how to create responsive background images.
PART 1) THE HTML
CITY LIFE
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- We will be applying the background image onto itself.
- Then, will serve as the “foreground content”.
PART 2) THE CSS
/* (A) FILL ENTIRE WINDOW */ body, main < width: 100vw; min-height: 100vh; >/* (B) - BACKGROUND */ body < /* (B1) REMOVE DEFAULT PAGE SPACING */ margin: 0; padding: 0; /* (B2) BACKGROUND IMAGE */ background-image: url("city.webp"); background-size: cover; background-position: center; background-repeat: no-repeat; >/* (C) - FOREGROUND */ main < padding: 20px; color: #fff; background: rgba(0,0,0,0.5); >/* (X) NOT REALLY IMPORTANT */ *
Just what is happening here?
- We set both to width: 100vw and min-height: 100vh . This will make the page fill up the entire window by default.
- Remember that will serve as the background?
- (B1) First, use margin: 0; padding: 0; to remove the annoying default page spacing.
- (B2) background-image: url(«city.webp») The background image.
- (B2) background-size: cover The background image will cover the entire page. You can also set this to contain , specify a percentage or even exact pixels.
- (B2) background-position: center Center the background image on the page.
- (B2) background-repeat: no-repeat We do not want the background image to repeat itself but stretch to fill the page. Remove this if you are using a repeating pattern.
- Finally, will serve as foreground content. All we are doing here is setting the font and color “as usual”. Give it a transparent background so that the text can show up a little better.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & SUMMARY
TUTORIAL VIDEO
INFOGRAPHIC CHEAT SHEET
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!