Using Images as Backgrounds with CSS3

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:

Join us in our newest publication:

Screen Shot 2016-11-06 at 3.44.54 PM

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

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

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


Share and Enjoy !

0 0