0

I want to display background image in all my web pages.My body css code is

body{
  background-image:url('image/silver.jpg');
  background-repeat:no-repeat;
  background-size:cover;
 }

I include this page in my page as

<link rel="stylesheet" type="text/css" href="css/body.css">

But This couldn't display background image now.If I include body css code in head section It display background image.

5
  • 1
    check your path? "../css/body.css/", also close the link tag <link /> Commented Jan 27, 2015 at 6:28
  • Right click on the web page and click inspect element. If it gives an error under console tell us what it says. Commented Jan 27, 2015 at 6:29
  • 1
    or check you bg path background-image:url('../image/silver.jpg'); Commented Jan 27, 2015 at 6:30
  • error in path sample.com/hr/css/image/silver.jpg my actual path is sample.com/hr/image/silver.jpg Commented Jan 27, 2015 at 6:34
  • How to over come this error.Any one can help me Commented Jan 27, 2015 at 6:41

2 Answers 2

3

Paths in CSS are relative to the CSS document, and are not relative to your website's root.

I recommend putting images (and all style assets) in the same directory as the stylesheet to keep paths simpler.

Anyway, change the background-image property to this:

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

...if your site's filesystem is this:

index.html
css/
    style.css
image/
    silver.jpg

In my opinion, your site should resemble this:

index.html
style/
    style.css
    silver.jpg
images/
    // put "content" images here (like a photo of yourself)...
    // ... not "style" images (e.g. a background gradient image).
Sign up to request clarification or add additional context in comments.

Comments

1

Make sure you are specifying the path to the image correctly. Maybe your CSS file is in a different directory than the HTML file?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.