0

I want adding background image with css but it did not work.Browser show me a blank page.

my application.css

home-body {
    background-image: url('/home.jpg') no-repeat center center fixed;

    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
     background-size: cover;

}

file home.jpg located in a directory /public/home.jpg.

How fix?

4
  • 1
    instead of using background-image: use only background. Commented Oct 15, 2014 at 8:01
  • What does your directory structure look like? Did you mean to use .home-body as a class? Commented Oct 15, 2014 at 8:02
  • 2
    shouldn't you have home-body as a class or id? .home-body {...} or #home-body {...} Commented Oct 15, 2014 at 8:05
  • @leftside really have eagle eyes, I am sure this will help OP. Commented Oct 15, 2014 at 8:06

5 Answers 5

1

If you are using an external CSS file... make sure to link the image file relative to the CSS file. For instance

-ROOT -css -file.css -background.jpg -index.html

you would need:

background("../background.jpg");

because ".." takes you up one directory :)

Sign up to request clarification or add additional context in comments.

Comments

1

it may be caused by 3 problem

1- home-body is not a tag of html.so it should be id or class name of a tag.so you should use .home-body for class name and #home-body for id name.

2- you do not address the background image correctly.if you want to make sure, give the url as an absolute path to your image like http://yoursiteUrl/public/image/image.png

3- your css file is not loaded on your page.try to make sure it is loaded on your site correctly if it is loaded then the problem is first or the second part mentioned here.

Comments

1

Please use this code. Use background instead of background-image and make sure that you have inserted the correct image path in url().

    html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
    .home-body {
    background: url('/home.jpg') no-repeat center center fixed;

    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
     background-size: cover;

}

5 Comments

This would not make a difference. Background is an alias for all 5 properties. background-color, background-image, background-position, background-repeat and background-attachment
This is works when I add width and height properties, but it looks bad not the entire screen, and on all sides of a white frame.why?
I have edited the answer let me confirm if this works
@138weare you have to define the width and height use 100% and use padding:0 for body element.
How to stretch it over the entire width of the window?
0

very easy like this....

.play{
    background: url("media_element/play.png") center center no-repeat;
    position : absolute;
    display:block;
    top:20%; 
    width:50px;
    margin:0 auto;
    left:0px;
    right:0px;
    z-index:100
} 

Comments

0
body {
    background-image: url('img-url');
    background-size: cover;
    background-repeat:no-repeat

}

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.