Using Images as Backgrounds with CSS3

Published November 10, 2016 by CSS Newbies.

With CSS you can make an image the background not only of the entire body of pages, but also of individual elements too. All you need to do is use the background property and provide the path to the image.

Let’s say you have this boring page, and you want to punch it up by using a fun image as the background:

Screen Shot 2016-11-06 at 3.44.54 PM

Here’s the CSS you would use to add a fun background:

body{
background: url(path/to/image.jpg);
background-size: cover;
}

Make sure to put the path to your image — either a locally hosted image or a live URL to an image you’ve found online (although saving the background to your server and linking to a local image is definitely the best way to go) in the url parameters for the background property. In this particular case we also used the background-size property to make sure that the image was appropriately sized to the browser). Other properties you can use to customize the way your background image appears on your site are background-position, background-repeat, and background-attachment. Keep in mind that this same CSS technique can be used to set images as backgrounds for elements other than the body, such as divs. Here are the results (results will vary based on which image you choose to use!):

Screen Shot 2016-11-06 at 3.44.26 PM

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>