1

I'm fairly new to web development and have been trying to make a website using Bootstrap, hosted on github. The bootstrap theme I'm using has a header section which I'm trying to cover with a background image (it was originally just a solid color), however the image doesn't appear.

Here is my code: HTML - header section:

<header class="masthead" id="banner">
  <div class="container">
    <div class="col-lg-12 text-center">
      <img class="img-fluid" src="img/headshot.png" alt="Headshot">
      <div class="intro-text">
        <span class="name">NAME</span>
        <hr class="star-light">
      </div>
    </div>
  </div>
</header>

CSS:

header.masthead {
  text-align: center;
  color: white; 
  background: #148ABC;}

#banner {
  background-image: url("/img/banner.jpg") no-repeat center; 
  background-size: cover; 
  width: 100%;
  height: 100%; }

My "banner.jpg" file which is the background image is located in at "img" folder, which is in the same directory as a "css" folder that contains the actual css file. I've tried the following:

  • changing the url to "/img/banner.jpg", "../img/banner.jpg" and "img/banner.jpg" (I think the first one is still right?)
  • removing the #banner id and putting everything in css under header.masthead alone
  • deleting the width and height
  • double checking the name of the file (it is banner.jpg)

I've been working on this for days so I'm pretty frustrated with myself. Thanks for your help!

3
  • folder path "../img/banner.jpg" is the right one. Commented Aug 17, 2017 at 4:32
  • Silly qustion - Have you cleared the browser's cache? Commented Aug 17, 2017 at 4:37
  • I've done both, but it hasn't helped unfortunately Commented Aug 17, 2017 at 4:42

2 Answers 2

1

Just try with this snippet , i have changed background-image to background and remove the background color of the header

header.masthead {
  text-align: center;
  color: white; 
}

#banner {
  background: url("https://www.w3schools.com/css/trolltunga.jpg") center center no-repeat; 
  background-size: cover; 
  width: 100%;
  height: 100%; }
<header class="masthead" id="banner">
  <div class="container">
    <div class="col-lg-12 text-center">
      <img class="img-fluid" src="img/headshot.png" alt="Headshot">
      <div class="intro-text">
        <span class="name">NAME</span>
        <hr class="star-light">
      </div>
    </div>
  </div>
</header>

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

1 Comment

thanks so much, this works! any idea why I can't just put an image as background cover over a background color?
0

You cannot have no-repeat and cover on background-image as they are property of background. So you can have only image url on background-image.

And also try removing fallback background on header.masthead.

Try below [fiddler][1] for reference.

  [1]: https://jsfiddle.net/rajsnd08/qcwvet4p/

2 Comments

This didn't work. Now I've got a white page with no image as well
If your files are located like this, html--root--> css--root-->css--> image--root-->img--> Then you are suppose to use ../img/image.format

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.