Robert MacLean
Robert MacLean

Reputation: 39261

CSS background-image - What is the correct usage?

What is the correct usage of the CSS background-image property? The key things I am trying to understand is

  1. Does it need to be in quotes i.e.: background-image: url('images/slides/background.jpg');
  2. Can it be a relative path (as above) or must it be a full URL?
  3. Any other points I should be aware of to make sure it works correctly across standards compliant browsers.

Upvotes: 158

Views: 428367

Answers (11)

manoj
manoj

Reputation: 187

You really don't need single quotes or double quotes... But using single quotes or double quotes is a good method...

.section-background {
  background-image: url(https://cdn.pixabay.com/photo/2016/06/08/15/45/lemon-1444025_1280.jpg); /* background image property */
  background-postion: center center;
  background-size: conver;
  background-repeat: no-repeat;
}
<div class="section-background">
  <h1>Title Goes Here</h1>
  <p>Content Goes Here</p>
</div>

Try This Futher Reference - How To Add Background Image in CSS

Upvotes: -1

Athini Watu
Athini Watu

Reputation: 11

you really don't need quotes if let say use are using the image from your css file it can be

{background-image: url(your image.png/jpg etc);}

Upvotes: 1

BERGUIGA Mohamed Amine
BERGUIGA Mohamed Amine

Reputation: 6280

just write in your css file like bellow

background:url("images/logo.jpg")

Upvotes: 1

sunny
sunny

Reputation: 121

just check the directory structure where exactly image is suppose you have a css folder and images folder outside css folder then you will have to use"../images/image.jpg" and it will work as it did for me just make sure the directory stucture.

Upvotes: 2

Amin Saqi
Amin Saqi

Reputation: 18977

If your images are in a separate directory of your css file and you want the relative path begins from the root of your web site:

background-image: url('/Images/bgi.png');

Upvotes: 6

Alex Rozanski
Alex Rozanski

Reputation: 38005

The path can either be full or relative (of course if the image is from another domain it must be full).

You don't need to use quotes in the URI; the syntax can either be:

background-image: url(image.jpg);

Or

background-image: url("image.jpg");

However, from W3:

Some characters appearing in an unquoted URI, such as parentheses, white space characters, single quotes (') and double quotes ("), must be escaped with a backslash so that the resulting URI value is a URI token: '\(', '\)'.

So in instances such as these it is either necessary to use quotes or double quotes, or escape the characters.

Upvotes: 169

Gumbo
Gumbo

Reputation: 655179

  1. No you don’t need quotes.

  2. Yes you can. But note that relative URLs are resolved from the URL of your stylesheet.

  3. Better don’t use quotes. I think there are clients that don’t understand them.

Upvotes: 50

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

1) putting quotes is a good habit

2) it can be relative path for example:

background-image: url('images/slides/background.jpg');

will look for images folder in the folder from which css is loaded. So if images are in another folder or out of the CSS folder tree you should use absolute path or relative to the root path (starting with /)

3) you should use complete declaration for background-image to make it behave consistently across standards compliant browsers like:

background:blue url('/images/clouds.jpg') no-repeat scroll left center;

Upvotes: 36

Bryan
Bryan

Reputation: 2870

Relative paths are fine and quotes aren't necessary. Another thing that can help is to use the "shorthand" background property to specify a background color in case the image doesn't load or isn't available for some reason.

#elementID {
    background: #000 url(images/slides/background.jpg) repeat-x top left;
}

Notice also that you can specify whether the image will repeat and in what direction (if you don't specify, the default is to repeat horizontally and vertically), and also the location of the image relative to its container.

Upvotes: 5

Ben
Ben

Reputation: 2445

Have a look at the respective sitepoint reference pages for background-image and URIs

  1. It does not have to be in quotes but can use them if you like. (I think IE5/Mac does not support single quotes).
  2. Both relative and absolute is possible; a relative path is relative to the path of the css file.

Upvotes: 0

Rigobert Song
Rigobert Song

Reputation: 2784

You don't need to use quotes and you can use any path you like!

Upvotes: 0

Related Questions